开发者

powershell 2 event handling

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 h开发者_运维百科elp !

[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 


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜