Question

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

Was it helpful?

Solution

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();
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top