I'm learning C++ and I made a new program. I deleted some of my code and now my console window is not hidden. Is there a way to make it hide on startup without them seeing it?
8 Answers
If you're writing a console program and you want to disconnect your program from the console it started with, then call FreeConsole
. Ultimately, you probably won't be satisfied with what that function really does, but that's the literal answer to the question you asked.
If you're writing a program that you never want to have a console in the first place, then configure your project so that it is not a console program. "Consoleness" is a property of the EXE file. The OS reads that setting and decides whether to allocate a console for your program before any of your code ever runs, so you can't control it within the program. Sometimes a non-console program is called a "GUI program," so you might look for a choice between "console" and "GUI" in the configuration options of your development environment. Setting it to GUI doesn't require that you have any user interface at all, though. The setting merely controls whether your program starts with a console.
If you're trying to write a program that can sometimes have a console and sometimes not, then please see an earlier question, Can one executable be both a console and GUI app?

- 1
- 1

- 161,384
- 21
- 275
- 467
-
14If running Mingw, setting GUI mode can be done by adding the "-mwindows" flag. – luiscubal Mar 07 '09 at 23:03
Assuming you're on windows, configure your linker to make a gui-program, not a console program.
- VS: Look in Linker ptions on project properties
- LINK: add /SUBSYSTEM:WINDOWS
- MinGW: -mwindows

- 24,812
- 7
- 82
- 118
-
-
@Youda008: as a linker parameter, `/SUBSYSTEM:WINDOWS` use, you should. ;) – Macke Dec 06 '16 at 10:20
-
and i just found on MinGW it is done with param `-mwindows`, can you add these into the answer? – Youda008 Dec 06 '16 at 10:44
#include <windows.h>
#include <iostream>
using namespace std;
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");
return EXIT_SUCCESS;
}

- 1,610
- 3
- 22
- 40
-
4Just a small improvement, you can use GetConsoleWindow() instead of FindWindowA(). – davidnr Jul 27 '16 at 10:19
-
-
@athos, Please read: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx, I am not using windows anymore but AFAIK make another function: void ShowWin() { HWND Stealth; AllocConsole(); Stealth = FindWindowA("ConsoleWindowClass", NULL); ShowWindow(Stealth,1); //for showing closed window } ; And in program call ShowWin(); wherever you want to display closed window. I hope this will work. – udit043 Oct 17 '16 at 04:15
I used to use ShowWindow (GetConsoleWindow(), SW_HIDE);
in such case, however if you no need console, so don't create console app project.

- 8,826
- 3
- 68
- 92
-
2it will not hide the window without SW_MINIMIZE HWND hWnd = GetConsoleWindow(); ShowWindow( hWnd, SW_MINIMIZE ); ShowWindow( hWnd, SW_HIDE ); – Ankur Aug 12 '16 at 06:45
-
@Ankur, did you try and `SW_HIDE` didn't work? Because worked for me perfectly. – ST3 Aug 12 '16 at 08:44
As already said, starting the application with console or not is set in the exe. Using gnu compiler the option is -mwindows for no console, for example
g++ -mwindows winapp.c
it seems that the method
#define _WIN32_WINNT 0x0500
#include <wincon.h>
....
case WM_CREATE :
ShowWindow (GetConsoleWindow(), SW_HIDE);
close all parent consoles as well, so if you launch the winapp.exe from a command line console this will be closed as well!

- 1,551
- 19
- 14
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);
}

- 922
- 9
- 16
-
-
-
Hmm, [remove new user restrictions](http://stackoverflow.com/help/privileges/new-user) is supposed to happen at 10 rep, so I think you have enough now? – SamB Nov 05 '16 at 15:00
-
You can create your window minimized. Or paint it outside the visible screen.
But you could also have messed with the window creation flags. If you really messed things up. It is often better to start a new window. (Or restore from a previous version, or the backup).

- 52,876
- 38
- 145
- 202
You can try this
#include <windows.h>
int main() {
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
MessageBox(NULL,"The console Window has been hidden.","Console Hidden",MB_ICONINFORMATION);
return 0;
}
It is part of the win32 API, which you can include using "#include "
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
The first argument tells the program to get the console window that is currently running the program. The second argument passes down the instruction for what you want to do with the window. "SW_HIDE" hides the window, while "SW_SHOW" shows the window.

- 127
- 1
- 6