[英]How to open Console during runtime [C++/Visual Studio]
Currently, I'm developing an SDL application with Visual Studio. 当前,我正在使用Visual Studio开发SDL应用程序。 Right now, if I want to have the console open in order to view output, I must enter the "properties of the project > Linker > System > SubSystem > Console" in order to enable it. 现在,如果要打开控制台以查看输出,则必须输入“项目属性>链接器>系统>子系统>控制台”以启用它。 When I'm ready to export however, I disable it. 但是,当我准备导出时,将其禁用。
However, I was wondering if there's anyway to open (or even close) the console window during runtime. 但是,我想知道在运行时是否可以打开(或关闭)控制台窗口。 Specifically, I wish to be able to press a key while the application is running in order open the console and view output. 具体来说,我希望能够在应用程序运行时按一个键,以便打开控制台并查看输出。
I've tried using AllocConsole
from windows.h
, but while it does open a console window, it does not appear to display the output that normally appears whenever I manually set the application to use a Console window. 我尝试从windows.h
使用AllocConsole
,但是虽然它确实打开了控制台窗口,但是当我手动将应用程序设置为使用控制台窗口时,它似乎没有显示通常显示的输出。
(Alternatively, I've been thinking I could open a second SDL window and display all output there, but I have no idea how to stream all output from the application to itself. Probably not the most convenient solution, but would work also.) (或者,我一直在想我可以打开第二个SDL窗口并在其中显示所有输出,但是我不知道如何将所有输出从应用程序流式传输到自身。可能不是最方便的解决方案,但也可以使用。)
You can use 您可以使用
if (::GetConsoleWindow() == NULL)
{
if (::AllocConsole())
{
(void)freopen("CONIN$", "r", stdin);
(void)freopen("CONOUT$", "w", stdout);
(void)freopen("CONOUT$", "w", stderr);
SetFocus(::GetConsoleWindow());
}
}
Right before using printf
就在使用printf
之前
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.