- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Chapter 22. Using OLE DB
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
Using Consoles
Consoles are special types of windows that emulate (and extend) the old 80x25 character DOS-style scrolling text display. An application can have only one attached console at any time, because the application's standard input and output channels are attached to the console.
You probably have seen the console in action when running the MS-DOS command prompt. This console lets you change the font size and screen buffer size, as well as modify the display to full-screen text mode. The console also provides limited cut-and-paste editing support.
The console uses selectable 8-bit code pages to display ANSI character set and national variants. You also can set attributes to show background and foreground colors.
Allocating a Console
Your application can start the console window simply by calling the AllocConsole() function. It can detach itself from the console by calling FreeConsole().
You can attach Win32 handles by calling CreateFile() and passing either CONIN$ or CONOUT$ as the filename and using the OPEN_EXISTING flag for the open mode parameter. Alternatively, the GetStdHandle() function returns the input, output, or error handles when GetStdHandle() is passed STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, or STD_ERROR_HANDLE, respectively. Or, you can redirect the standard handles of a console window by using the corresponding SetStdHandle() function.
Console I/O
After the console window is open and attached, you can use low-level, streams-based, or Win32 I/O (covered earlier in this chapter) to communicate with it.
Alternatively, a set of console-specific functions—such as ReadConsole() and WriteConsole()—can read characters from the keyboard and write character strings to the console window. You can use PeekConsoleInput() to read characters from the input buffer without removing them so that subsequent ReadConsole() functions retrieve the same "peeked at" characters.
You can call SetConsoleTextAttribute() to set the default color attributes of characters subsequently written to the console by passing combined attribute flags, such as BACKGROUND_GREEN, FOREGROUND_RED, and FOREGROUND_INTENSITY. These flags let you set eight foreground and background colors, each with two levels of brightness, giving 16 overall colors.
You can reposition the console's cursor by using the SetConsoleCursorPosition() function and passing the column (X) and row (Y) SHORT coordinates in a COORD structure. You can change your cursor's size and visibility with the SetConsoleCursorInfo() function after setting the dwSize and bVisible members of the CONSOLE_CURSOR_INFO structure. The SetConsoleCursorPosition() and SetConsoleCursorInfo() counterparts fill the appropriate structures to return the current settings.
You can change the input and output code pages with the SetConsoleCP() and SetConsoleOutputCP() functions. These change the way keyboard characters are mapped for input and display characters are shown for output.
You can simulate special keyboard events, such as Ctrl+Break and Ctrl+C keys (specified by the CTRL_BREAK_EVENT and CTRL_C_EVENT flags), by using the GenerateConsoleCtrlEvent() function. These events—whether generated by the user or from your code—can be handled by your supplied handler function. Your handler func tion must be registered with a call to SetConsoleCtrlHandler(), which can add a chain of event-handler functions.
Customizing the Console Buffers and Display
You can use a range of functions to set scrollable display buffers that are larger than the current display size. You can change the size of the window using the SetConsoleWindowInfo() function. You can find the largest possible size from GetLargestConsoleWindowSize() and the current settings from GetConsoleWindowInfo().
You can set the scrollable buffer larger than the visible console region. You can set this buffer size using SetConsoleScreenBufferSize() and passing a COORD structure holding the new number of rows and columns for the screen buffer. The ScrollConsoleScreenBuffer() function then lets you scroll the visible region from within your code to display various parts of the text buffer.
You can customize the title bar of your console window with a call to SetConsoleTitle() passing a pointer to the new text string.
Summary | Next Section

Account Sign In
View your cart