In many cases, it is inconvenient to debug the GUI program. The common practice is to use MessageBox, but as a modal window, it often produces unnecessary messages, such as killfocus, setfocus, or paint, this affects the execution of debugging. Of course, the VC debugger is also good, but this can easily cause window switching and interference messages.
Therefore, it is much easier to use the CIN/cout object or printf family function as in the console program. Generally, Windows does not generate a separate command line window for GUI programs. Therefore, we cannot see the output using the standard input/output stream. Since the system does not provide it, create one by yourself!
The following is a simple Console window object. It creates a command line window for your program and redirects stdout, stdin, and stderr to this command line window. After setting up such an object in the program, you can directly use CIN/cout/* printf to manipulate this new command line window!
. H file
# Ifndef _ custom_console _
# DEFINE _ custom_console _
# Include <Io. h>
# Include <fcntl. h>
# Include <stdio. h>
# Include <windows. h>
Class Console
{
Public:
Console ();
Console (maid, short consoleheight = 300, short consolewidth = 80 );
~ Console ();
PRIVATE:
Void attach (short consoleheight, short consolewidth );
Static bool isexistent;
};
# Endif
. Cpp File
# Include "****. H"
Bool Console: isexistent = false;
Console: Console ()
{
If (isexistent)
Return;
Allocconsole ();
Attach (300, 80 );
Isexistent = true;
}
Console: Console (lpctstr lpsztitle, short consoleheight, short consolewidth)
{
If (isexistent)
Return;
Allocconsole ();
Setconsoletitle (lpsztitle );
Attach (consoleheight, consolewidth );
Isexistent = true;
}
Void Console: attach (short consoleheight, short consolewidth)
{
Handle hstd;
Int FD;
File * file;
// Redirect the standard input stream handle to the new console window
Hstd = getstdhandle (std_input_handle );
FD = _ open_osfhandle (reinterpret_cast <intptr_t> (hstd), _ o_text); // text mode
File = _ fdopen (FD, "R ");
Setvbuf (file, null, _ ionbf, 0); // No Buffer
* Stdin = * file;
// Redirect the standard output stream handle to the new console window
Hstd = getstdhandle (std_output_handle );
Coord size;
Size. x = consolewidth;
Size. Y = consoleheight;
Setconsolescreenbuffersize (hstd, size );
FD = _ open_osfhandle (reinterpret_cast <intptr_t> (hstd), _ o_text); // text mode
File = _ fdopen (FD, "W ");
Setvbuf (file, null, _ ionbf, 0); // No Buffer
* Stdout = * file;
// Redirect the standard error stream handle to the new console window
Hstd = getstdhandle (std_error_handle );
FD = _ open_osfhandle (reinterpret_cast <intptr_t> (hstd), _ o_text); // text mode
File = _ fdopen (FD, "W ");
Setvbuf (file, null, _ ionbf, 0); // No Buffer
* Stderr = * file;
}
Console ::~ Console ()
{
If (isexistent)
{
Freeconsole ();
Isexistent = false;
}
}
You can create this object in winmain. If you create this object in main, a new console window will also appear.
# Ifdef _ debug // of course, it can also be used in release
Console notused;
# Endif