C# 靠近托盘(如 msn Messenger)

发布于 2024-09-11 21:17:55 字数 318 浏览 4 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜宝宝 2024-09-18 21:17:55

试试这个:

bool _closingFromMenu;

void NOTIFYICON_EXIT_MENU_HANDLER(object sender, EventArgs e)
{
    _closingFromMenu = true;
    Close();
}

//form closing handler
FormClosing +=(a,b) =>{
    if(_closingFromMenu){
        Close();
    }
    else{
        e.Cancel = true;
        //do minimize stuff;
    }
}

或者如果您只有一种表单,您可以在上下文菜单项处理程序中调用 Application.Exit();

try this:

bool _closingFromMenu;

void NOTIFYICON_EXIT_MENU_HANDLER(object sender, EventArgs e)
{
    _closingFromMenu = true;
    Close();
}

//form closing handler
FormClosing +=(a,b) =>{
    if(_closingFromMenu){
        Close();
    }
    else{
        e.Cancel = true;
        //do minimize stuff;
    }
}

or if you have only one form you can call Application.Exit(); in context menu item handler

笑,眼淚并存 2024-09-18 21:17:55

您可能希望根据用户的操作来跟踪应用程序的状态,因为这不一定反映在窗口的状态中。因此,当用户从菜单中选择“退出”时,您需要设置一个标志来指示您确实正在退出,而不仅仅是隐藏窗口。

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.

尹雨沫 2024-09-18 21:17:55

只需让您的上下文菜单关闭事件调用 Application.Exit()

Just make your Context Menu close event call Application.Exit()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文