Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a console application in Windows that would minimize to the system tray?

I have a written a Visual C++ console application (i.e. subsystem:console) that prints useful diagnositic messages to the console.

However, I would like to keep the application minimized most of the time, and instead of minimizing to the taskbar, appear as a nice icon on the system tray. I would also like to restore the console when the system tray icon is clicked.

How should I change my program to do this?

like image 975
sep Avatar asked Dec 01 '22 08:12

sep


2 Answers

This is going to be an ugly hack.

First, you have to retrieve the hWnd / hInstance of you console application. Right now, I can only come up with one way:

  • Create a Guid with CoCreateGuid()
  • Convert it to a string
  • Set the title of the console window to this guid with SetConsoleTitle()
  • Find the hWnd of the your window with the Guid as the tile with FindWindow()
  • And you can do it from the usual way from this point. See http://www.gidforums.com/t-9218.html for more info.
  • Don't forget the rename your console window to the original title once you're done.

As you can see, even though this is possible to do, it's a horrible and painful solution. Please don't do it. Please do not minimize console applications to the system tray. It is not something you are supposed to be able to do in the Windows API.

like image 111
Tamas Czinege Avatar answered Dec 03 '22 22:12

Tamas Czinege


You might want to write a separate gui to function as a log reader. You will then find it much easier to make this minimize to the tray. It would also let you do some other stuff you might find useful, such as changing which level of logging messages are visible on the fly.

like image 42
Colin Pickard Avatar answered Dec 03 '22 22:12

Colin Pickard