我正在运行一个C++控制台应用程序,
在一段时间内,
我想要最小化我的应用程序所在的窗口。
例如,我从cmd中启动myApp.exe。然后它在新窗口中启动。
那么有哪些库可以最小化应用程序所在的窗口。
应用程序没有任何GUI。
我假设您的应用程序在Windows上运行(这在不同操作系统之间不可移植)。
首先,您需要使用 GetConsoleWindow() 函数获取控制台窗口的句柄,然后可以使用 ShowWindow() 根据需要隐藏/显示它。别忘了包含windows.h
:
ShowWindow(GetConsoleWindow(), SW_MINIMIZE);
你可以使用SW_HIDE
代替SW_MINIMIZE
来完全隐藏窗口(但它在应用程序刚启动时会闪现可见)。
请注意,如果您控制进程的创建,您可以将其创建为DETACHED_PROCESS
:一个分离的控制台应用程序没有控制台窗口。 CreateProcess()
函数还有其他一些解决方法可能会对您感兴趣(例如,您可以为输出创建一个子进程...)
更新:作为Patrick's answer 的后续,您可以将子系统从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. 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.
start /min myApp.exe
启动它(假设你正在Windows上运行...)。 - Serge Ballesta