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)?

Dani AI

Generated

— 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):

  • Add an icon resource to the executable (resource script or project settings).
  • At runtime call GetConsoleWindow() to get the window handle.
  • Load the icon from your module (LoadImage or LoadIcon).
  • Send 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:

  • Use GetConsoleWindow and WM_SETICON as documented (GetConsoleWindow, WM_SETICON). Loading/destroying icons depends on the API/flags you use; see LoadImage/DestroyIcon docs (LoadImage).
  • On many Unix/Linux terminals the emulator owns the window and controls the icon; a pure CLI program cannot reliably change the window icon across terminals. On macOS the icon comes from the app bundle. If consistent UI presence is required, wrap the CLI in a minimal GUI stub or package it as an application bundle.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.