0

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 

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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!
Try starting your Powershell.exe with the option -STA.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.