Gui programs can also use console windows

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.