Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to minimize console window?

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 cmd. Then its launched in new window.
So what are libraries which can minimize the window in which application is running.
Application doesnt have any GUI

like image 860
Anurag Daware Avatar asked May 07 '14 10:05

Anurag Daware


People also ask

How do I close a window in console?

To close or exit the Windows command line window, also referred to as command or cmd mode or DOS mode, type exit and press Enter . The exit command can also be placed in a batch file. Alternatively, if the window is not fullscreen, you can click the X close button in the top-right corner of the window.


1 Answers

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();
}
like image 86
Adriano Repetti Avatar answered Sep 16 '22 18:09

Adriano Repetti