MFCProgramTo output debugging information, we generally use trace or log files, which is not very convenient. The first one needs to be in the debugging status, the second one is also difficult to configure and is not intuitive. Using the console to display debugging information should be a better choice. The following describes how to use the console to output debugging information in the MFC program.
1. You can create a command line window and call the allocconsole () function during initialization of the main program. Call freeconsole () when exiting ()
1 Bool cmfctestapp: initinstance ()
2 {
3 ...
4 # Ifdef _ debug
5 Allocconsole ();
6 # Endif
7 ...
8 Cwinapp: initinstance ();
9 }
In this way, the output function must use _ cprintf or writeconsole (getstdhandle (std_output_handle)...). Other types such as cout and printf cannot be output here. Of course, you can encapsulate a set of debugging functions by yourself.
2. modify the configuration information of the Project. Generally, the program type is configured through these two/subsystem: Windows. This can be seen in project properties/linker/system/subsystem, we can display the command line by modifying the program to the console type, but directly modifying this attribute will prompt a link error because different types of programs have different program portals. Here we use a simple method to add a sentence directly in stdafx. h.
1 # Ifdef _ debug
2 # PragmaComment (linker, "/subsystem: console/entry: wwinmaincrtstartup ")// I use vs2005
3 # Endif
We have redefined the system types and entry functions, which can be combined as needed. You can refer to the current property information of your project, project property/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 ")
PS How to change the display color of the Win32 console Program
Code
Handle hcon = Getstdhandle (std_output_handle );
/*
Std_input_handle standard input handle std_output_handle standard output handle std_error_handle standard error handle */
Setconsoletextattribute (hcon, forecolor | Backgroundcolor );
/* The text and background colors can be foreground_blue, foreground_green, foreground_red, foreground_intensity, background_blue, background_green, background_red, and background_intensity. */
End: happy National Day ~