views:

377

answers:

1

I want to handle the System.Windows.Forms.NotifyIcon's BalloonTipClicked. That is to say, I want to handle the event when the tip is clicked. My code is below, however I can't catch the event. Please help !

[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 
+2  A: 

OK, I'm with you now. This works from within 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

Note this works when you click in the body of the window but not when you click the "x" to close the window. So you may want to subscribe to the BalloonTipClosed event also (or instead of BalloonTipClicked).

Keith Hill
I've refered that, but the question is it can't catch the event. As my code shows, once the even occurs, the script will NOT wait 15 seconds. But I script always wait for 15 seconds.
Sefler
It drives me crazy!!!!! When I try to run your vesion and mine in PowerShell ISE. Both works!! However, when I run them in normal PowerShell window, none of them works! I've tried them in two computers. One is Windows 7 Pro and the other is Vista Home Basic. I just can't understand why!
Sefler
Try starting your Powershell.exe with the option -STA.
Keith Hill
Yeah! It works, thank GOD!!!!!
Sefler
FYI, ISE, since it is a GUI app, uses single-threaded apartment (STA) by default whereas the console host uses multi-threaded apartment (MTA) by default unless you specify -STA when starting PowerShell.exe.
Keith Hill