Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start WinForm app minimized to tray?

I've successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs when trying to start with the app minimized. At first the problem was that the app would be minimized but would still appear in the alt-tab dialog. Changing the FormBorderStyle to one of the ToolWindow options (from the "None" option) fixed this, but introduced another problem. When the app first starts the titlebar of the minimized window is visible just above the start menu:

enter image description here

Opening the form and the closing it causes it to hide properly. I've tried lots of variations, but here's essentially how it's working right now...

WindowState is set to Minimized in the Designer. After some initialization in the constructor I have the following lines:

this.Visible = false; this.ShowInTaskbar = false; 

When the NotifyIcon is double-clicked I have the following:

 this.WindowState = FormWindowState.Normal;  this.Visible = true;  this.ShowInTaskbar = true; 

Like I said, I've tried lots of minor variations on this (this.Hide(), etc.). Is there a way to have the NotifyIcon be the primary component such that I can completely start and dispose of the form while leaving the NotifyIcon running? There's got to be a way to start the app with the form minimized without any of the weirdness. Please help me find it!

like image 567
jluce50 Avatar asked Nov 13 '09 17:11

jluce50


People also ask

How do I minimize a program to the system tray?

Press Alt + F1 and that window will minimize to the tray.

How minimize windows form to System Tray in C#?

This can be done by doing the following in your form's Resize event handler: Check whether the form's WindowState property is set to FormWindowState. Minimized. If yes, hide your form, enable the NotifyIcon object, and show the balloon tip that shows some information. Once the WindowState becomes FormWindowState.


1 Answers

The right way to do this is to prevent the form from getting visible in the first place. That requires overriding SetVisibleCore(). Let's assume a context menu for the NotifyIcon with a Show and Exit command. You can implement it like this:

public partial class Form1 : Form {     public Form1() {         InitializeComponent();         notifyIcon1.ContextMenuStrip = contextMenuStrip1;         this.showToolStripMenuItem.Click += showToolStripMenuItem_Click;         this.exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;     }      private bool allowVisible;     // ContextMenu's Show command used     private bool allowClose;       // ContextMenu's Exit command used      protected override void SetVisibleCore(bool value) {         if (!allowVisible) {             value = false;             if (!this.IsHandleCreated) CreateHandle();         }         base.SetVisibleCore(value);     }      protected override void OnFormClosing(FormClosingEventArgs e) {         if (!allowClose) {             this.Hide();             e.Cancel = true;         }         base.OnFormClosing(e);     }      private void showToolStripMenuItem_Click(object sender, EventArgs e) {         allowVisible = true;         Show();     }      private void exitToolStripMenuItem_Click(object sender, EventArgs e) {         allowClose = true;         Application.Exit();     } } 

Note a wrinkle with the Load event, it won't fire until the main form is first shown. So be sure to do initialization in the form's constructor, not the Load event handler.

like image 99
Hans Passant Avatar answered Oct 06 '22 00:10

Hans Passant