如何使 Windows 窗体 .NET 应用程序显示为托盘图标?
需要做什么才能让您的 .NET 应用程序作为图标显示在 Window 的系统托盘中?
您如何处理该图标上的鼠标按钮点击?
What needs to be done to have your .NET application show up in Window's system tray as icon?
And how do you handle mousebutton clicks on said icon?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,向形式。 然后连接通知图标以执行您想要的操作。
如果您希望它隐藏在最小化托盘上,请尝试此操作。
我偶尔会使用气球文本来通知用户 - 这样做是这样的:
First, add a NotifyIcon control to the Form. Then wire up the Notify Icon to do what you want.
If you want it to hide to tray on minimize, try this.
I'll occasionally use the Balloon Text in order to notify a user - that is done as such:
您可以将工具箱中的 NotifyIcon 组件添加到主窗体中。
它具有诸如 MouseDoubleClick 之类的事件,您可以使用它们来处理各种事件。
编辑:如果您希望图标正确显示在系统托盘中,则必须确保将 Icon 属性设置为有效的 .ico 文件。
You can add the NotifyIcon component from the toolbox onto your main form.
This has events such as MouseDoubleClick that you can use to handle various events.
Edit: You have to make sure that you set the Icon property to a valid .ico file if you want it to show up properly in the systray.
为了扩展Tom的回答,我喜欢仅在应用程序最小化时使图标可见。
为此,请为 NotifyIcon 并使用以下代码。
我还有下面的代码可以在关闭期间隐藏图标,以防止应用程序关闭后仍然存在烦人的幽灵托盘图标。
如果要添加右键菜单:
VB.NET:如何为托盘图标制作右键菜单
根据文章(带有上下文的 mods) ):
设置用于承载托盘图标上下文菜单的表单
后面的表单代码如下所示:
然后,我将 notificationicon 鼠标事件更改为此(
TrayIconMenuForm
是用于提供上下文菜单的表单的名称):To extend Tom's answer, I like to only make the icon visible if the application is minimized.
To do this, set
Visible = False
for NotifyIcon and use the below code.I also have code below to hide the icon during close the prevent the annoying ghost tray icons that persist after application close.
If you want to add a right click menu:
VB.NET: How to Make a Right Click Menu for a Tray Icon
Per the article (with mods for context):
Setting up the Form for hosting the tray icon context menu
The Form code behind will look like this:
I then change the notifyicon mouse event to this (
TrayIconMenuForm
is the name of my Form for providing the context menu):