Click here to Skip to main content
13,456,167 members
Rate this:
Please Sign up or sign in to vote.
See more:
How to minimize a console application to system tray and restore the console when clicking the Icon in the tray
Posted 25-Mar-12 23:43pm
Updated 8-Dec-17 3:07am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can't minimize the console window to the system tray. That's a GUI function, and the console app isn't a GUI app.

You could create a WinForms app and simulate the appearance of a console window, and then minimize it to the system tray.
Member 8080254 26-Mar-12 6:52am
Is it possible to Convert a Console Application to Windows Application
It's probably best to start from scratch with a new WinForms app, and move the code from the console app to the WinForms app by hand.
Member 8080254 29-Mar-12 3:42am
I tried that , But my windows is not responding when it waits for accepting a connection.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

This thread is old, but still thought some one might want the solution with out changing their project types. And this solution is specific to C#, so I think there might be similar approach for VC++. Doesn't have a hands on with VC++

You can achieve this using few simple steps. This piece of code minimizes the Console Window, hides from Task Bar. You can hide, restore the app from the Notification Tray.

class Program

		[DllImport("user32.dll", SetLastError = true)]
		static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

		static extern IntPtr GetShellWindow();

		static extern IntPtr GetDesktopWindow(); 

		static NotifyIcon notifyIcon;
		static IntPtr processHandle;
		static IntPtr WinShell;
		static IntPtr WinDesktop;
		static MenuItem HideMenu;
		static MenuItem RestoreMenu;

		static void Main(string[] args)
			notifyIcon = new NotifyIcon();			
			notifyIcon.Icon = new Icon("TestICON.ico");
			notifyIcon.Text = "Monitor";			
			notifyIcon.Visible = true;

			ContextMenu menu = new ContextMenu();
			HideMenu = new MenuItem("Hide", new EventHandler(Minimize_Click));
			RestoreMenu = new MenuItem("Restore", new EventHandler(Maximize_Click));

			menu.MenuItems.Add(new MenuItem("Exit", new EventHandler(CleanExit)));

			notifyIcon.ContextMenu = menu;

                        //You need to spin off your actual work in a different thread so that the Notify Icon works correctly

			processHandle = Process.GetCurrentProcess().MainWindowHandle;
			WinShell = GetShellWindow();

			WinDesktop = GetDesktopWindow();

                        //Hide the Window

                        ///This is required for triggering WinForms activity in Console app


		static void Run()
			Console.WriteLine("Listening to messages");

			while (true)

		private static void CleanExit(object sender, EventArgs e)
			notifyIcon.Visible = false;

		static void Minimize_Click(object sender, EventArgs e)

		static void Maximize_Click(object sender, EventArgs e)

		static void ResizeWindow(bool Restore = true)
			if (Restore)
				RestoreMenu.Enabled = false;
				HideMenu.Enabled = true;				
				SetParent(processHandle, WinDesktop);
				RestoreMenu.Enabled = true;
				HideMenu.Enabled = false;
				SetParent(processHandle, WinShell);
claudiocas 5-May-15 3:52am
The code is great but It doesn't work on Windows Server Standard...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180322.1 | Last Updated 8 Dec 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100