简体   繁体   English

如何最小化控制台窗口?

[英]How to minimize console window?

I am running a C++ console application, for some period of time, 我正在运行一个C ++控制台应用程序,在一段时间内,
I want to minimize the window in which my application is running. 我想最小化我的应用程序运行的窗口。
for eg. 例如。 I launch myApp.exe from cmd. 我从cmd启动myApp.exe。 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 应用程序没有任何GUI

I suppose your application is running on Windows (this is not portable across different operating systems). 我想你的应用程序是在Windows上运行的(这不能在不同的操作系统上运行)。

You have first to get handle of your Console window with GetConsoleWindow() function, then you can use ShowWindow() to hide/show it as required. 您首先使用GetConsoleWindow()函数处理控制台窗口,然后您可以使用ShowWindow()根据需要隐藏/显示它。 Ddon't forget to include windows.h : 别忘了包含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). 您可以使用SW_HIDE完全隐藏它(而不是SW_MINIMIZE (但是当应用程序刚启动时它会闪烁一次)。

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. 请注意,如果您可以控制流程创建,则可以将其创建为DETACHED_PROCESS :分离的控制台应用程序没有控制台窗口。 CreateProcess() function has also other workarounds you may be interested in (for example you may create a child process for outputting...) CreateProcess()函数还有其他您可能感兴趣的变通方法(例如,您可以创建一个子进程来输出...)

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 : 更新 :作为Patrick的回答的后续内容,您可以将子系统从Console更改为Windows,然后,如果您需要写入控制台,请使用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 . 控制台Windows However, you then need to add a WinMain() entry point, such as: 但是,您需要添加WinMain()入口点,例如:

  int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  { int argc = 0;
    LPWSTR* argv = CommandLineToArgvW(GetCommandLine(), &argc);    
    return Main(argc, argv);
  }

assuming unicode. 假设unicode。 To avoid confusion, I rename the console's wmain() function to something like Main(), as above. 为避免混淆,我将控制台的wmain()函数重命名为Main(),如上所述。 Of course printf no longer has a console to write to. 当然printf不再有要写入的控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM