vendredi 11 juin 2021

WaitAndClick

Wait&Click est une boucle qui envoie un clique de souris à l'endroit souhaité après un temps d'attente.


 
#Assembly.
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
[void][reflection.assembly]::LoadWithPartialName("Microsoft.VisualBasic")

#Variables.
$global:X = 0
$global:Y = 0
$global:Decompte = 0
$global:Attente = 20

################################################
# Types.
################################################

#Ajoute du type : Send mouse click (Left 02 + 04 / Right 08 + 10 / Middle 20 + 40).
$signSendClick = @'
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
'@
$global:sendClick = Add-Type -memberDefinition $signSendClick -name "Win32MouseEventNew" -namespace Win32Functions -passThru

################################################
# Timer.
################################################

#Timer.
$timer1 = New-Object System.Windows.forms.timer
$timer1.Interval = (1000) #Millisecondes.
$timer1.Add_Tick({
                   $global:Decompte--
                   $global:labelA1.Text = [string]$global:Decompte

                   if ($global:Decompte -eq 0)
                     {
                       #Position courante.
                       $positon = [System.Windows.Forms.Cursor]::Position

                       #Enregistre la position si elle n'est pas initialisée.
                       if (($global:X -eq 0) -and ($global:Y -eq 0)) { $global:X = $positon.X; $global:Y = $positon.Y; $global:labelA1.BackColor = "White" }

                       #Déplace la souris.
                       [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($global:X, $global:Y)

                       #Envoie un clique gauche.
                       $global:sendClick::mouse_event(0x00000002, 0, 0, 0, 0)
                       $global:sendClick::mouse_event(0x00000004, 0, 0, 0, 0)

                       #Envoie un clique droit.
                       #$global:sendClick::mouse_event(0x00000008, 0, 0, 0, 0)
                       #$global:sendClick::mouse_event(0x00000010, 0, 0, 0, 0)

                       #Envoie un clique milieu.
                       #$global:sendClick::mouse_event(0x00000020, 0, 0, 0, 0)
                       #$global:sendClick::mouse_event(0x00000040, 0, 0, 0, 0)

                       #Envoie la touche "Enter".
                       #[System.Windows.Forms.SendKeys]::SendWait("TEST")
                       #[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")

                       #Replace la souris.
                       [System.Windows.Forms.Cursor]::Position = $positon

                       #Relance de temps d'attente.
                       $global:Decompte = $global:Attente
                     }
                })

################################################
# Fonctions.
################################################

#Affiche une boite de dialogue.
function InputBox([string]$TEXT)
  {
    Try { $i = [int]([Microsoft.VisualBasic.Interaction]::InputBox($TEXT, "Notice",$global:Attente)) }
    Catch { $i = 20 }
    if ($i -eq 0) { Exit } #Bouton Annuler.
    if ($i -lt 5) { $i = 5 }
    return $i
  }

################################################
# Main.
################################################

#Démarrage du programme.
$global:Attente = InputBox "1 - Entrez le temps d'attente (en secondes).`r`n`r`n2 - Fermer la fenêtre en cliquant sur OK.`r`n`r`n3 - Positionnez la souris à l'emplacement à cliquer, et attendez la fin du premier décompte pour enregistre la position.`r`n`r`n"
$global:Decompte = $global:Attente

#Création d'une fenêtre.
$formA = New-Object Windows.Forms.Form
$formA.ClientSize = New-Object System.Drawing.Size(120,100)
$formA.MaximizeBox = 0
$formA.MinimizeBox = 0
$formA.ShowIcon = 0
$formA.FormBorderStyle = "Fixed3D"
$formA.text = "Wait&Click"
$formA.TopMost = 1 #Récupère le jeton TopMost.
$formA.ShowInTaskbar = 0
$formA.BackColor = "White"

#Création d'un label.
$global:labelA1 = New-Object Windows.Forms.Label
$global:labelA1.Location = New-Object Drawing.Point 0,0
$global:labelA1.Size = New-Object System.Drawing.Size 125,100
$global:labelA1.Font = New-Object System.Drawing.Font("Verdana","50",[System.Drawing.FontStyle]::Regular,3,0)
$global:labelA1.BackColor = [System.Drawing.Color]::FromArgb(255,248,203,47)
$global:labelA1.TextAlign = "MiddleCenter"
$global:labelA1.Text = "--"

#Attache le contrôle à la fenêtre.
$formA.controls.add($global:labelA1)

#Lance le timer.
$timer1.start()

#Affiche la fenêtre.
[void]$formA.ShowDialog()

#Fin.
$timer1.Stop()