2010-01-22 47 views
0

我想處理System.Windows.Forms.NotifyIcon的BalloonTipClicked。也就是說,我想在點擊提示時處理事件。我的代碼在下面,但我無法趕上事件。請幫忙 !powershell 2事件處理

[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

好吧,我現在和你在一起。這個工作在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 

注意,當你在窗口的身體點擊,但當你點擊「X」關閉窗口工作原理。所以你可能也想訂閱BalloonTipClosed事件(或者不是BalloonTipClicked)。

+0

我提到過,但問題是它無法捕捉事件。正如我的代碼所示,一旦發生,腳本就不會等待15秒。但我的腳本總是等待15秒。 – Sefler 2010-01-22 15:23:20

+0

它讓我瘋狂!當我嘗試在PowerShell ISE中運行你的vesion和我的。兩個作品!但是,當我在正常的PowerShell窗口中運行它們時,它們都不起作用!我在兩臺電腦上嘗試過它們。一個是Windows 7 Pro,另一個是Vista Home Basic。我只是不明白爲什麼! – Sefler 2010-01-22 17:06:02

+0

嘗試使用選項-STA啓動Powershell.exe。 – 2010-01-22 18:02:27