Console window Interface Programming Control (2)

Source: Internet
Author: User

7. Rolling and moving

Scrollconsolescreenbuffer is an API function that implements rolling and moving in the SWAp area. It can move a specified text area to another area, and the area to be removed is filled by the specified characters. The function prototype is as follows:

Bool scrollconsolescreenbuffer (
Handle hconsoleoutput, // handle
Const small_rect * lpscrollrectangle, // area to be rolled or moved
Const small_rect * lpcliprectangle, // specifies the cropping area.
Coord dwdestinationorigin, // new location
Const char_info * lpfill // fill character
);

This API function can also be used to delete a specified row. The following is an example of a program:

# Include
# Include
# Include
Handle Hout;
Void deleteline (INT row); // delete a row
Void movetext (int x, int y, small_rect RC); // move the text block area
Void clearscreen (void); // clear the screen
Void main ()
{
Hout = getstdhandle (std_output_handle); // gets the handle of the standard output device.
Word ATT = foreground_red | foreground_green | foreground_intensity |
Background_blue;
// The background is blue and the text color is yellow.
Setconsoletextattribute (Hout, ATT );
Clearscreen ();
Printf ("/n/nthe soul selects her own society,/N ");
Printf ("then shuts the door;/N ");
Printf ("On Her Devine majority;/N ");
Printf ("obtrude no more./n ");
Lele_screen_buffer_info binfo;
Getconsolescreenbufferinfo (Hout, & binfo );
Coord endpos = {0, binfo. dwsize. Y-1 };
Setconsolecursorposition (Hout, endpos); // you can specify the cursor position.
Small_rect rc = {0, 2, 40, 5 };
_ Getch ();
Movetext (10, 5, RC );
_ Getch ();
Deleteline (5 );
Closehandle (Hout); // closes the handle of the standard output device.
}

Void deleteline (INT row)
{
Small_rect rcscroll, rcclip;
Coord crdest = {0, row-1 };
Char_info chfill;
Lele_screen_buffer_info binfo;
Getconsolescreenbufferinfo (Hout, & binfo );
Rcscroll. Left = 0;
Rcscroll. Top = row;
Rcscroll. Right = binfo. dwsize. X-1;
Rcscroll. Bottom = binfo. dwsize. Y-1;
Rcclip = rcscroll;
Chfill. Attributes = binfo. wattributes;
Chfill. Char. asciichar = '';
Scrollconsolescreenbuffer (Hout, & rcscroll, & rcclip, crdest, & chfill );
}

Void movetext (int x, int y, small_rect RC)
{
Coord crdest = {x, y };
Char_info chfill;
Lele_screen_buffer_info binfo;
Getconsolescreenbufferinfo (Hout, & binfo );
Chfill. Attributes = binfo. wattributes;
Chfill. Char. asciichar = '';
Scrollconsolescreenbuffer (Hout, & rc, null, crdest, & chfill );
}

Void clearscreen (void)
{
Lele_screen_buffer_info binfo;
Getconsolescreenbufferinfo (Hout, & binfo );
Coord home = {0, 0 };
Word ATT = binfo. wattributes;
Unsigned long size = binfo. dwsize. x * binfo. dwsize. Y;
Fillconsoleoutputattribute (Hout, ATT, size, home, null );
Fillconsoleoutputcharacter (Hout, '', size, home, null );
}

In the program, the basic principle of the Eline operation is: first, set the cropping area and moving area to the console window area below the specified row (including the row, then, specify the Moving position as (0, row-1 ). In this way, the content beyond the cropping area is cropped to delete the row.

Note that if the cropping area parameter is null, the cropping area is the entire Console window.
 

8. cursor operation

The cursor in the console window reflects the current position of text insertion. You can use the setconsolecursorposition function to change the current position so that you can control the character (string) output. In fact, the size and hide of the cursor can also be set through the corresponding API function. For example:

Bool setconsolecursorinfo (// sets the cursor Information
Handle hconsoleoutput, // handle
Const console_cursor_info * lpconsolecursorinfo // cursor Information
);

Bool getconsolecursorinfo (// get cursor Information
Handle hconsoleoutput, // handle
Pconsole_cursor_info lpconsolecursorinfo // returns the cursor information.
);

Both functions are related to the lele_cursor_info struct type, which is defined as follows:

Typedef struct _ console_cursor_info {
DWORD dwsize; // percentage of cursor size
Bool bvisible; // whether it is visible
} Console_cursor_info, * pconsole_cursor_info;

It should be noted that the dwsize value reflects the size of the cursor, and its value range is 1-100. When it is 1, the cursor is the smallest, only a horizontal line at the bottom, when it is 100, the maximum cursor is a character-sized square.

9. Reading keyboard Information

Keyboard Events usually include character events and button events. The information attached to these events forms the keyboard information. It is obtained through the readconsoleinput API function. Its prototype is as follows:

Bool readconsoleinput (
Handle hconsoleinput, // enter the device handle
Pinput_record lpbuffer, // return data records
DWORD nlength, // number of records to read
Lpdword lpnumberofeventsread // returns the number of records that have been read
);

Input_record is defined as follows:

Typedef struct _ input_record {
Word eventtype; // event type
Union {
Key_event_record keyevent;
Mouse_event_record mouseevent;
Window_buffer_size_record windowbuffersizeevent;
Menu_event_record menuevent;
Focus_event_record focusevent;
} Event;
} Input_record;

The key_event_record structure related to Keyboard Events is defined as follows:

Typedef struct _ key_event_record {
Bool bkeydown; // true indicates that the key is pressed, and false indicates that the key is released.
Word wrepeatcount; // Number of buttons
Word wvirtualkeycode; // virtual key code
Word wvirtualscancode; // virtual key scan code
Union {
Wchar unicodechar; // wide character
Char asciichar; // ASCII character
} Uchar; // character
DWORD dwcontrolkeystate; // control key status
} Key_event_record;

We know that every meaningful key on the keyboard corresponds to a unique scan code. Although the scan code can be used as a key identifier, it depends on a specific device. Therefore, in applications, virtual key code is often used that is irrelevant to a specific device. This type of virtual key code is a keyboard code unrelated to the device. In Visual C ++, the most common virtual key code has been defined in winuser. H. For example, vk_shift indicates the Shift key and vk_f1 indicates the function key.

In the preceding schema definition, dwcontrolkeystate is used to indicate the control key state. It can be capslock_on (caps lock light), enhanced_key (press the extension Key), and left_alt_pressed (press the left Alt key) left_ctrl_pressed (press the left ctrl key), numlock_on (Num Lock light on), right_alt_pressed (press the right Alt key), right_ctrl_pressed (press the right ctrl key), scrolllock_on (scroll lock light on) A combination of one or more values in the shift_pressed (press the Shift key.

The following program inputs the character of a user's key into a certain area of a console window, and when you press the num lock, caps lock, and scroll lock keys, the status of these keys is displayed in the last line of the console window.

# Include
Handle Hout;
Handle hin;
Void drawbox (bool bsingle, small_rect RC );
Void clearscreen (void );
Void charwindow (char CH, small_rect RC); // enter CH in the specified window.
Void controlstatus (DWORD State); // display the control key status in the last row
Void deletetopline (small_rect RC); // Delete the top row in the specified window and scroll
Void main ()
{
Hout = getstdhandle (std_output_handle); // gets the handle of the standard output device.
Hin = getstdhandle (std_input_handle); // get the device handle of the standard input.
Word ATT = foreground_red | foreground_green | foreground_intensity |
Background_blue;
// The background is blue and the text color is yellow.
Setconsoletextattribute (Hout, ATT );
Clearscreen (); // clear screen
Input_record keyrec;
DWORD state = 0, Res;
Char ch;
Small_rect rc = {20, 2, 40, 12 };
Drawbox (true, RC );
Coord Pos = {RC. Left + 1, RC. Top + 1 };
Setconsolecursorposition (Hout, POS); // you can specify the cursor position.
For (;) // Loop
{
Readconsoleinput (Hin, & keyrec, 1, & res );
If (State! = Keyrec. event. keyevent. dwcontrolkeystate ){
State = keyrec. event. keyevent. dwcontrolkeystate;
Controlstatus (State );
}
If (keyrec. eventtype = key_event ){
If (keyrec. event. keyevent. wvirtualkeycode = vk_escape) break;
// Press ESC to exit the loop
If (keyrec. event. keyevent. bkeydown ){
Ch = keyrec. event. keyevent. uchar. asciichar;
Charwindow (CH, RC );
}
}
}
POs. x = 0; pos. Y = 0;
Setconsolecursorposition (Hout, POS); // you can specify the cursor position.
Closehandle (Hout); // closes the handle of the standard output device.
Closehandle (HIN); // closes the handle of the standard input device.
}

Void charwindow (char CH, small_rect RC) // input ch to the specified window.
{
Static coord chpos = {RC. Left + 1, RC. Top + 1 };
Setconsolecursorposition (Hout, chpos); // you can specify the cursor position.
If (CH <0x20) | (CH> 0x7e) return;
Writeconsoleoutputcharacter (Hout, & Ch, 1, chpos, null );
If (chpos. x> = (RC. Right-1 ))
{
Chpos. x = RC. Left;
Chpos. y ++;
}
If (chpos. Y> (RC. Bottom-1 ))
{
Deletetopline (RC );
Chpos. Y = RC. Bottom-1;
}
Chpos. x ++;
Setconsolecursorposition (Hout, chpos); // you can specify the cursor position.
}

Void controlstatus (DWORD state) // display the control key status in the last row
{
Lele_screen_buffer_info binfo;
Getconsolescreenbufferinfo (Hout, & binfo );
Coord home = {0, binfo. dwsize. Y-1 };
Word att0 = background_intensity;
Word att1 = foreground_green | foreground_intensity | background_red;
Fillconsoleoutputattribute (Hout, att0, binfo. dwsize. X, home, null );
Fillconsoleoutputcharacter (Hout, '', binfo. dwsize. X, home, null );
Setconsoletextattribute (Hout, att1 );
Coord stapos = {binfo. dwsize. X-16, binfo. dwsize. Y-1 };
Setconsolecursorposition (Hout, stapos );
If (State & numlock_on)
Writeconsole (Hout, "num", 3, null, null );
Stapos. x + = 4;
Setconsolecursorposition (Hout, stapos );
If (State & capslock_on)
Writeconsole (Hout, "Caps", 4, null, null );
Stapos. x + = 5;
Setconsolecursorposition (Hout, stapos );
If (State & scrolllock_on)
Writeconsole (Hout, "scroll", 6, null, null );
Setconsoletextattribute (Hout, binfo. wattributes); // restore the original attribute
Setconsolecursorposition (Hout, binfo. dwcursorposition); // restore the original cursor position
}

Void deletetopline (small_rect RC)
{
Coord crdest;
Char_info chfill;
Small_rect rcclip = RC;
Rcclip. Left ++; rcclip. Right --;
Rcclip. Top ++; rcclip. Bottom --;
Crdest. x = rcclip. Left;
Crdest. Y = rcclip. Top-1;
Lele_screen_buffer_info binfo;
Getconsolescreenbufferinfo (Hout, & binfo );
Chfill. Attributes = binfo. wattributes;
Chfill. Char. asciichar = '';
Scrollconsolescreenbuffer (Hout, & rcclip, & rcclip, crdest, & chfill );
}

Shows the program running result:
10. Read mouse information

Similar to reading keyboard information, the mouse information is obtained through readconsoleinput. Its mouse_event_record has the following definitions:

Typedef struct _ mouse_event_record {
Coord dwmouseposition; // current mouse position
DWORD dwbuttonstate; // The status of the mouse button
DWORD dwcontrolkeystate; // The keyboard control key status.
DWORD dweventflags; // event status
} Mouse_event_record;

Among them, dwbuttonstate reflects the user's mouse button, which can be: Pushed (leftmost button), rightmost_button_pressed (rightmost button), from_left_2nd_button_pressed (second button from left) from_left_3rd_button_pressed (third button from left) and from_left_4th_button_pressed (fourth button from left ). Dweventflags indicates mouse events, such as double_click (double-click), mouse_moved (MOVE), and mouse_wheeled (scroll wheel scroll, applicable only to Windows 2000/XP ). The meaning of dwcontrolkeystate is the same as that before.

The following is an example. In this example, the current position of the mouse can be displayed on the last line of the console window. If you click the left mouse button, write a character 'a' at the current position. If you double-click any button, the program is terminated. The Code is as follows:

# Include <windows. h>
# Include <stdio. h>
# Include <string. h>
Handle Hout;
Handle hin;
Void clearscreen (void );
Void dispmousepos (coord POS); // display the mouse position on the last line
Void main ()
{
Hout = getstdhandle (std_output_handle); // gets the handle of the standard output device.
Hin = getstdhandle (std_input_handle); // get the device handle of the standard input.
Word ATT = foreground_red | foreground_green | foreground_intensity |
Background_blue;
// The background is blue and the text color is yellow.
Setconsoletextattribute (Hout, ATT );
Clearscreen (); // clear screen
Input_record mouserec;
DWORD state = 0, Res;
Coord Pos = {0, 0 };
For (;) // Loop
{
Readconsoleinput (Hin, & mouserec, 1, & res );
If (mouserec. eventtype = mouse_event ){
If (mouserec. event. mouseevent. dweventflags = double_click) break;
// Double-click the mouse to exit the loop
Pos = mouserec. event. mouseevent. dwmouseposition;
Dispmousepos (POS );
If (mouserec. event. mouseevent. dwbuttonstate = from_left_1st_button_pressed)
Fillconsoleoutputcharacter (Hout, 'A', 1, POs, null );
}
}
POs. x = 0; pos. Y = 0;
Setconsolecursorposition (Hout, POS); // you can specify the cursor position.
Closehandle (Hout); // closes the handle of the standard output device.
Closehandle (HIN); // closes the handle of the standard input device.
}

Void dispmousepos (coord POS) // display the mouse position on the last line
{
Lele_screen_buffer_info binfo;
Getconsolescreenbufferinfo (Hout, & binfo );
Coord home = {0, binfo. dwsize. Y-1 };
Word att0 = background_intensity;
Fillconsoleoutputattribute (Hout, att0, binfo. dwsize. X, home, null );
Fillconsoleoutputcharacter (Hout, '', binfo. dwsize. X, home, null );
Char s [20];
Sprintf (S, "x = % 2lu, y = % 2lu", POS. X, POS. y );
Setconsoletextattribute (Hout, att0 );
Setconsolecursorposition (Hout, home );
Writeconsole (Hout, S, strlen (s), null, null );
Setconsoletextattribute (Hout, binfo. wattributes); // restore the original attribute
Setconsolecursorposition (Hout, binfo. dwcursorposition); // restore the original cursor position
}

Void clearscreen (void)
{
Lele_screen_buffer_info binfo;
Getconsolescreenbufferinfo (Hout, & binfo );
Coord home = {0, 0 };
Unsigned long size = binfo. dwsize. x * binfo. dwsize. Y;
Fillconsoleoutputattribute (Hout, binfo. wattributes, size, home, null );
Fillconsoleoutputcharacter (Hout, '', size, home, null );
}

The program running result is as follows:

XI. Conclusion

In summary, the widows API functions in the console window can be used to design a concise and beautiful text interface, this makes it easy to use visual c ++ 6.0 development environment to thoroughly learn C ++ and text interface design. Of course, the design of the text interface still requires some methods and skills, which are limited by space.
Article Source: http://www.diybl.com/course/3_program/c++/cppsl/2008829/138698_2.html

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.