Windows GUI +控制台输出,Linux风格 [英] Windows GUI + Console Output, Linux-style

查看:130
本文介绍了Windows GUI +控制台输出,Linux风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GUI应用程序,我正在为Linux和Windows开发跨平台。在Linux上,一切工作顺利。但是,我在Windows上遇到了困难。我想在Windows,Linux风格的GUI应用程序上将某些消息记录到控制台。

I have a GUI application, which I am developing cross-platform for Linux and Windows. On Linux, everything works smoothly. However, I've run into a hitch on Windows. I would like to be able to log certain messages to the console with a GUI app on Windows, Linux-style.

Linux风格的意思是,如果程序从控制台打开,输出将转到控制台,但如果程序打开,例如,通过开始菜单,用户将永远不会看到控制台输出。

What I mean by Linux-style is, if the program is opened from a console, the output will go to the console, but if the program is opened, for example, through the start menu, the user will never see console output. Apparently, this is harder than it sounds on Windows.

目前,我在main()中使用以下技巧:

Currently, I use the following trickery in main():

#if _WINDOWS /* Fix console output on Windows */
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
    freopen("CONOUT$","wb",stdout);
    freopen("CONOUT$","wb",stderr);
}
#endif

这允许我在窗口之前创建输出实际上是由程序打开的,比如从命令行响应--help。但是,一旦一个窗口实际上由我的程序初始化和打开,控制台被返回。我需要一个解决方案,这将允许我在程序的整个生命期间继续访问控制台,而无需打开一个新的控制台,如果没有最初使用。

This allows me to create output before a window is actually opened by the program, such as responding to "--help" from the command line. However, once a window is actually initialized and opened by my program, the console is returned. I need a solution that will allow me continued access to the console throughout the life of my program, without opening a new console if none was originally used.

推荐答案

我们使用:: AllocConsole()而不是:: AttachConsole,它在整个应用程序中保持打开。试试吧?

We use ::AllocConsole() instead of ::AttachConsole and it remains open throughout the app. Try that?

这篇关于Windows GUI +控制台输出,Linux风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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