Open In App

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

Last Updated : 27 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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.

  • #include<windows.h> – The windows.h header in C++ programming languages are specifically designed for windows and contain a very large number of windows specific functions.
  • AllocConsole()- AllocConsole initializes standard input, standard output, and standard error handles for the new console. 
  • ShowWindow()- Sets the specified window’s show state.
  • FindWindowA()– Takes string parameters and checks whose class name and window name match the specified strings

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads