In the process of writing complex programs, we often need to export some information to a file or screen. More than console application, MFC form program to appear troublesome!
There are 2 ways to add a debug console to an MFC form program, which allows programmers to debug programs and understand the current program's running state.
Important Windows API:allocconsole(); Creating the console window
Freeconsole (); Destroying the console window
(1) Start the console window
header files that need to be included
#include <io.h>
#include <cstdio>
#include <FCNTL.H>
voidCtestdebugconsoledlg::onbnclickedbutton6 () {//Start Debugconsoleallocconsole (); intptr_t handle=(intptr_t) GetStdHandle (std_output_handle);intHCRT =_open_osfhandle (Handle,_o_text); FILE* HF = _fdopen (HCRT,"W" );*stdout =*HF;Chartitle[1024x768] ={0};sprintf_s (title,1024x768,"debugcosole[%u]", (unsignedLong)( This-GetSafeHwnd ())); Setconsoletitle (title); Setconsoletextattribute (HANDLE) HANDLE, foreground_red| Foreground_blue |foreground_intensity); HWND hwnd=NULL; while(Null==hwnd) hwnd=:: FindWindow (NULL, (LPCTSTR) title); HMENU HMENU=:: GetSystemMenu (hwnd, FALSE); DeleteMenu (Hmenu, Sc_close, Mf_bycommand);}
MFC Forms Test Program:
Console information displays:
Code Download (VS2008): Http://files.cnblogs.com/kekec/DebugConsole.rar
(2) Design another MFC form program
For this approach, see " Implementing a common Debug console " for Azure Product.
Adding the debug console to an MFC form program