【MFC】 MFC添加控制台Console进行调试

来源:互联网 发布:数据挖掘及应用是什么 编辑:程序博客网 时间:2024/05/15 08:18

需要包含头文件

#include <conio.h>


在主对话框OnInitDialog()函数中添加

AllocConsole();


在需要显示数据时(这里使用的unicode数据集)

HANDLE hdlWrite = GetStdHandle(STD_OUTPUT_HANDLE); //这里也可以使用STD_ERROR_HANDLE
DWORD dw;
TCHAR ch[256] = _T("你好");
WriteConsole(hdlWrite, ch, lstrlen(ch), &dw, NULL);
WriteConsole(hdlWrite, "\n", strlen("\n"), &dw, NULL);

double testDouble = 2017.0101;
TCHAR ch[256];
memset(ch, 0, 256);
_stprintf_s(ch, _T("%.4f"), testDouble);
WriteConsole(m_hdlWrite, ch, lstrlen(ch) , &m_dw, NULL);
WriteConsole(m_hdlWrite, "\n", strlen("\n"), &m_dw, NULL);


在程序结束时 如 OnClose() 中进行释放

FreeConsole();


0 0