Touring Whidbey's Base Class Library Enhancements : Page 5
The core API set underpinning managed application development in .NETthe Base Class Libraries (BCL)have received several notable additions in the Whidbey release. Find out performance-based improvements, class-oriented feature additions, and the introduction of previously missing functionality through entirely new classes.
by Michael Lane Thomas
Feb 19, 2004
Page 5 of 6
Not Your Grandpa's Console
The BCL class that gains the most new features is the Console class. Prior to the new features, the Console class allowed limited input and output buffer access, and interactions limited to very linear scenarios. Reading and writing task interactions with the screen buffer were dictated by the simple Read, Write, ReadLine, and WriteLine methods.
Enhancements to the Console class enable complete control over positioning of the cursor, foreground and background colors, and the ability to move sections of text, including color scheme, around the screen buffer. Moving text around the screen buffer eliminates the need to manually reconstruct the information in the screen buffer at the new location. The new properties supported by the Console class, shown in Table 3, allow extensive abilities to manipulate the buffer screen characteristics. The possible color choices have been expand to 16 choices.
Table 3: Numerous new Console class properties provide significantly enhanced features for console-based applications.
Console Property name
Modifies the background color of the console
Gets the current height of the buffer area
Gets the current width of the buffer area
Gets the current column position of the cursor
Modifies the current size of the cursor
Gets the current row position of the cursor
Modifies whether the cursor is visible
Modifies the foreground color of the console
Indicates whether a key has been pressed
Modifies the title of the console title bar
Modifies whether (CTRL+C) is treated as ordinary user input to be processed by the Console class or handled by the operating system as a program execution interruption. If set to False, the CancelKeyPress event can be used to process (CTRL+C).
Gets the current height of the console window area
Gets the current leftmost position of the console window area relative to the screen buffer
Gets the current top position of the console window area relative to the screen buffer
Gets the current width of the console window
The Console class gains numerous methods and a new event. You can call the Beep method to issue a system beep. You can call the Clear method to move the cursor to the upper-left corner and clear the screen buffer using the current values of the ForegroundColor and BackgroundColor properties. Call the ResetColor method to revert back to the default console color scheme. You can call the SetBufferSize method to programmatically control the size of the screen buffer and call MoveBufferArea to reallocate characters and their current color scheme from one section of the screen buffer to another without manually reconstructing the screen buffer section affected. You use a combination of the KeyAvailable property and the ReadKey method to grab user input character by character without blocking the executing thread. You handle the CancelKeyPress event to perform complex logic in response to the user typing Ctrl-C.
The enhancements to the Console class provide significantly increased options for developing highly interactive console-based applications that are far more visually appealing than the traditional console-based application. These applications don't have the overhead associated with moving to a full-blown graphical-based user interface provided by Windows Forms-based applications.
An example of many of these features in action together is shown in Figure 4, and the complete code listing is in Listing 1.