【问题标题】:Open a window from System Tray icon从系统托盘图标打开一个窗口
【发布时间】:2013-02-19 23:28:16
【问题描述】:

所以我创建了一个带有系统托盘图标的窗口。该窗口开始时最小化,并在单击系统托盘图标时重新出现。但是,它仅在您单击最小化按钮时才有效。如果单击红色退出按钮,窗口会消失,系统托盘图标仍然存在(应该如此),但是当您单击它时,程序会引发错误。

无法设置可见性或调用 Show、ShowDialog 或 WindowInteropHelper.EnsureHandle 窗口关闭后。

这里是相关代码

public partial class MainWindow : Window
{
    public static NotifyIcon icon;

    List<string> food = new List<string>();
    bool on = false;

    public MainWindow()
    {
        InitializeComponent();

        food.Add("Breakfast");
        food.Add("Soups");
        food.Add("Vegetables");
        food.Add("Crab roll");
        food.Add("Sushi");
        food.Add("Egg rolls");
        food.Add("Salad");


        MainWindow.icon = new NotifyIcon();

        window1.WindowState = WindowState.Minimized; 

        icon.Icon = new System.Drawing.Icon("favicon.ico");
        icon.Visible = true;

        icon.Click += new EventHandler(icon_Click);
        icon.BalloonTipClicked += new EventHandler(icon_BalloonTipClicked);
        icon.DoubleClick += new EventHandler(icon_DoubleClick);
        icon.BalloonTipClosed += new EventHandler(icon_BalloonTipClosed);
        icon.MouseMove += new System.Windows.Forms.MouseEventHandler(icon_MouseMove);
        StateChanged += new EventHandler(MainWindow_StateChanged);

    }

    void icon_BalloonTipClicked(object sender, EventArgs e)
    {
        this.Show(); //This is where the error is 
        window1.WindowState = WindowState.Normal;
    }

    void icon_DoubleClick(object sender, EventArgs e)
    {
        this.Show(); //This is where the error is 
        window1.WindowState = WindowState.Normal;
    }

    void icon_BalloonTipClosed(object sender, EventArgs e)
    {
        on = false;
    }

    void icon_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {

        if (!on)
        {
            icon.BalloonTipText = "";
            foreach(string item in food){
                if (!item.Contains("Breakfast") && !item.Contains("Soups") && !item.Contains("Vegetables"))
                {
                    icon.BalloonTipText += item+"\n";
                }
            }
            icon.ShowBalloonTip(10);
            on = true;
        }

    }

    void MainWindow_StateChanged(object sender, EventArgs e)
    {
        if (window1.WindowState == WindowState.Minimized)
        {
            this.Hide();
        }
    }

    private void icon_Click(Object sender, EventArgs e)
    {
        icon.BalloonTipText = "";
        foreach (string item in food)
        {
            if (!item.Contains("Breakfast") && !item.Contains("Soups") && !item.Contains("Vegetables"))
            {
                icon.BalloonTipText += item + "\n";
            }
        }
        icon.ShowBalloonTip(10);
        on = true;

    }        
}

【问题讨论】:

  • 相关代码,还是全部代码。有很多代码要看。
  • 哈哈,是的,对不起,我想相关的相关...

标签: c# wpf icons windowstate


【解决方案1】:

拦截窗口的Closing事件,并取消它(这将阻止窗口关闭)-然后隐藏窗口:

    public MainWindow()
    {
        // Subscribe to closing event (when X is pressed)
        this.Closing += MainWindow_Closing;
        InitializeComponent();
    }

    void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        // Prevent window from closing
        e.Cancel = true;

        // Hide window
        this.Hide();
    }

【讨论】:

  • 这就是我喜欢这个网站的原因。我什至不能接受这个作为答案,但我很快就得到了。谢谢它的工作原理!
猜你喜欢
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
  • 1970-01-01
  • 2011-11-11
相关资源
最近更新 更多