Washington | 27°C (few clouds)
Hiding and Revealing a Console Window in C++ (Windows)

Control console visibility using the WinAPI

A quick walkthrough showing how C++ programs on Windows can hide their console, keep running, and bring it back when needed.

Ever written a C++ console app that suddenly needs to disappear from the screen, yet keep doing its job? On Windows you can actually make the console window vanish – and later pop back in – with just a couple of WinAPI calls.

At the heart of the trick are two functions: ShowWindow(), which tells Windows whether a window should be shown or hidden, and FindWindowA(), which helps you grab the handle (HWND) of the console you’re interested in. The code snippet below walks you through a simple countdown, hides the window, runs another countdown behind the scenes, and finally shows the console again.

#include <iostream>
#include <windows.h>

using namespace std;

void countdown()
{
    cout << "3" << endl;
    Sleep(1000);
    cout << "2" << endl;
    Sleep(1000);
    cout << "1" << endl;
    Sleep(1000);
    cout << "0" << endl;
}

int main()
{
    // First, a normal countdown so you can see something happening.
    countdown();

    HWND consoleWnd;
    // Ensure a console exists (useful if the program was built as a GUI app).
    AllocConsole();
    // Grab the handle of the console window.
    consoleWnd = FindWindowA("ConsoleWindowClass", NULL);
    // Hide it – the second argument 0 means "hide".
    ShowWindow(consoleWnd, 0);

    // While the console is out of sight, do some work.
    countdown();

    // Bring the console back – 1 means "show".
    ShowWindow(consoleWnd, 1);
    return 0;
}

What’s really happening?

  • The first call to countdown() prints numbers 3 to 0, pausing a second between each – you’ll see the output in the console.
  • AllocConsole() makes sure a console window exists; it’s a harmless call even if one is already present.
  • FindWindowA("ConsoleWindowClass", NULL) fetches the window handle for the current console. (You could also locate other windows by class name or title.)
  • ShowWindow(handle, 0) tells Windows to hide that window. The program keeps running; it’s just invisible.
  • The second countdown() runs while you can’t see the console, demonstrating that the code keeps executing.
  • Finally, ShowWindow(handle, 1) makes the console reappear, displaying the last set of numbers.

Keep in mind this technique relies on the Windows API, so it only works on Windows platforms. If you need a cross‑platform solution, you’d have to look into other libraries or avoid console UI altogether.

That’s all there is to it – a few lines, a tiny bit of Windows magic, and you’ve got full control over whether your console is visible or hidden.

Comments 0
Please login to post a comment. Login
No approved comments yet.

Editorial note: Nishadil may use AI assistance for news drafting and formatting. Readers can report issues from this page, and material corrections are reviewed under our editorial standards.