Affichage des articles dont le libellé est Timer. Afficher tous les articles
Affichage des articles dont le libellé est Timer. Afficher tous les articles

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()

jeudi 18 décembre 2014

Console avec minuteur (timer)

Nous allons voir dans cet article deux méthodes pour créer un timer et afficher le résultat d'une commande à intervalle régulier. Dans les exemples suivants, je vais afficher le résultat de la commande "Get-Process" toutes les 5 secondes. Mon premier script montre comment créer un timer dans une console PowerShell.

Exemple 1


 
#Création d'un timer.
$timer = new-object timers.timer
$timer.Interval = 1000 #Démarrage.
$nom = "T1"            #Identifiant de mon timer.
$boucle = 5            #Arrête le timer après 5 exécutions.

#Action.
$action = {
            #Initialisation.
            $timer.Interval = 5000  #En milliseconde.
            #Action.
            Write-Host (Get-Process | Out-String)
            #Supprimer le timer.
            if (--$boucle -eq 0)
              {
                $timer.stop()
                Unregister-Event $nom
                Write-host "Timer" $Nom "supprimé."
              }
          }

#Lancement du timer.
Register-ObjectEvent -InputObject $timer -EventName elapsed –SourceIdentifier $nom -Action $action
$timer.start()

#Fin.


Exemple 2


Le script suivant est une solution alternatif. Il  utilise le timer des fenêtres (Forms) pour afficher toutes les 5 secondes le résultat de la commande "Get-Process".

 
######################
# Console avec timer
######################

#Assembly.
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

#Plugin.
Get-Command -PSSnapin VirtualMachineManagerSnapin

#Paramêtres.
$global:consw = 600      #Largeur de la console au démarrage.
$global:consh = 300      #Hauteur de la console au démarrage.
$global:intvl = 5        #Intervale d'execution du timer (en seconde).
$global:titre = "Console avec timer"  #Titre de la fenêtre.

###########
# Timers
###########

#Timer.
$timer1 = new-object System.Windows.forms.timer
$timer1.Interval = 1000      #Démarrage rapide.

#Action.
$timer1.Add_Tick({
                   #Initialisation.
                   $global:labelA2.text = "Scan en cours ..."
                   #Intervalle après démarrage.
                   $timer1.Interval = ($global:intvl * 1000)
                   #Commande.
                   $global:labelA1.text = (Get-Process | Out-String)
                   #Fin.
                   $global:labelA2.text = "Dernier scan le " + (date -Format "dd-MM-yyyy à HH:mm:ss")
                })

#########################
# Fenêtre style console
#########################

#Ouvre une nouvelle fenêtre.
$formA = New-Object Windows.Forms.Form
$formA.text = $global:titre          
$formA.Size = New-Object System.Drawing.Size($global:consw,$global:consh)
$formA.BackColor = [System.Drawing.Color]::FromArgb(255,1,36,86)

#Création d'un label (Label).
$global:labelA1 = New-Object Windows.Forms.Label
$global:labelA1.Location = New-Object Drawing.Point 9,0
$global:labelA1.Size = New-Object System.Drawing.Size($formA.Size.Width,$formA.Size.Height)
$global:labelA1.ForeColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$global:labelA1.Font = New-Object System.Drawing.Font("Consolas","9",[System.Drawing.FontStyle]::Regular,3,0)
$global:labelA1.text = ""

#Création d'un label (Label).
$global:labelA2 = New-Object Windows.Forms.Label
$global:labelA2.Location = New-Object Drawing.Point 0,(($formA.Size.Height) - 56)
$global:labelA2.Size = New-Object System.Drawing.Size(($formA.Size.Width),20)
$global:labelA2.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
$global:labelA2.BackColor = [System.Drawing.Color]::FromArgb(255,222,222,222)
$global:labelA2.Font = New-Object System.Drawing.Font("Consolas","9",[System.Drawing.FontStyle]::Regular,3,0)
$global:labelA2.TextAlign = "MiddleLeft"
$global:labelA2.text = "Initialisation ..."

#Events.
$formA.Add_Resize({
                    $global:labelA1.Size = New-Object System.Drawing.Size($formA.Size.Width,$formA.Size.Height)
                    $global:labelA2.Location = New-Object Drawing.Point 0,(($formA.Size.Height) - 56)
                    $global:labelA2.Size = New-Object System.Drawing.Size(($formA.Size.Width),20)
                 })

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

#Lance le timer.
$timer1.Start()

#Affiche le tout.
$formA.ShowDialog()

#Libère le timer.
$timer1.Stop()

#Fin.

mercredi 12 décembre 2012

Minuteur (Timer).

Lorsque nous ouvrons une fenêtre, il est possible de lui attribuer des minuteurs (Timer) afin de déclencher une action à intervalles de temps réguliers. Dans l'exemple suivant, je vais créer une fenêtre qui va afficher la date toutes les 5 secondes.

 
#Ouvre une fenêtre.
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form = New-Object Windows.Forms.Form
$form.text = "Timer"            
$form.Size = New-Object System.Drawing.Size(160,110)

#Création d'un label (Label).
$label1 = New-Object Windows.Forms.Label
$label1.Location = New-Object Drawing.Point 20,30
$label1.Size = New-Object Drawing.Point 150,16
$label1.Text = date

#Minuteur (Timer).
$timer1 = new-object System.Windows.forms.timer
$timer1.Interval = 5000              #en milliseconds
$timer1.Add_Tick({   $label1.Text = date   })
$timer1.Start()

#Attache le contrôle à la fenêtre.
$form.controls.add($label1)

#Affiche le tout.
$form.ShowDialog()

#Libère le timer.
$timer1.Stop()

#Fin.

Lorsque le script du minuteur est en cours d'exécution, la fenêtre est gelée. Il n'est alors plus possible d'actionner les contrôles ou de fermer la fenêtre. Dans notre exemple, le script du minuteur s'exécute très rapidement. Cela passe donc inaperçu. Mais si nous devions exécuter un script plus long, cela pourrait poser un problème. Si c'est le cas, regardez du côté des runspaces.