I am running a C++ console application,
for some period of time,
I want to minimize the window in which my application is running.
for eg. I launch myApp.exe from cm
I suppose your application is running on Windows (this is not portable across different operating systems).
You have first to get handle of your Console window with GetConsoleWindow() function, then you can use ShowWindow() to hide/show it as required. Ddon't forget to include windows.h
:
ShowWindow(GetConsoleWindow(), SW_MINIMIZE);
Instead of SW_MINIMIZE
you can use SW_HIDE
to completely hide it (but it'll flash visible once when application just started).
Note that if you have control over process creation you can create it as DETACHED_PROCESS
: a detached console application does not have a console window. CreateProcess()
function has also other workarounds you may be interested in (for example you may create a child process for outputting...)
UPDATE: as follow-up of Patrick's answer you may change the subsystem from Console to Windows and then, if you require to write to console, create a new one using AllocConsole:
if (AllocConsole()) {
printf("Now I can print to console...\n");
FreeConsole();
}
Another option is to change
Properties... | Configuration Properties | Linker | System | Subsystem
from Console to Windows. However, you then need to add a WinMain() entry point, such as:
int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{ int argc = 0;
LPWSTR* argv = CommandLineToArgvW(GetCommandLine(), &argc);
return Main(argc, argv);
}
assuming unicode. To avoid confusion, I rename the console's wmain() function to something like Main(), as above. Of course printf no longer has a console to write to.