如何在WPF中的系统托盘中编写“退出”菜单项? [关闭]

问题描述 投票:-4回答:1

当我右键单击我的应用程序的系统托盘图标时,我想在菜单上显示“退出”选项,当我单击此选项时,我希望程序退出。我怎样才能做到这一点?

c# wpf menuitem exit system-tray
1个回答
1
投票

你可以举办一个活动,比方说双击关闭应用程序。因此,当用户双击系统托盘中的图标时,应用程序将退出。您还可以构建托盘菜单并选择“退出”。

System.Windows.Forms.NotifyIcon notifyIcon;
public MainWindow()
{
    InitializeComponent();
    notifyIcon = new System.Windows.Forms.NotifyIcon();
    notifyIcon.DoubleClick += notifyIcon_DoubleClick;
    notifyIcon.Icon = new System.Drawing.Icon("MyIcon.ico");
    notifyIcon.Visible = true;
}

void notifyIcon_DoubleClick(object sender, EventArgs e)
{
    System.Windows.Application.Current.Shutdown();
    // OR
    // Environment.Exit(0)
}
© www.soinside.com 2019 - 2024. All rights reserved.