我使用PowerShell运行以下NotifyIcon:
这是右键单击图标打开的上下文菜单,此时只显示退出:
我想知道如何添加两个事件处理程序:
我已经在网上搜索了一个多小时,我尝试使用来自5个或6个不同网站的旧代码来尝试大约20种不同的变体(所有这些都显示了截然不同的例子)。我只是头疼了。有人可以提供任何指导/指导吗?
$ProgramDataPath = "$ENV:ProgramData\test"
$ProgramFilesPath = "${env:ProgramFiles(x86)}\test"
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$STForm = New-Object System.Windows.Forms.form
$NotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$ContextMenu = New-Object System.Windows.Forms.ContextMenu
$MenuItem = New-Object System.Windows.Forms.MenuItem
$Timer = New-Object System.Windows.Forms.Timer
$HealthyIcon = New-Object System.Drawing.Icon("$ProgramFilesPath\icons\healthy.ico")
$UnhealthyIcon = New-Object System.Drawing.Icon("$ProgramFilesPath\icons\unhealthy.ico")
$STForm.ShowInTaskbar = $false
$STForm.WindowState = "minimized"
$NotifyIcon.Icon = $HealthyIcon
$NotifyIcon.ContextMenu = $ContextMenu
$NotifyIcon.ContextMenu.MenuItems.AddRange($MenuItem)
$NotifyIcon.Visible = $True
# We need to avoid using Start-Sleep as this freezes the GUI. Instead, we'll utilitse the .NET forms timer, it repeats a function at a set interval.
$Timer.Interval = 300000 # (5 min)
$Timer.add_Tick({ Load-Config })
$Timer.start()
# This will appear as a right click option on the system tray icon
$MenuItem.Index = 0
$MenuItem.Text = "Exit"
$MenuItem.add_Click({
$Timer.Stop()
$NotifyIcon.Visible = $False
$STForm.close()
})
function Load-Config
{
#Get-Content some Data from a file here
if ($warn)
{
$NotifyIcon.Icon = $UnhealthyIcon
$NotifyIcon.ShowBalloonTip(30000, "Attention!", "Some data from a file here...", [system.windows.forms.ToolTipIcon]"Warning")
Remove-Variable warn
}
else
{
$NotifyIcon.Icon = $HealthyIcon
}
}
Load-Config
[void][System.Windows.Forms.Application]::Run($STForm)
让我们谈谈你真正需要的东西。看起来你有很多不需要的部分,如计时器等。您只需要一个运行空间。打开表单将保持运行空间打开,而无需该计时器。确保$Form.ShowDialog()
是最后运行的东西。
所以让我们转到NotifyIcon弹出窗口。使弹出窗口发生的方法是私有的,这意味着我们需要通过反射来实现它。我们还需要设置通知图标的事件在MouseDown
上运行以及点击按钮$_.button
确保将$NotifyIcon.Icon
设置为Icon,否则Notify Icon将不会显示。
工作脚本
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.ShowInTaskbar = $true
$form.WindowState = [System.Windows.WindowState]::Normal
$MenuItemLeft = New-Object System.Windows.Forms.MenuItem
$MenuItemLeft.Text = "Left Exit"
$MenuItemLeft.add_Click({
$NotifyIcon.Visible = $False
$form.Close()
$NotifyIcon.Dispose()
})
$ContextMenuLeft = New-Object System.Windows.Forms.ContextMenu
$ContextMenuLeft.MenuItems.Add($MenuItemLeft)
$MenuItemRight = New-Object System.Windows.Forms.MenuItem
$MenuItemRight.Text = "Right Exit"
$MenuItemRight.add_Click({
$NotifyIcon.Visible = $False
$form.Close()
$NotifyIcon.Dispose()
})
$ContextMenuRight = New-Object System.Windows.Forms.ContextMenu
$ContextMenuRight.MenuItems.Add($MenuItemRight)
$NotifyIcon= New-Object System.Windows.Forms.NotifyIcon
$NotifyIcon.Icon = "C:\Test\Test.ico"
$NotifyIcon.ContextMenu = $ContextMenuRight
$NotifyIcon.add_MouseDown({
if ($_.Button -eq [System.Windows.Forms.MouseButtons]::left ) {
$NotifyIcon.contextMenu = $ContextMenuLeft
}else{
$NotifyIcon.contextMenu = $ContextMenuRight
}
$NotifyIcon.GetType().GetMethod("ShowContextMenu",[System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).Invoke($NotifyIcon,$null)
})
$NotifyIcon.Visible = $True
$form.ShowDialog()
$NotifyIcon.Dispose()
我重读你的帖子,生病了,为你提供最重要的部分
要使Powershell运行命令,必须激活运行空间。 Runspace采用powershell命令并将其转换为实际操作。
由于您使用PowerShell,因此notifyicons操作依赖于运行空间来解释这些操作。
NotifyIcon基本上只是角落里的一个图标,可以弹出气球通知或上下文菜单。
因此,当您查看时,您将看到$NotifyIcon.ContextMenu
,它是一个包含ContextMenu对象的属性。上下文菜单对象包含菜单项。
因此,只需将MenuItems添加到ContextMenu对象,然后将该ContextMenu对象添加到$ NotifyIcon.ContextMenu。现在您可以更改并添加您喜欢的所有项目。
由于powershell在继续下一行代码之前等待表单关闭,因此$Form.ShowDialog()
将保持运行空间的活动状态,直到表单退出。
让我们看看这个令人讨厌的混乱:$NotifyIcon.GetType().GetMethod("ShowContextMenu",[System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic).Invoke($NotifyIcon,$null)
这称为反射。它允许您与班级互动。更容易理解。 ShowContextMenu方法是私有的,不能从类的内部工作之外正常运行。使用反射,无论如何都可以调用它。
因此,让我们分解一下,因为这真的是你所问的。
GetType()将为您提供对象的内容。如果我做"HEY".gettype()
它会告诉我这个对象是一个字符串。在这种情况下,$NotifyIcon.GetType()
告诉我这是一个NotifyIcon。它发生了什么,它带回了类型类。
在这里我们看到GetMethod("ShowContextMenu")
,但让我在这里深入挖掘......我们怎么知道有一个名为ShowContextMenu的方法。那么我们可以做的是使用GetMembers()
查看此NotifyIcon类的所有成员。现在GetMembers()
实际上只是一个搜索...默认情况下只搜索公共会员,所以我们需要搜索所有会员。要搜索的内容的参数是枚举[System.Reflection.BindingFlags]
和一些Bitwise数学。
$BitWise
[System.Reflection.BindingFlags].GetEnumNames() | %{
$BitWise = $BitWise -bor [System.Reflection.BindingFlags]$_
} | out-null
$NotifyIcon.GetType().GetMembers($BitWise) | ?{$_.Name -like "*Context*"} | select Name, MemberType
这表示查找名称中包含单词Context的所有项目,并显示其全名及其类型。作为回应我们得到
Name MemberType
---- ----------
set_ContextMenu Method
get_ContextMenu Method
get_ContextMenuStrip Method
set_ContextMenuStrip Method
ShowContextMenu Method
ContextMenu Property
ContextMenuStrip Property
contextMenu Field
contextMenuStrip Field
我们可以看到ShowContextMenu,我们也可以看到它的方法
所以现在我们需要直接获得该方法。来自getMethod()
这是另一个搜索带回1项而不是所有项目。所以
GetMethod("ShowContextMenu",[System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic)
获取方法ShowContextMenu它将是Private,因此NonPublic和类的实例必须先创建才能运行Instance。
.Invoke($NotifyIcon,$null)
然后我们通过告诉它哪个控件具有我们想要运行的方法来调用该方法,并传递任何非零$ null的参数。
那就是你如何做到的。