NotifyIcon隐藏任务栏中的应用程序。怎么避免呢?

时间:2013-06-25 11:31:43

标签: c# .net wpf winforms notifyicon

当我最小化它时,我有一个应用程序进入系统托盘。我创建了一个通知图标来使用该通知图标从鼠标右键单击来处理应用程序的一些辅助选项。

我希望应用程序在我最小化它并保留系统托盘上的通知图标时不会从任务栏中消失。

有没有办法来完成这个?

编辑:当我minimize应用程序时,我使用隐藏()命令来使用NotifyIcon。但我希望它继续留在任务栏上。

请参阅此处的代码:

private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
    if (WindowState != WindowState.Minimized) return;
    Hide();
    ShowInTaskbar = true;
    if (notifyIcon != null)
        notifyIcon.ShowBalloonTip(2000);
}

注意:NotifyIcon以编程方式嵌入到WPF容器中:

DrawNotifyIcon();

private void DrawNotifyIcon()
    {
        try
        {
            string source = Path.GetDirectoryName(Assembly.GetAssembly(typeof(MainWindow)).CodeBase);
            string tmpSource = source + @"\Resources\mainico.ico";
            tmpSource = tmpSource.Replace(@"file:\", "");
            // notify Icon
            notifyIcon = new NotifyIcon
                {
                    BalloonTipTitle = Cultures.Resources.Title,
                    BalloonTipText = Cultures.Resources.NotifyIconExecuting,
                    BalloonTipIcon = ToolTipIcon.Info,
                    Icon = new System.Drawing.Icon(tmpSource)
                };
            notifyIcon.DoubleClick += notifyIcon_DoubleClick;
            notifyIcon.Click += notifyIcon_Click;
            notifyIcon.MouseUp += notifyIcon_MouseUp;

            // Create ContextMenu
            contextMenu = new ContextMenuStrip();
            contextMenu.Closing += contextMenu_Closing;

            // Exit item
            menuItemExit = new ToolStripMenuItem
                {
                    Text = Cultures.Resources.Exit,
                    Image = Cultures.Resources.close
                };
            menuItemExit.Click += menuItemExit_Click;

            // Restore item
            menuItemRestore = new ToolStripMenuItem
                {
                    Text = Cultures.Resources.Restore,
                    Image = Cultures.Resources.restore1
                };
            menuItemRestore.Click += menuItemRestore_Click;

            // Active or inactive log
            menuItemActive = new ToolStripMenuItem
                {
                    Text = Cultures.Resources.On,
                    Image = Cultures.Resources.green,
                    Checked = true
                };
            menuItemActive.Click += menuItemActive_Click;
            menuItemActive.CheckStateChanged += menuItemActive_CheckStateChanged;

            // Order of appearance of ContextMenu items
            contextMenu.Items.Add(menuItemActive);
            contextMenu.Items.Add("-");
            contextMenu.Items.Add(menuItemRestore);
            contextMenu.Items.Add(menuItemExit);

            notifyIcon.ContextMenuStrip = contextMenu;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

如何保留WPF的两个图标?

1 个答案:

答案 0 :(得分:1)

嗯,不可能在处于隐藏状态的任务栏中显示表单。你仍然可以强制最小化表格。尝试以下修改后的代码:

private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
    if (WindowState != WindowState.Minimized) return;
    this.ShowInTaskbar = true;
    if (notifyIcon != null)
    notifyIcon.ShowBalloonTip(2000);
    this.WindowState = FormWindowState.Minimized;
}