Open In App

How to Hide and Show a Console Window in C++?

The task is to hide and Show the console window of a C++ program. The program for the same is given below.

Note: The results of the following program can only be seen when it is executed on a console. 



Example:




// C++ program to hide and show a console window
#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()
{
    countdown();
    HWND window;
    AllocConsole();
    // You Can Find HANDLE of other windows too
    window = FindWindowA("ConsoleWindowClass", NULL);
    ShowWindow(window, 0);
  
    countdown();
    ShowWindow(window, 1);
}

Output:



 

Explanation: The above program counts from 3 to 1 before the Console Window disappears. After the window has disappeared, the ShowWindow helps the program so that the Console Window reappears again after counting from 3 to 1(executing the countdown function).

The execution of the program can be understood by understanding the key functions of the program.

Article Tags :