Hello DaniWeb,
I am making a command line program, which does not use a GUI. However, I am wondering if there is any code that I can use in order to change the program's icon (the one that appears on the top-left of the window, or in the Taskbar)?
Hello DaniWeb,
I am making a command line program, which does not use a GUI. However, I am wondering if there is any code that I can use in order to change the program's icon (the one that appears on the top-left of the window, or in the Taskbar)?
— yes, you can change the icon that appears for a console window on Windows. The console window is a normal Win32 window: get its HWND with GetConsoleWindow, load an icon resource from your EXE, then tell the window to use it by sending WM_SETICON. pointed to related material, but the concise approach below should let a plain command-line program set its own titlebar/taskbar icon.
Typical steps (Windows):
GetConsoleWindow() to get the window handle.LoadImage or LoadIcon).WM_SETICON for both ICON_SMALL and ICON_BIG (optionally update the class icon with SetClassLongPtr).Simple example (C):
#include <windows.h>
int main(void)
{
HWND hwnd = GetConsoleWindow();
if (hwnd) {
HICON hIcon = (HICON)LoadImage(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDI_MYICON),
IMAGE_ICON, 0, 0, LR_DEFAULTSIZE);
if (hIcon) {
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
}
}
/* ... */
return 0;
} Resource script line:
IDI_MYICON ICON "myicon.ico" Notes and cautions:
GetConsoleWindow and WM_SETICON as documented (GetConsoleWindow, WM_SETICON). Loading/destroying icons depends on the API/flags you use; see LoadImage/DestroyIcon docs (LoadImage).We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.