C Language Console window graphic interface Programming four common text output functions

Source: Internet
Author: User
Tags bool

Text color attributes have been learned, then learn a few more commonly used text output functions, as follows:

BOOL Fillconsoleoutputattribute (    //Fill character Properties    
HANDLE Hconsoleoutput,              //Handle    
word wattribute,                    //Text properties    
DWORD nlength,                      //number    
COORD Dwwritecoord,                 //Start position    
Lpdword Lpnumberofattrswritten      // Returns the number of fills    
);    
BOOL Fillconsoleoutputcharacter (    ///fill the specified data character    
HANDLE hconsoleoutput,              //Handle    
TCHAR Ccharacter,                   //Character    
DWORD nlength,//                      number of characters    
COORD Dwwritecoord,                 //Start position    
Lpdword Lpnumberofcharswritten      //write number    
);    
BOOL Writeconsoleoutputcharacter (   //Inserts a specified number of characters at a specified position    
HANDLE hconsoleoutput,              //Handle    
LPCTSTR Lpcharacter,                //String    
DWORD nlength,//                      number of characters    
COORD Dwwritecoord,                 //start position    
Lpdword Lpnumberofcharswritten      //written number    
);

In addition a structural body of the expression area is introduced, as follows:

The typedef struct _SMALL_RECT  //Represents the structural body of the rectangular region {short left    
  ;       The left side of short top    
  ;        Upper border short right    
  ;      Right border short    
  Bottom;     Lower boundary    
} small_rect;    
/* Microsoft's official statement is the x-coordinate of the lower-right vertex of the y-coordinate of the top-left vertex of the upper-left vertex of the    left-hand region   
Bottom The  y-coordinate of the lower-right vertex of the area   

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/C/

With the above text output function, let's do a simple sample program that displays a string in a shaded window, as follows:

#include <stdio.h> #include <stdlib.h> #include <Windows.h> #include <conio.h>     int main () {char *str = "Hello world!";    
    Define output information int len = strlen (str), I;     WORD shadow = background_intensity; Shadow properties WORD Text = Background_green |    background_intensity;    Text attributes HANDLE handle_out = GetStdHandle (Std_output_handle);    Obtain the standard output device handle Console_screen_buffer_info csbi;  Defines the window buffer information structure body getconsolescreenbufferinfo (handle_out, &AMP;CSBI);      Obtain window buffer Information Small_rect RC;      Defines a text box output area COORD postext;    Defines the starting coordinates of a text box COORD Posshadow; Defines the starting coordinate of the shadow box//determines the boundary RC for the area.     top = 8; Upper boundary RC. Bottom = RC.     Top + 4; Lower boundary RC.    left = (csbi.dwsize.x-len)/2-2; The left edge, in order for the output string to be centered on the RC. right = RC.   Left + len + 4; Right boundary//OK text box starting coordinate postext.x = RC.    
    Left; POSTEXT.Y = RC.    
    Top; Determines the starting coordinate p of the shadow Boxosshadow.x = postext.x + 1;    
    POSSHADOW.Y = Postext.y + 1; For (i=0 i<5; ++i)//First output shadow box {Fillconsoleoutputattribute (Handle_out, shadow, Len + 4, Posshado    
        W, NULL);    
    posshadow.y++; For (i=0 i<5; ++i)//In the Output text box, where the part that overlaps with the shadow box is overwritten {Fillconsoleoutputattribute (handle_out    
        , text, Len + 4, Postext, NULL);    
    postext.y++; //Set the coordinates postext.x = RC at the text output.    
    Left + 2; POSTEXT.Y = RC.    
    Top + 2;   Writeconsoleoutputcharacter (Handle_out, str, Len, Postext, NULL);   Output string Setconsoletextattribute (Handle_out, csbi.wattributes);    
    Restores the original properties CloseHandle (Handle_out);    
return 0; }

The examples above are compiled in Code::Blocks 13.12.

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.