C# WinForms托盘应用MenuItem鼠标悬停检测
我有一个相当简单的问题,但我找不到解决方案。我有一个驻留在任务托盘中的应用程序。当用户右键单击托盘图标时,程序会显示 MenuItems 菜单。我想在鼠标悬停在某些菜单项上时执行代码。
这可能吗?
你能带我到正确的方向吗?
我正在使用 NotifyIcon
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Exit", OnExit);
trayIcon = new NotifyIcon();
trayIcon.Text = "blah";
trayIcon.Icon = new Icon("favicon.ico", 40, 40);
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
I have a fairly simple question, but I'm failing to find the solution. I have an application that resides in the task tray. When a user right clicks the tray icon, the program displays a menu of MenuItems. I would like to execute code when some of my MenuItems are mouse hovered over.
Is this possible?
Can you send me in the right direction?
I am using NotifyIcon
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Exit", OnExit);
trayIcon = new NotifyIcon();
trayIcon.Text = "blah";
trayIcon.Icon = new Icon("favicon.ico", 40, 40);
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须使用每个菜单项的
MouseHover
或MouseEnter
和MouseLeave
事件。更新:
是的,NotifyIcon 控件有一个名为 ContextMenuStrip 的属性。您必须创建 ContextMenuStrip 控件才能显示菜单。它包含 ToolStripMenuItems 类型的项目。我尝试创建一个简单的原型 -
MouseHover
工作得很好。You'll have to use
MouseHover
orMouseEnter
andMouseLeave
events of each menuitem.Update:
Yep, NotifyIcon controls have a property named ContextMenuStrip. You'll have to create the ContextMenuStrip control to display the menu. It contains items of ToolStripMenuItems type. I tried to create a simple prototype -
MouseHover
works just fine.我想您可能需要 MenuItem 的 选择事件:
I think you might want the MenuItem's Select event:
这是您的解决方案 https://www.codeproject.com/提示/254525/悬停时自动显示菜单
Here is your solution https://www.codeproject.com/Tips/254525/Automatically-display-Menu-on-Hover