C# 靠近托盘(如 msn Messenger)
我有 ac# .net 应用程序。所以我创建了一个位于托盘中的notifyIcon。我想要做的是,当用户点击表单上的“x”按钮时,我希望它靠近托盘。他们应该只能通过使用托盘图标中的上下文菜单退出程序。
所以我所做的是,在表单关闭事件中,我检查表单是否可见。如果它可见,我将其设置为不可见并将 showInTaskbar 设置为 false(模拟最小化到托盘)如果表单已经不可见,那么他们可能会从托盘中关闭它,所以在这种情况下我将退出程序。
但是,我遇到的问题是,如果窗口可见,但他们右键单击托盘图标的上下文菜单并单击退出,我需要退出程序而不是最小化。
我该如何解决这个问题?
I have a c# .net app. So I created a notifyIcon that sits in the tray. What I want to do is when the user hits the "x" button on the form, I want it to close to the tray. They should only be able to exit program by using the context menu in the tray icon.
So what I did was, on the form close event, I check whether the form is visible. If its visible, i set it to invisible and set showInTaskbar to false (simulating minimize to tray) If the form is invisible already, then they are probably closing it from the tray, so I will exit the program in that case.
However, the problem I have is that if the window is visible, but they right click on the context menu of the tray icon and hit exit, I need to exit the program and not minimize.
How do I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
或者如果您只有一种表单,您可以在上下文菜单项处理程序中调用
Application.Exit();
try this:
or if you have only one form you can call
Application.Exit();
in context menu item handler您可能希望根据用户的操作来跟踪应用程序的状态,因为这不一定反映在窗口的状态中。因此,当用户从菜单中选择“退出”时,您需要设置一个标志来指示您确实正在退出,而不仅仅是隐藏窗口。
You probably want to track the state of the application based on the actions of the user as that's not necessarily reflected in the state of the window. So when the user selects Exit from the menu you need to set a flag to indicate that you're really exiting, not just hiding the window.
只需让您的上下文菜单关闭事件调用 Application.Exit()
Just make your Context Menu close event call Application.Exit()