<p>What is the proper way to minimize a WinForms app to the system tray? </p>

<p>Note: minimize to <strong><em>system tray</em></strong>; on the right side of the taskbar by the clock. I'm not asking about minimizing to taskbar, which is what happens when you hit the "minus" button on the window.</p>

<p>I've seen hackish solutions like, "minimize, set ShowInTaskbar = false, then show your NotifyIcon."</p>

<p>Solutions like that are hackish because the app doesn't appear to minimize to the tray like other apps, the code has to detect when to set ShowInTaskbar = true, among other issues.</p>

<p>What's the proper way to do this?</p>

[{'answer_id': 46927, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 3, 'selected': False, 'text': '<pre><code>this.WindowState = FormWindowState.Minimized  \n</code></pre>\n\n<p>That is the built in way to do it and it looks fine to me most of the time. The only time is has some weirdness to it is if you call it on startup which has some weirdness sometimes which is why most people will also set the ShowInTaskbar = false and hide the form too.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.windowstate.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.windows.forms.form.windowstate.aspx</a></p>\n'}, {'answer_id': 46970, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 2, 'selected': False, 'text': '<p>Update: Looks like I posted too soon.\nI was also using the below hack for a tool of mine. Waiting for the right solution for this..........</p>\n\n<p>You can use Windows.Forms.NotifyIcon for this. <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx</a></p>\n\n<p>Add NotifyIcon to the form, set some properties and you are done.</p>\n\n<pre><code>        this.ShowIcon = false;//for the main form\n        this.ShowInTaskbar = false;//for the main form\n        this.notifyIcon1.Visible = true;//for notify icon\n        this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));//set an icon for notifyicon\n\n    private void Form1_Shown(object sender, EventArgs e)\n    {\n        this.Hide();\n    }\n</code></pre>\n'}, {'answer_id': 47743, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 5, 'selected': True, 'text': '<p>There is actually no managed way to do that form of animation to the tray in native winforms, however you can P/Invoke shell32.dll to do it:</p>\n\n<p>Some good info here (In the comments not the post):</p>\n\n<p><a href="http://blogs.msdn.com/jfoscoding/archive/2005/10/20/483300.aspx" rel="noreferrer">http://blogs.msdn.com/jfoscoding/archive/2005/10/20/483300.aspx</a></p>\n\n<p>And here it is in C++:</p>\n\n<p><a href="http://www.codeproject.com/KB/shell/minimizetotray.aspx" rel="noreferrer">http://www.codeproject.com/KB/shell/minimizetotray.aspx</a></p>\n\n<p>You can use that to figure out what stuff to Pinvoke for your C# version.</p>\n'}, {'answer_id': 398046, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>This code is tested and supports many options.</p>\n\n<p>More here: <a href="http://code.msdn.microsoft.com/TheNotifyIconExample" rel="nofollow noreferrer">http://code.msdn.microsoft.com/TheNotifyIconExample</a></p>\n'}, {'answer_id': 520291, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p>In the constructor of the Form:</p>\n\n<pre><code>this.Resize += new EventHandler(MainForm_Minimize);\n</code></pre>\n\n<p>Then use this Event Handler method:</p>\n\n<pre><code>    private void MainForm_Minimize(object sender, EventArgs e)\n    {\n        if(this.WindowState == FormWindowState.Minimized)\n            Hide();\n    }\n</code></pre>\n'}, {'answer_id': 533002, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Similar as above...</p>\n\n<p>I have a resize event handler that is triggered whenever the window gets resized (Maximized, minimized, etc.)...</p>\n\n<pre><code>    public form1()\n    {\n       Initialize Component();\n\n       this.Resize += new EventHanlder(form1_Resize);\n    } \n\n\n    private void form1_Resize(object sender, EventArgs e)\n    {\n       if (this.WindowState == FormWindowState.Minimized &amp;&amp; minimizeToTrayToolStripMenuItem.Checked == true)\n       {\n             NotificationIcon1.Visible = true;\n             NotificationIcon1.BalloonTipText = "Tool Tip Text"\n             NotificationIcon1.ShowBalloonTip(2);  //show balloon tip for 2 seconds\n             NotificationIcon1.Text = "Balloon Text that shows when minimized to tray for 2 seconds";\n             this.WindowState = FormWindowState.Minimized;\n             this.ShowInTaskbar = false;\n       }\n    }\n</code></pre>\n\n<p>This allows the user to select whether or not they want to minimize to tray via a menubar.  They can go to Windows -> and click on Minimize to Tray.  If this is checked,\nand the window is being resized to Minimized, then it will minimize to the tray.  Works flawlessly for me.</p>\n'}, {'answer_id': 3394963, 'author': 'James', 'author_id': 409463, 'author_profile': 'https://Stackoverflow.com/users/409463', 'pm_score': -1, 'selected': False, 'text': '<p>If you are having problems getting this to work, check that you have</p>\n\n<pre><code>this.Resize += new System.EventHandler(this.Form1_Resize);\n</code></pre>\n\n<p>in the fom1.designer.cs</p>\n'}, {'answer_id': 3510214, 'author': 'Jeyavel', 'author_id': 212809, 'author_profile': 'https://Stackoverflow.com/users/212809', 'pm_score': 3, 'selected': False, 'text': '<p>It will helps:   </p>\n\n<pre><code>public partial class Form1 : Form\n{\n    public static bool Close = false;\n    Icon[] images;\n    int offset = 0;\n\n    public Form1()\n    {\n        InitializeComponent();\n\n        notifyIcon1.BalloonTipText = "My application still working...";\n        notifyIcon1.BalloonTipTitle = "My Sample Application";\n        notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; \n    }\n\n    private void Form1_Resize(object sender, EventArgs e)\n    {\n        if (FormWindowState.Minimized == WindowState)\n        {\n            this.Hide();\n            notifyIcon1.ShowBalloonTip(500);\n            //WindowState = FormWindowState.Minimized;\n        }\n    }\n\n    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)\n    {\n        this.Show();\n        notifyIcon1.ShowBalloonTip(1000);\n        WindowState = FormWindowState.Normal;\n    }\n\n    private void maximizeToolStripMenuItem_Click(object sender, EventArgs e)\n    {\n        this.Show();\n        WindowState = FormWindowState.Normal;\n    }\n\n    private void closeToolStripMenuItem_Click(object sender, EventArgs e)\n    {\n        Close = true;\n        this.Close();  \n    }\n\n    // Handle Closing of the Form.\n    protected override void OnClosing(CancelEventArgs e)\n    {\n        if (Close)\n        {\n            e.Cancel = false;\n        }\n        else\n        {\n            WindowState = FormWindowState.Minimized;\n            e.Cancel = true;\n        }\n    }\n\n    private void Form1_Load(object sender, EventArgs e)\n    {\n        // Load the basic set of eight icons.\n        images = new Icon[5];\n        images[0] = new Icon("C:\\\\icon1.ico");\n        images[1] = new Icon("C:\\\\icon2.ico");\n        images[2] = new Icon("C:\\\\icon3.ico");\n        images[3] = new Icon("C:\\\\icon4.ico");\n        images[4] = new Icon("C:\\\\icon5.ico");\n    }\n\n    private void timer1_Tick(object sender, EventArgs e)\n    {\n        // Change the icon.\n        // This event handler fires once every second (1000 ms).\n        if (offset &lt; 5)\n        {\n            notifyIcon1.Icon = images[offset];\n            offset++;\n        }\n        else\n        {\n            offset = 0;\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 63037966, 'author': 'Brian_The_Programmer', 'author_id': 13975911, 'author_profile': 'https://Stackoverflow.com/users/13975911', 'pm_score': 0, 'selected': False, 'text': "<p>I have programmed a quick solution for you guys, this might not be the most efficient way (I'm not sure), but it definitely works !</p>\n<p>The below solution successfully minimizes your Winforms application to the system Tray, and by using the notify Icon you create a small icon in the system tray with the aforementioned menu items. than we use the in-built libraries to fetch the Window (GUI in this case), and using the appropriate methods with Booleans we then minimize the window to the tray.</p>\n<pre><code>private void TransformWindow()\n        {\n            ContextMenu Menu = new ContextMenu();\n\n            Menu.MenuItems.Add(RestoreMenu);\n            Menu.MenuItems.Add(HideMenu);\n            Menu.MenuItems.Add(new MenuItem(&quot;Exit&quot;, new EventHandler(CleanExit)));\n\n            notifyIcon = new NotifyIcon()\n            {\n                Icon = Properties.Resources.Microsft_Icon,\n                Text = &quot;Folder Watcher&quot;,\n                Visible = true,\n                ContextMenu = Menu\n            };\n            processHandle = Process.GetCurrentProcess().MainWindowHandle;\n            ResizeWindow(false);\n\n        }\n        #region Getting Libraries\n        [DllImport(&quot;user32.dll&quot;)]\n        public static extern IntPtr GetShellWindow();\n        [DllImport(&quot;user32.dll&quot;)]\n        public static extern IntPtr GetDesktopWindow();\n        public const Int32 SwMINIMIZE = 0;\n        public const Int32 SwMaximize = 9;\n\n        [DllImport(&quot;Kernel32.dll&quot;, CallingConvention = CallingConvention.StdCall, SetLastError = true)]\n        public static extern IntPtr GetConsoleWindow();\n\n        [DllImport(&quot;User32.dll&quot;, CallingConvention = CallingConvention.StdCall, SetLastError = true)]\n        [return: MarshalAs(UnmanagedType.Bool)]\n        public static extern bool ShowWindow([In] IntPtr hWnd, [In] Int32 nCmdShow);\n        public static void MinimizeConsoleWindow()\n        {\n            IntPtr hWndConsole = processHandle;\n            ShowWindow(hWndConsole, SwMINIMIZE);\n\n        }\n        public static void MaximizeConsoleWindow()\n        {\n            IntPtr hWndConsole = processHandle;\n            ShowWindow(hWndConsole, SwMaximize);\n\n        }\n        [DllImport(&quot;user32.dll&quot;, SetLastError = true)]\n        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);\n        public static void ResizeWindow(bool show)\n        {\n            RestoreMenu.Enabled = !show;\n            HideMenu.Enabled = show;\n            SetParent(processHandle, WinDesktop);\n\n            if (show)\n                MaximizeConsoleWindow();\n            else\n                MinimizeConsoleWindow();\n\n        }\n        public static void MinimizeClick(object sender, EventArgs e) =&gt; ResizeWindow(false);\n        public static void MaximizeClick(object sender, EventArgs e) =&gt; ResizeWindow(true);\n        public static IntPtr processHandle;\n        public static IntPtr WinShell;\n        public static IntPtr WinDesktop;\n        public static NotifyIcon notifyIcon;\n        public static MenuItem HideMenu = new MenuItem(&quot;Hide&quot;, new EventHandler(MinimizeClick));\n        public static MenuItem RestoreMenu = new MenuItem(&quot;Restore&quot;, new EventHandler(MaximizeClick));\n        public void CleanExit(object sender, EventArgs e)\n        {\n            notifyIcon.Visible = false;\n            this.Close();\n        }\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/46918', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/536/']