-1

I have an application that goes to system tray when I minimize it. I created a notify icon to handle some secondary options for the app using right click from mouse using that notify icon.

But I would like the application not to disappear from task bar when I minimize it and mantain the notify icon on system tray.

Is there any way to acomplish thtat?

EDIT: When I minimize the application, I use the Hide() command to use the NotifyIcon. But I want it to remain on task bar.

See code here:

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

Note: This NotifyIcon is embeded on a WPF container programatically like this:

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);
        }
    }

Any idea on how to keep both icons for WPF?

Sonhja
  • 8,230
  • 20
  • 73
  • 131
  • Just delete that code. – Hans Passant Jun 25 '13 at 12:23
  • But if I delete that code I miss the NotifyIcon... – Sonhja Jun 25 '13 at 12:59
  • Your user won't miss it. If it is important enough then he'll make sure that it doesn't disappear in the icon overflow area. Of course, so can you. Click the triangle in the notification area and click Customize. – Hans Passant Jun 25 '13 at 13:15
  • @HansPassant thanks. I saw that it works on WinForms, but I'm using WPF and embeding a NotifyIcon on a WinForms container. So I guess I'm missing anything... ? – Sonhja Jun 25 '13 at 13:23

1 Answers1

1

Well, Its not possible to show forms in taskbar which is in hidden state. Still you can forcefully minimized the form. try below modified code :

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;
}
Manoj
  • 863
  • 1
  • 8
  • 12
  • This code makes the form to minimize at task bar, but I need the notifyIcon at system tray and the app minimized normally. – Sonhja Jun 25 '13 at 12:37