Set window icon in Windows console application

Console application is a special Windows interface that doesn't need any GUI window since show only a console prompt as input. Each MS-DOS executable or batch file is automatically executed using this special mode. However is possible to develop a pure Windows application using this modality.

Since console application code doesn't create a own window only a small subset of APIs can be used (basically all the API that doesn't require a window handle as main param). In place of standard APIs there are a small special set of APIs dedicated to manage console mode only. However the console output is showed inside a window than a connected handle will exist. Usually Windows use the first icon resource available inside the executable file as main program icon. It show the icon in the caption bar of the application window. Is not the case of a console application where the "default" icon is showed and no dedicated API seem exist to make the assignment. However is possible to use a simply workaround for assign a custom icon to be showed in the console caption bar. Basically is only necessary to get the window handle and assign the standard and small icon by using the default API calls as showed below:

HWND hWnd = GetConsoleWindow();

SendMessage(hWnd, WM_SETICON,ICON_SMALL, [16x16 icon resource handle]);
SendMessage(hWnd, WM_SETICON,ICON_BIG, [32x32 icon resource handle]);

Quite easy, isn't it? ^_^


Popular posts from this blog

Android: adb push and read-only file system error

Access GPIO from Linux user space