Adding consoles to MFC programs
MFC program, if you want to output debugging information, we are generally trace or use a log file, is not very convenient, the first one needs us in the debug state, the second is also to configure the trouble and not intuitive. Using the console to display debug information should be a better choice. Here are a few of the MFC programs that use the console to output debugging information.
1. We can create a command-line window ourselves, calling the AllocConsole () function when the main program is initialized. The corresponding exit call Freeconsole ()
1 BOOL cmfctestapp::initinstance ()2 {3 ...4 #ifdef _DEBUG5 allocconsole ();6 #endif7 ...8 cwinapp::initinstance ();9 }
In this way, the output function needs to use _cprintf or Writeconsole (GetStdHandle (std_output_handle) ...), and the others, such as cout,printf, cannot be exported here. Of course, you can encapsulate a set of debugging functions yourself.
2. Modify the project configuration information, the General control program type is through the two/subsystem:windows to configure, this can be seen in the project properties/linker/system/subsystem here, We can make the program display the command line by modifying the program as the console type, but modifying this property directly will prompt for link errors because different types of programs have different entry points. Here is a simple way to add a sentence directly to the stdafx.h
1 #ifdef _DEBUG 2 #pragma COMMENT (linker, "/subsystem:console/entry:wwinmaincrtstartup") //I use VS2005 3 #endif
We have redefined the system type and the entry function, which can be arbitrarily combined according to your own needs. You can refer to the current property information of your project, project properties/linker/command line, the following reference
#pragma COMMENT (linker, "/subsystem:windows/entry:winmaincrtstartup")
#pragma COMMENT (linker, "/subsystem:windows/entry:maincrtstartup")
#pragma COMMENT (linker, "/subsystem:console/entry:maincrtstartup")
#pragma COMMENT (linker, "/subsystem:console/entry:winmaincrtstartup")
How PS changes the display color of Win32 Console program
Code
HANDLE Hcon = GetStdHandle (std_output_handle); /* */setconsoletextattribute (hcon,forecolor| backgroundcolor); /* its text and background colors can be foreground_blue, Foreground_green, foreground_red, foreground_intensity, Background_blue, Background_green, background_red, and background_intensity* *
Adding consoles to MFC programs