2010-01-22 46 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