C++ How do I hide a console window on startup? C++ How do I hide a console window on startup? windows windows

C++ How do I hide a console window on startup?


To literally hide/show the console window on demand, you could use the following functions:It's possible to hide/show the console by using ShowWindow. GetConsoleWindow retrieves the window handle used by the console.IsWindowVisible can be used to checked if a window (in that case the console) is visible or not.

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


Hiding a console window at startup is not really possible in your code because the executable is run by the operating system with specific settings. That's why the console window is displayed for a very short time at startup when you use for example FreeConsole();To really hide the window at startup, you have to add a special option to you compiler. If you use gcc on Windows (MinGW) you can just add -mwindows as compiler option in your makefile and there will be absolutely no window or "flash".I don't know about VisualStudio or whatever you use at the moment, but changing the way your IDE compiles you code is the way to go instead of coding workarounds in C++.

In my view, this approach is better than using WinMain because it works reliably and you don't make your C++ Code platform dependent.


#include <windows.h>#include <iostream.h>void Stealth(){ HWND Stealth; AllocConsole(); Stealth = FindWindowA("ConsoleWindowClass", NULL); ShowWindow(Stealth,0);}int main(){  cout<<"this sentence is visible\n";  Stealth(); //to hide console window  cout<<"this sentence is not visible\n";  system("PAUSE"); //here you can call any process silently like system("start chrome.exe") , so google chrome will open and will surprise user..  return EXIT_SUCCESS;}