[英]How can I get the selected item in a Notify Icon context menu within Powershell?
My code is below.我的代码如下。 I'd like to get the item/text selected from the notify icon context menu and also trigger an event from it.我想从通知图标上下文菜单中选择项目/文本,并从中触发一个事件。 I can't figure out how to obtain which menu item was clicked and am not able to trigger any events.我不知道如何获取单击了哪个菜单项并且无法触发任何事件。 I have read and reread the stackoverflow link in the Powershell comment and have not had success.我已经阅读并重新阅读了 Powershell 评论中的 stackoverflow 链接,但没有成功。 Thanks.谢谢。
As a note, you'll need to specify an ico file twice at the top to see the icon in the system tray.请注意,您需要在顶部指定两次 ico 文件才能在系统托盘中看到图标。
<#
https://stackoverflow.com/questions/54649456/powershell-notifyicon-context-menu
#>
cls;
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form1 = New-Object System.Windows.Forms.Form;
$notifyIcon= New-Object System.Windows.Forms.NotifyIcon;
$iconOK = New-Object System.Drawing.Icon([you'll need to find an icon for this using get-childitem -Recurse -Filter "*.ico" -File -Path c:\ -ErrorAction SilentlyContinue);
$iconWarn = New-Object System.Drawing.Icon([you'll need to find an icon for this using get-childitem -Recurse -Filter "*.ico" -File -Path c:\ -ErrorAction SilentlyContinue);
$menuItem1 = New-Object System.Windows.Forms.MenuItem;
$menuItem1.Text = "Menu Item 1";
$menuItem1.Name = "MenuItem1";
$menuItem1.Tag = "MenuItem1";
$menuItem2 = New-Object System.Windows.Forms.MenuItem;
$menuItem2.Text = "Menu Item 2";
$menuItem2.Name = "MenuItem2";
$menuItem2.Tag = "MenuItem2";
$menuItem3 = New-Object System.Windows.Forms.MenuItem;
$menuItem3.Text = "Menu Item 3";
$menuItem3.Name = "MenuItem3";
$menuItem3.Tag = "MenuItem3";
$contextMenu = New-Object System.Windows.Forms.ContextMenu;
$contextMenu.Name = "Context menu name";
$contextMenu.Tag = "Context menu tag";
$contextMenu.MenuItems.Add($menuItem1) | Out-Null;
$contextMenu.MenuItems.Add($menuItem2) | Out-Null;
$contextMenu.MenuItems.Add($menuItem3) | Out-Null;
$notifyIcon.Icon = $iconOK;
$notifyIcon.Visible = $True;
$menuItem1.add_Click
({
Write-Host "menuItem1.add_Click";
})
$menuItem2.add_Click
({
Write-Host "menuItem2.add_Click";
})
$menuItem3.add_Click
({
Write-Host "menuItem3.add_Click";
})
$contextMenu.add_Click
({
Write-Host "contextMenu.add_Click";
})
$notifyIcon.ContextMenu = $contextMenu;
$global:notifyIconContextMenu = $notifyIcon.ContextMenu.PSObject.Copy();
$notifyIconContextMenuMenuItems = $notifyIconContextMenu.MenuItems;
$notifyIcon.Add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderNotifyIconAddClick = $sender.PSObject.Copy();
$global:eventArgsNotifyIconAddClick = $e.PSObject.Copy();
$global:menuItemTest = $senderNotifyIconAddClick.ContextMenu;
$global:selectedContextItem = ([System.Windows.Forms.ContextMenu]$senderNotifyIconAddClick.ContextMenu)#.CommandParameter as User;
if ($_.Button -eq [Windows.Forms.MouseButtons]::Left)
{
#$form1.WindowStartupLocation = "CenterScreen"
$form1.Show();
$form1.Activate();
Write-Host "$notifyIcon.Add_Click left click";
}
elseif ($_.Button -eq [Windows.Forms.MouseButtons]::Right)
{
Write-Host "$notifyIcon.Add_Click right click";
}
})
$form1.add_Closing({
$notifyIcon.Dispose();
Write-Host "form.add_Closing completed";
return;
});
[void][System.Windows.Forms.Application]::Run($form1);
Here is a complete working example, the only thing needed will be to provide a file path to the icon files, otherwise this will allow someone to paste and modify to suit their own needs.这是一个完整的工作示例,唯一需要的是提供图标文件的文件路径,否则这将允许某人粘贴和修改以满足他们自己的需要。 The issue was that I should have been using a contextmenustrip
and toolstripitem
, rather than a contextmenu and menuitem.问题是我应该使用contextmenustrip
和toolstripitem
,而不是 contextmenu 和 menuitem。
cls;
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form1 = New-Object System.Windows.Forms.Form;
$notifyIcon= New-Object System.Windows.Forms.NotifyIcon;
$iconOK = New-Object System.Drawing.Icon([you'll need to find an icon for this using get-childitem -Recurse -Filter "*.ico" -File -Path c:\ -ErrorAction SilentlyContinue);
$iconWarn = New-Object System.Drawing.Icon([you'll need to find an icon for this using get-childitem -Recurse -Filter "*.ico" -File -Path c:\ -ErrorAction SilentlyContinue);
$notifyIcon.Icon = $iconOK;
$notifyIcon.Visible = $True;
$toolStripItemOne = New-Object -TypeName Windows.Forms.ToolStripMenuItem -ArgumentList 'File'
$toolStripItemOne.add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderToolStripItemOneAddClick = $sender.PSObject.Copy();
$global:eventArgsToolStripItemOneAddClick = $e.PSObject.Copy();
Write-Host "toolStripItemOne.add_Click - " $senderToolStripItemOneAddClick.Text;
});
$toolStripItemTwo = New-Object -TypeName Windows.Forms.ToolStripMenuItem -ArgumentList 'Edit'
$subMenuItemOne = New-Object -TypeName Windows.Forms.ToolStripMenuItem;
$subMenuItemOne.Text = "Copy";
$subMenuItemOne.add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderSubMenuItemOneAddClick = $sender.PSObject.Copy();
$global:eventArgsSubMenuItemOneAddClick = $e.PSObject.Copy();
Write-Host "subMenuItemOne.add_Click - " $senderSubMenuItemOneAddClick.Text;
});
$toolStripItemTwo.DropDownItems.Add($subMenuItemOne) | Out-Null;
$subMenuItemTwo = New-Object -TypeName Windows.Forms.ToolStripMenuItem;
$subMenuItemTwo.Text = "Paste";
$subMenuItemTwo.add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderSubMenuItemTwoAddClick = $sender.PSObject.Copy();
$global:eventArgsSubMenuItemTwoAddClick = $e.PSObject.Copy();
Write-Host "subMenuItemTwo.add_Click - " $senderSubMenuItemTwoAddClick.Text;
});
$toolStripItemTwo.DropDownItems.Add($subMenuItemTwo) | Out-Null;
$toolStripItemTwo.add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderToolStripItemTwoAddClick = $sender.PSObject.Copy();
$global:eventArgsToolStripItemTwoAddClick = $e.PSObject.Copy();
Write-Host "toolStripItemTwo.add_Click - " $senderToolStripItemTwoAddClick.Text;
});
$toolStripItemThree = New-Object -TypeName Windows.Forms.ToolStripMenuItem -ArgumentList 'Save'
$toolStripItemThree.add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderToolStripItemThreeAddClick = $sender.PSObject.Copy();
$global:eventArgsToolStripItemThreeAddClick = $e.PSObject.Copy();
Write-Host "toolStripItemThree.add_Click - " $senderToolStripItemThreeAddClick.Text;
});
$toolStripItemFour = New-Object -TypeName Windows.Forms.ToolStripMenuItem -ArgumentList 'Close'
$toolStripItemFour.add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderToolStripItemFourAddClick = $sender.PSObject.Copy();
$global:eventArgsToolStripItemFourAddClick = $e.PSObject.Copy();
Write-Host "toolStripItemFour.add_Click - " $senderToolStripItemFourAddClick.Text;
$form1.Close();
});
$contextMenuStrip = New-Object System.Windows.Forms.ContextMenuStrip;
$contextMenuStrip.Name = "Context menu strip name";
$contextMenuStrip.Tag = "Context menu strip tag";
$contextMenuStrip.Items.AddRange($toolStripItemOne);
$contextMenuStrip.Items.AddRange($toolStripItemTwo);
$contextMenuStrip.Items.AddRange($toolStripItemThree);
$contextMenuStrip.Items.AddRange($toolStripItemFour);
$contextMenuStrip.Size = New-Object System.Drawing.Size(153, 70);
$notifyIcon.ContextMenuStrip = $contextMenuStrip;
$global:notifyIconContextMenuStrip = $notifyIcon.ContextMenuStrip.PSObject.Copy();
$notifyIconContextMenuMenuItems = $notifyIconContextMenu.MenuItems;
$notifyIcon.Add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderNotifyIconAddClick = $sender.PSObject.Copy();
$global:eventArgsNotifyIconAddClick = $e.PSObject.Copy();
$global:menuItemTest = $senderNotifyIconAddClick.ContextMenu;
$global:selectedContextItem = ([System.Windows.Forms.ContextMenu]$senderNotifyIconAddClick.ContextMenu);
if ($_.Button -eq [Windows.Forms.MouseButtons]::Left)
{
$form1.Show();
$form1.Activate();
Write-Host "$notifyIcon.Add_Click left click";
}
elseif ($_.Button -eq [Windows.Forms.MouseButtons]::Right)
{
Write-Host "$notifyIcon.Add_Click right click";
}
})
$form1.add_Closing({
$notifyIcon.Dispose();
Write-Host "form.add_Closing completed";
return;
});
[void][System.Windows.Forms.Application]::Run($form1);
and here is how to display a label as the icon, which can be dynamically updated within the code这里是如何显示一个 label 作为图标,可以在代码中动态更新
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form1 = New-Object System.Windows.Forms.Form;
$notifyIcon= New-Object System.Windows.Forms.NotifyIcon;
$iconOK = New-Object System.Drawing.Icon([you'll need to find an icon for this using get-childitem -Recurse -Filter "*.ico" -File -Path c:\ -ErrorAction SilentlyContinue);
$iconWarn = New-Object System.Drawing.Icon([you'll need to find an icon for this using get-childitem -Recurse -Filter "*.ico" -File -Path c:\ -ErrorAction SilentlyContinue);
$memoryStream = New-Object System.IO.MemoryStream;
$idleTimeBitmap = New-Object System.Drawing.Bitmap(40, 14);
$idleTimeFont = New-Object System.Drawing.Font("Microsoft San Serif",7,[System.Drawing.FontStyle]::Regular);
$idleTimeBrushForegroundColor = [System.Drawing.Brushes]::White;
$idleTimeBrushBackgroundColor = [System.Drawing.Brushes]::Black;
$idleTimeImageGraphics = [Drawing.Graphics]::FromImage($idleTimeBitmap);
$idleTimeImageGraphics.FillRectangle($idleTimeBrushBackgroundColor,0,0,$idleTimeBitmap.Width,$idleTimeBitmap.Height)
$idleTimeImageGraphics.DrawString("00:00:00", $idleTimeFont, $idleTimeBrushForegroundColor, 1, 1);
$idleTimeBitmap.Save($memoryStream, [System.Drawing.Imaging.ImageFormat]::Bmp);
$toolStripItemOne = New-Object -TypeName Windows.Forms.ToolStripMenuItem -ArgumentList 'File'
$toolStripItemOne.add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderToolStripItemOneAddClick = $sender.PSObject.Copy();
$global:eventArgsToolStripItemOneAddClick = $e.PSObject.Copy();
Write-Host "toolStripItemOne.add_Click - " $senderToolStripItemOneAddClick.Text;
});
$toolStripItemTwo = New-Object -TypeName Windows.Forms.ToolStripMenuItem -ArgumentList 'Edit'
$subMenuItemOne = New-Object -TypeName Windows.Forms.ToolStripMenuItem;
$subMenuItemOne.Text = "Copy";
$subMenuItemOne.add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderSubMenuItemOneAddClick = $sender.PSObject.Copy();
$global:eventArgsSubMenuItemOneAddClick = $e.PSObject.Copy();
Write-Host "subMenuItemOne.add_Click - " $senderSubMenuItemOneAddClick.Text;
});
$toolStripItemTwo.DropDownItems.Add($subMenuItemOne) | Out-Null;
$subMenuItemTwo = New-Object -TypeName Windows.Forms.ToolStripMenuItem;
$subMenuItemTwo.Text = "Paste";
$subMenuItemTwo.add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderSubMenuItemTwoAddClick = $sender.PSObject.Copy();
$global:eventArgsSubMenuItemTwoAddClick = $e.PSObject.Copy();
Write-Host "subMenuItemTwo.add_Click - " $senderSubMenuItemTwoAddClick.Text;
});
$toolStripItemTwo.DropDownItems.Add($subMenuItemTwo) | Out-Null;
$toolStripItemTwo.add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderToolStripItemTwoAddClick = $sender.PSObject.Copy();
$global:eventArgsToolStripItemTwoAddClick = $e.PSObject.Copy();
Write-Host "toolStripItemTwo.add_Click - " $senderToolStripItemTwoAddClick.Text;
});
$toolStripItemThree = New-Object -TypeName Windows.Forms.ToolStripMenuItem -ArgumentList 'Save'
$toolStripItemThree.add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderToolStripItemThreeAddClick = $sender.PSObject.Copy();
$global:eventArgsToolStripItemThreeAddClick = $e.PSObject.Copy();
Write-Host "toolStripItemThree.add_Click - " $senderToolStripItemThreeAddClick.Text;
});
$toolStripItemFour = New-Object -TypeName Windows.Forms.ToolStripMenuItem -ArgumentList 'Close'
$toolStripItemFour.add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderToolStripItemFourAddClick = $sender.PSObject.Copy();
$global:eventArgsToolStripItemFourAddClick = $e.PSObject.Copy();
Write-Host "toolStripItemFour.add_Click - " $senderToolStripItemFourAddClick.Text;
$form1.Close();
});
$memoryStream = New-Object System.IO.MemoryStream;
$idleTimeBitmap = New-Object System.Drawing.Bitmap(32, 20);
$idleTimeFont = New-Object System.Drawing.Font("Microsoft San Serif",12,[System.Drawing.FontStyle]::Regular);
$idleTimeBrushForegroundColor = [System.Drawing.Brushes]::White;
$idleTimeBrushBackgroundColor = [System.Drawing.Brushes]::Black;
$idleTimeImageGraphics = [Drawing.Graphics]::FromImage($idleTimeBitmap);
$idleTimeImageGraphics.FillRectangle($idleTimeBrushBackgroundColor,0,0,$idleTimeBitmap.Width,$idleTimeBitmap.Height)
$idleTimeImageGraphics.DrawString("14", $idleTimeFont, $idleTimeBrushForegroundColor, 1, 1);
$idleTimeBitmap.Save($memoryStream, [System.Drawing.Imaging.ImageFormat]::Bmp);
$idleTimeIcon = [System.Drawing.Icon]::FromHandle($idleTimeBitmap.GetHicon());
$contextMenuStrip = New-Object System.Windows.Forms.ContextMenuStrip;
$contextMenuStrip.Name = "Context menu strip name";
$contextMenuStrip.Tag = "Context menu strip tag";
$contextMenuStripLabel = New-Object System.Windows.Forms.Label;
$contextMenuStripLabel.Text = "contextMenuStripLabel";
$toolStripItemLabel = New-Object -TypeName Windows.Forms.ToolStripMenuItem;
$toolStripControlHost = New-Object Windows.Forms.ToolStripControlHost($contextMenuStripLabel);
$contextMenuStrip.Items.Insert(0, $toolStripControlHost);
$contextMenuStrip.Items.Add($toolStripControlHost) | Out-Null;
$contextMenuStrip.Items.AddRange($toolStripItemOne);
$contextMenuStrip.Items.AddRange($toolStripItemTwo);
$contextMenuStrip.Items.AddRange($toolStripItemThree);
$contextMenuStrip.Items.AddRange($toolStripItemFour);
$contextMenuStrip.Size = New-Object System.Drawing.Size(153, 70);
#$notifyIcon.Icon = $iconOK;
$notifyIcon.Visible = $True;
$notifyIcon.Icon = $idleTimeIcon;
$notifyIcon.ContextMenuStrip = $contextMenuStrip;
$global:notifyIconContextMenuStrip = $notifyIcon.ContextMenuStrip.PSObject.Copy();
$notifyIconContextMenuMenuItems = $notifyIconContextMenu.MenuItems;
$notifyIcon.Add_Click({
param(
[System.Object] $sender,
[System.EventArgs] $e
)
$global:senderNotifyIconAddClick = $sender.PSObject.Copy();
$global:eventArgsNotifyIconAddClick = $e.PSObject.Copy();
$global:menuItemTest = $senderNotifyIconAddClick.ContextMenu;
$global:selectedContextItem = ([System.Windows.Forms.ContextMenu]$senderNotifyIconAddClick.ContextMenu);
if ($_.Button -eq [Windows.Forms.MouseButtons]::Left)
{
$form1.Show();
$form1.Activate();
Write-Host "$notifyIcon.Add_Click left click";
}
elseif ($_.Button -eq [Windows.Forms.MouseButtons]::Right)
{
Write-Host "$notifyIcon.Add_Click right click";
}
})
$form1.add_Closing({
$notifyIcon.Dispose();
Write-Host "form.add_Closing completed";
return;
});
[void][System.Windows.Forms.Application]::Run($form1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.