powershell 2事件处理 [英] powershell 2 event handling

查看:151
本文介绍了powershell 2事件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想处理System.Windows.Forms.NotifyIcon的BalloonTipClicked。也就是说,我想在点击提示时处理事件。我的代码在下面,但是我无法捕捉到这个事件。请帮助!

  [void] [System.Reflection.Assembly] :: LoadWithPartialName(System.Windows.Forms)
[void] [System.Reflection.Assembly] :: LoadWithPartialName(System.Timers)

##这是您的下载文件的位置
$ notification =E :\TDdownload

$ notification =新对象System.Windows.Forms.NotifyIcon

$ notification.Icon =C:\Users\Sefler\ Desktop \PerfCenterCpl.ico
$ notification.BalloonTipIcon =Info
$ notification.BalloonTipText =Windows现在将按照计划清理+ $ fileLocation +。
$ notification.BalloonTipTitle =Windows自动维护

$ notification.Visible = $ True
$ notification.ShowBalloonTip(15000)

##注册一个点击事件
register-objectevent $ notification BalloonTipClicked -sourceIdentifier notification_event

##等待onClick事件
wait-event -timeout 15


解决方案

好的,我现在和你在一起这可以在ISE内工作:

  [void] [System.Reflection.Assembly] :: LoadWithPartialName(System.Windows.Forms )
[void] [System.Reflection.Assembly] :: LoadWithPartialName(System.Timers)

##这是您的下载文件的位置
$ notification =E:\TDdownload

$ notification =新对象System.Windows.Forms.NotifyIcon

$ notification.Icon =C:\Users\ Sefler\Desktop\PerfCenterCpl.ico
$ notification.BalloonTipTitle =Windows自动维护
$ notification.BalloonTipIcon =信息
$ title =Windows现在将尝试清理{0}按计划进行。 -f $ fileLocation
$ notification.BalloonTipText = $ title
$ notification.Visible = $ True
##清除以前的任何事件
删除事件notification_event -ea SilentlyContinue
##注册一个点击事件
register-objectevent $ notification BalloonTipClicked notification_event
$ notifications.ShowBalloonTip(15000)

##等待onClick事件
wait- event -timeout 15 -sourceIdentifier notification_event> $ null
删除事件notification_event -ea静默继续

完成!!

注销事件-SourceIdentifier通知_event

注意,当您点击当您点击x关闭窗口时,窗口的主体不过。所以你也可以订阅BalloonTipClosed事件(或者不是BalloonTipClicked)。


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 

解决方案

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

这篇关于powershell 2事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆