Click here to Skip to main content
15,868,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to minimize a console application to system tray and restore the console when clicking the Icon in the tray
Posted
Updated 8-Dec-17 3:07am

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.
 
Share this answer
 
Comments
Geo Jackson 26-Mar-12 6:52am    
Is it possible to Convert a Console Application to Windows Application
#realJSOP 27-Mar-12 8:11am    
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.
Geo Jackson 29-Mar-12 3:42am    
I tried that , But my windows is not responding when it waits for accepting a connection.
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);

		[DllImport("user32.dll")]
		static extern IntPtr GetShellWindow();

		[DllImport("user32.dll")]
		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(RestoreMenu);
			menu.MenuItems.Add(HideMenu);
			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
			Task.Factory.StartNew(Run);

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

			WinDesktop = GetDesktopWindow();

                        //Hide the Window
			ResizeWindow(false);

                        ///This is required for triggering WinForms activity in Console app
			Application.Run();
						

		}

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

			while (true)
			{
				System.Threading.Thread.Sleep(1000);
			}
		}


		private static void CleanExit(object sender, EventArgs e)
		{
			notifyIcon.Visible = false;
			Application.Exit();
			Environment.Exit(1);
		}
		

		static void Minimize_Click(object sender, EventArgs e)
		{			
			ResizeWindow(false);
		}


		static void Maximize_Click(object sender, EventArgs e)
		{
			ResizeWindow();
		}

		static void ResizeWindow(bool Restore = true)
		{
			if (Restore)
			{
				RestoreMenu.Enabled = false;
				HideMenu.Enabled = true;				
				SetParent(processHandle, WinDesktop);
			}
			else
			{
				RestoreMenu.Enabled = true;
				HideMenu.Enabled = false;
				SetParent(processHandle, WinShell);
			}
		}
	}
 
Share this answer
 
v2
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900