Open In App

How to Hide the Console Window of a C Program?

The task is to hide 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. 






// C program to hide the console window
#include <stdio.h>
#include <windows.h>
 
int main()
{
    HWND myWindow = GetConsoleWindow();
    printf("Hiding the window\n");
    Sleep(3000);
    ShowWindow(myWindow, SW_HIDE);
 
    return 0;
}

Output:

 

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



Since the console window is just hidden, the execution of the program continues in the background of the device. 

Example:




// C program to hide the console window
#include <stdio.h>
#include <windows.h>
 
int main()
{
    HWND myWindow = GetConsoleWindow();
    printf("Hiding the window\n");
 
    Sleep(3000);
    ShowWindow(myWindow, SW_HIDE);
 
    Sleep(3000);
    ShowWindow(myWindow, SW_SHOW);
 
    printf("The window is displayed again!");
 
    return 0;
}

Output:

 

Explanation: The above program initially waits for the specified time and hides the console window and then again displays the window after the specified time. SW_SHOW is used to activate the window and display it. 


Article Tags :