Open In App

How to Hide a Console Window On Startup in C++?

Last Updated : 02 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The console is an OS window through which users interact with the operating system either by entering text input through the computer keyboard or by reading text output from the computer terminal.

The term console is derived from the French word ‘consolide’, which is derived from the Latin word ‘consolidare’, meaning ‘to strengthen’.

Methods to Hide A Console Window on Startup in C++

1. Using the ShowWindow() function and IsConsoleVisible() function

ShowWindow shows or hides a window. The function can minimize, maximize, or restore a given window. If the function returns 0, it means the window has been hidden before the call and if it returns a non-zero value it had been visible.

Syntax:

BOOL ShowWindow(
     [in] HWND hWnd,
     [in] int  nCmdShow
);

Value: SW_HIDE  0                    

Meaning: Hides the window and activates another window.

C++




#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();
    window = FindWindowA("ConsoleWindowClass", NULL);
    ShowWindow(window, 0);
      
    countdown();
    ShowWindow(window, 1);
}


Output:

How To Hide A Console Window On Startup In C++

2

Example:

C++




#include <Windows.h>
  
int main(){
    
    void HideConsole()
    {
        ::ShowWindow(::GetConsoleWindow(), SW_HIDE);
    }
  
    void ShowConsole()
    {
        ::ShowWindow(::GetConsoleWindow(), SW_SHOW);
    }
  
    bool IsConsoleVisible()
    {
        return ::IsWindowVisible(::GetConsoleWindow()) != FALSE;
    }
}


Explanation: 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 and C++ programming languages are specifically designed for windows and contain a very large number of windows specific functions.
  • ShowWindow. GetConsoleWindow is used to retrieve the window handle used by the console.
  • IsWindowVisible is used to check if a window is visible or not.
  • ShowWindow() – The ShowWindow() function controls how a window will be displayed on the device.  SW_HIDE is used to hide the window.
  • GetConsoleWindow() – The GetConsoleWindow() is a function that takes no parameters and returns a handle to the window used by the console associated with the calling process or NULL if there is no such associated console.
     

Note: SW_MINIMIZE instead of SW_HIDE to have the console hidden but still visible in the taskbar.

For Windows systems:

C++




#include <iostream>
#include <windows.h>
using namespace std;
  
int main(){
    ShowWindow(GetConsoleWindow(), SW_HIDE);
}


Note: This will return a windows handle (HWND) to ShowWindow() which will hide the console window.

2. Using FreeConsole() Function

FreeConsole() function can be used for detaching a process from its console. If two or more processes share the same console, the console is not destroyed, but the process that is called FreeConsole cannot refer to it. A console is closed when the last process attached to it terminates or calls FreeConsole() function.

Example:

C++




#include <iostream>
using namespace std;
  
int main() {
  
    BOOL WINAPI FreeConsole(VOID);
}


Apart from the above methods, we can write these lines in our program to hide the console window on startup. 

Example:

C++




#include <iostream>
using namespace std;
  
int main() {
  
    myConsole = GetConsoleWindow();
    ShowWindow(myConsole,0);
}




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads