The console interface of Windows API

Source: Internet
Author: User

The console interface of Windows API

Tkorays ([email protected])

In Windows, the user interface is in the form of the Console and window (Windows). Console form interface We are often in contact, is not that black box? But we don't use the Windows API directly, usually we call printf to output, scanf to enter. In fact, this C-language function is also implemented through the Windows API. If you don't believe it, you can open Crtdll.dll to see the string inside.
There are not many APIs on the console, mainly including, getting console handles, console input and output, console styling. Here are the following:
1)HANDLE WINAPI getstdhandle (_in_ DWORD nstdhandle); Gets a handle based on a parameter. The standard input handle is std_input_handle, the standard output handle is Std_output_handle, or is set to the error handle std_error_handle.
2)BOOL WINAPI writeconsole (_in_ HANDLE hconsoleoutput,//standard output handle _in_ const void* lpbuffer,//output content buffer pointer _in_ DWORD nnumberofcahrstowrite,//buffer content size _out_ Lpdword lpnumberofcharswriten//Actual output character count LPVoid lpreseverd); Reserved parameters, set to NULL
3)BOOL WINAPI readconsole (_in_ HANDLE hconsoleinput,//Standard input handle _out_ lpvoid lpbuffer,//save read-in character buffer pointer _in DWORD NN Umberofcharstoread,//Buffer size Out_ lpdword lpnumberofcharsread,//actual read-in character _in_opt_ lpvoid Pinoutcontrol//input control in, this is a A pointer to the Console_readconsole_control struct, which can be set to null);
4)BOOL WINAPI getconsolescreenbufferinfo _in_ HANDLE hconsoleoutput,//Standard output handle _out_ Pconsole_screen_buffer_info Lpconsolescreenbufferinfo//CONSOLE_SCREEN_BUFFER_INFO structure pointer, used to save console font color and other information, this can be used to restore the initial settings, that is, the first to obtain the Save console information,     This variable can also be used to restore the settings after the modification);      The CONSOLE_SCREEN_BUFFER_INFO structure is defined as follows: typedef struct _CONSOLE_SCREEN_BUFFER_INFO {COORD dwsize;      COORD dwcursorposition;      WORD wattributes;      Small_rect Srwindow;     COORD dwmaximumwindowsize; } Console_screen_buffer_info;
5)BOOL WINAPI setconsoletextattribute (_in_ HANDLE hconsoleoutput,//Standard output handle _in_ WORD wattributes//combination of text attributes, foreground color, background    color, etc.); For example, setting the foreground color red, colour enhancement: Setconsoletextattribute (hstdout, foreground_red | foreground_intensity) Note Several color combinations change to other colors
6)BOOL WINAPI fillconsoleoutputattribute (_in_ HANDLE hconsoleoutput,//output handle _in_ WORD Wattribute,//color _in_ Dwor D nlength,///number of cells _in_ COORD Dwwritecoord,//fill coordinates _out_ Lpdword lpnumberofattrswritten//actual change);
7)Other Getconsoletitle/setconsoletitle Get/Set Console caption Fillconsoleoutputattribute set the properties of the text cell, as you can see from fill, this is the fill effect Fillconsol Eoutputcharacter Fill character setconsolecursorposition set cursor position get, set font, etc.
For more usage please refer to msdn:http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073%28v=vs.85%29.aspx

Example:

#include <stdio.h> #include <windows.h>int main (int argc, char** argv) {HANDLE hstdout = GetStdHandle (std_ Output_handle); Gets the output handle HANDLE hStdIn = GetStdHandle (Std_input_handle); Gets the input handle console_screen_buffer_info backinfo; Getconsolescreenbufferinfo (hStdOut, &backinfo); Save original information DWORD dwsize;//set color Writeconsolea (hStdOut, "I ' m tkorays.\n", &dwsize, NULL); The font color is not set and the content is ASCII encoded Setconsoletextattribute (hStdOut, foreground_red | foreground_intensity); Writeconsole (hStdOut, L "hello\n", 6, &dwsize, NULL); Set color after output, Unicode encoding Setconsoletextattribute (hStdOut, Foreground_green | foreground_intensity); system ("pause"); Pause under, look at the effect of the front lpwstr Lpchar = (LPWSTR) HeapAlloc (GetProcessHeap (), heap_zero_memory, 2048); COORD COORD; Relative coordinates coord. X = 0;coord. Y = 0; Console_screen_buffer_info Csbi; Getconsolescreenbufferinfo (hStdOut, &AMP;CSBI); Get at this time screen cache Readconsoleoutputcharacter (hStdOut, Lpchar, 2047, coord, &dwsize); Get a screen cache//color array for later set word wcolors[3];wcolors[0] = background_bLUE | foreground_red | Foreground_blue | FOREGROUND_GREEN;WCOLORS[1] = background_red | BACKGROUND_GREEN;WCOLORS[2] = background_green;//Modify the screen cache DWORD Dwlen;for (size_t i = 0; i < dwsize; i++) {//The lowercase letters in the screen change style if (Lpchar[i] >= ' A ' &&lpchar[i] <= ' z ') {//calculate character coordinate coord. Y = I/csbi.dwsize.x;coord. X = i%csbi.dwsize.x; Writeconsoleoutputattribute (hStdOut, wcolors, 1, coord, &dwlen);}} Restores the original font color Setconsoletextattribute (hstdout, backinfo.wattributes); return 0;}

Results:
Run to System ("pause"), Where:

After you modify the lowercase letter style:




The console interface of Windows API

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.