C ++ Windows:如何关闭控制台窗口? [英] C++ Windows: How to close a console window?

查看:117
本文介绍了C ++ Windows:如何关闭控制台窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了Win32主窗口外,我还有一个使用AllocConsole()打开的控制台窗口.控制台窗口将在主窗口之前打开.

I have a console window opened using AllocConsole(), besides the main Win32 window. The console window opens before the main one.

当我关闭主窗口并且程序从主函数返回时,控制台保持打开状态(过程也是如此).正如调试器在暂停Visual Studio 2012中的程序时所显示的那样,它实际上卡在了ntdll中.

When I close the main window and the program returns from main function, the console remains open(and so does the process). It actually gets stuck somewhere in ntdll, as the debugger shows on pausing the program from Visual Studio 2012.

通过单击X按钮将其关闭将退出该过程,但是使用FreeConsole()将其关闭则不会,该过程将保持无窗口运行.CloseWindow(GetConsoleWindow())不会将其关闭,而是将其最小化(!?!).

Closing it by clicking on the X button exits the process, but closing it with FreeConsole() doesn't, the process remains working windowless. CloseWindow(GetConsoleWindow()) doesn't close it, it minimizes it (!?!). PostMessage(GetConsoleWindow(),WM_QUIT,0,0) closes the window but the process still remains working(visually this is the same as FreeConsole().

有时程序会以正确的方式退出,但是每次单击X按钮都可以.

Occasionally the program exits in the right way, but clicking on the X button works every time.

我如何做与单击X按钮相同的事情?

How do I do the same thing that clicking on the X button does?

推荐答案

使用 PostMessage(wnd,WM_CLOSE,0,0)关闭控制台窗口,但是问题可能出在您的其他地方程序,即使它可以作为修补程序使用.从 main() WinMain()返回时,控制台窗口应自动关闭/消失.

Use PostMessage(wnd, WM_CLOSE, 0, 0) to close the console window, but the problem is probably somewhere else in your program even if this works as a hotfix. The console window should close/disappear automatically when you return from your main() or WinMain().

这篇关于C ++ Windows:如何关闭控制台窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆