2010-01-22 8 views
0

Je souhaite gérer le composant BalloonTipClicked de System.Windows.Forms.NotifyIcon. C'est-à-dire que je veux gérer l'événement quand on clique sur la pointe. Mon code est ci-dessous, mais je ne peux pas attraper l'événement. S'il vous plaît aider!powershell 2 gestion des événements

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

## This is the location of your download files 
$notification = "E:\TDdownload" 

$notification = New-Object System.Windows.Forms.NotifyIcon 

$notification.Icon = "C:\Users\Sefler\Desktop\PerfCenterCpl.ico" 
$notification.BalloonTipIcon = "Info" 
$notification.BalloonTipText = "Windows will now try to clean "+ $fileLocation +" as scheduled." 
$notification.BalloonTipTitle = "Windows auto maintaince" 

$notification.Visible = $True 
$notification.ShowBalloonTip(15000) 

## Register a click event 
register-objectevent $notification BalloonTipClicked -sourceIdentifier notification_event 

## Wait for the onClick event 
wait-event -timeout 15 

Répondre

2

OK, je suis avec vous maintenant. Cela fonctionne à l'intérieur ISE:

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

## This is the location of your download files 
$notification = "E:\TDdownload" 

$notification = New-Object System.Windows.Forms.NotifyIcon 

$notification.Icon = "C:\Users\Sefler\Desktop\PerfCenterCpl.ico" 
$notification.BalloonTipTitle = "Windows auto maintaince" 
$notification.BalloonTipIcon = "Info" 
$title = "Windows will now try to clean {0} as scheduled." -f $fileLocation 
$notification.BalloonTipText = $title 
$notification.Visible = $True 
## Clear any previous events 
Remove-Event notification_event -ea SilentlyContinue 
## Register a click event 
register-objectevent $notification BalloonTipClicked notification_event 
$notification.ShowBalloonTip(15000) 

## Wait for the onClick event 
wait-event -timeout 15 -sourceIdentifier notification_event > $null 
Remove-Event notification_event -ea SilentlyContinue 

"Done!!" 

Unregister-Event -SourceIdentifier notification_event 

Notez que cela fonctionne lorsque vous cliquez dans le corps de la fenêtre, mais pas lorsque vous cliquez sur le « x » pour fermer la fenêtre. Donc, vous pouvez vous abonner à l'événement BalloonTipClosed aussi (ou à la place de BalloonTipClicked).

+0

Je me suis référé à cela, mais la question est qu'il ne peut pas attraper l'événement. Comme mon code le montre, une fois que le pair est arrivé, le script n'attendra PAS 15 secondes. Mais je script toujours attendre 15 secondes. – Sefler

+0

Ça me rend fou !!!!! Quand j'essaye d'exécuter votre vesion et le mien dans PowerShell ISE. Les deux fonctionnent !! Cependant, quand je les lance dans la fenêtre normale de PowerShell, aucun d'eux ne fonctionne! Je les ai essayés sur deux ordinateurs. L'un est Windows 7 Pro et l'autre est Vista Home Basic. Je ne peux pas comprendre pourquoi! – Sefler

+0

Essayez de démarrer votre Powershell.exe avec l'option -STA. –