How to minimize console window?

后端 未结 2 713
名媛妹妹
名媛妹妹 2021-01-18 14:41

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

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 14:52

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

提交回复
热议问题