Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC++
How to minimize a console application to system tray and restore the console when clicking the Icon in the tray
Posted 26-Mar-12 0:43am
Rate this: bad
good
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.
  Permalink  
Comments
Member 8080254 at 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 at 29-Mar-12 3:42am
   
I tried that , But my windows is not responding when it waits for accepting a connection.
Rate this: bad
good
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);
 
		[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);
			}
		}
	}
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 18 Sep 2014
Copyright © CodeProject, 1999-2014
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