45fan.com - 路饭网

搜索: 您的位置主页 > 手机频道 > 阅读资讯:怎么样基于对话框的应用程序有命令行输出窗口?

怎么样基于对话框的应用程序有命令行输出窗口?

2016-08-31 13:04:39 来源:www.45fan.com 【

怎么样基于对话框的应用程序有命令行输出窗口?

VC中机遇对话框的应用程序中没有普通的命令行输出,这样对于想看到整型或者浮点型结果输出的工程来说,虽然可以通过类型转换,然后用MessageBox()方法输出,但是很不方便,如果能够像普通的命令行用cout或者printf输出就好了。

其实,要实现这个功能很简单,只用在工程中添加两个文件和两句话就可以了。

首先创建一个基于对话框的应用程序test。

添加Console.h文件,内容如下:

#ifndef __CONSOLE_H__
#define __CONSOLE_H__

#define PLATFORM_ENABLE_CONSOLE

#endif //__CONSOLE_H__

添加Console.cpp文件,内容如下:

#include "stdafx.h"
#include <windows.h>
#include <process.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <fstream>
#include "Console.h"


#ifdef PLATFORM_ENABLE_CONSOLE

#define MAX_CONSOLE_LINES 1000;
HANDLE g_hConsoleOut; // Handle to debug console


void RedirectIOToConsole(const char title[]);

// This function dynamically creates a "Console" window and points stdout and stderr to it.
// It also hooks stdin to the window
// You must free it later with FreeConsole

void RedirectIOToConsole(const char title[])
{
int hConHandle;
long lStdHandle;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
FILE *fp;
// allocate a console for this app
AllocConsole();
// set the screen buffer to be big enough to let us scroll text
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
coninfo.dwSize.Y = MAX_CONSOLE_LINES;
// How many lines do you want to have in the console buffer
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
// redirect unbuffered STDOUT to the console
g_hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
#pragma warning(push)
#pragma warning(disable:4311)
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );
// redirect unbuffered STDIN to the console
lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
setvbuf( stdin, NULL, _IONBF, 0 );
// redirect unbuffered STDERR to the console
lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
#pragma warning(pop)
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stderr = *fp;
setvbuf( stderr, NULL, _IONBF, 0 );
SetConsoleTitle(title);
// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well
// Uncomment the next line if you are using c++ cio or comment if you don't

}

#endif// PLATFORM_ENABLE_CONSOLE
////////////////////////////////////////////////////////////////////////////
//
// end of console.cpp
//
////////////////////////////////////////////////////////////////////////////

然后,在test.cpp文件中添加下面两句话:

在BEGIN_MESSAGE_MAP(CTempdlgApp, CWinApp)前面添加void RedirectIOToConsole(const char title[]);

在CTestApp::InitInstance()函数添加中RedirectIOToConsole(" ");

 

本文地址:http://www.45fan.com/a/luyou/70304.html
Tags: 窗口 基于 输出
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部