C Language Console window graphic interface programming three functions to set text properties

Source: Internet
Author: User
Tags printf

Here is a function to set the Text property, as follows

BOOL Setconsoletextattribute (   //setting character properties of functions such as Writeconsole    
HANDLE hconsoleoutput,//handle    
Word Wattributes                //text attributes    
);

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

By the way, the text attribute, actually is the color attribute, has the background color and the foreground color (is the character color) two kinds, each class only provides three primary colors (red, green, blue) and strengthens the color (gray, may use with other colors, causes the color to lighten, will mention later). Finally there is a reverse color (not very clear how the use of this, very wonderful things). The sample program is as follows:

#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <conio.h>/*  Basic Text Properties Foreground_blue Blue Foreground_green green foreground_red red foreground_intensity Enhanced Background_blue
        
Blue background background_green green background background_red red background background_intensity background color enhanced common_lvb_reverse_video inverse color * *           Const WORD Fore_blue = Foreground_blue;          Blue Text Property const WORD Fore_green = Foreground_green;            Green Text Property const WORD fore_red = foreground_red; Red Text Property const WORD Fore_purple = Fore_blue |      fore_red; Purple Text Property Const WORD Fore_cyan = Fore_blue |    Fore_green; Cyan Text Property const WORD Fore_yellow = fore_red |     Fore_green;      Yellow Text Property const WORD Fore_gray = foreground_intensity;           Gray Text Properties const WORD Back_blue = Background_blue;          Blue Background Properties const WORD Back_green = Background_green; Green Background Property const WORD back_red = background_red;           Green Background Properties Const WORD Back_purple = Back_blue |      back_red; Purple Background Properties Const WORD Back_cyan = Back_blue |    Back_green; Cyan Background Property const WORD Back_yellow = back_red |     Back_green;      Yellow background Attribute const WORD Back_gray = background_intensity;    Gray Background Property int main () {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 Setconsoletextattribute (handle_out, Fore_blue);    
    printf ("Blue character \ n");    
    Setconsoletextattribute (Handle_out, fore_red);    
    printf ("Red character \ n");    
    Setconsoletextattribute (Handle_out, Fore_green);    
    printf ("Green character \ n");    
    Setconsoletextattribute (Handle_out, fore_purple);    
    printf ("Purple character \ n");    
    Setconsoletextattribute (Handle_out, Fore_cyan);    
    printf ("Cyan character \ n"); Setconsoletextattribute (handle_out, Fore_yellow);    
    printf ("Yellow character \ n");    
    Setconsoletextattribute (Handle_out, Fore_gray);    
    printf ("Gray character \ n"); Setconsoletextattribute (handle_out, Fore_green | Fore_blue |    
    fore_red);    
    printf ("White character \ n");    
    Setconsoletextattribute (Handle_out, Back_blue);    
    printf ("Blue background \ n");    
    Setconsoletextattribute (Handle_out, back_red);    
    printf ("Red background \ n");    
    Setconsoletextattribute (Handle_out, Back_green);    
    printf ("Green background \ n");    
    Setconsoletextattribute (Handle_out, back_purple);    
    printf ("Purple background \ n");    
    Setconsoletextattribute (Handle_out, Back_cyan);    
    printf ("Cyan background \ n");    
    Setconsoletextattribute (Handle_out, Back_yellow);    
    printf ("Yellow background \ n");    
    Setconsoletextattribute (Handle_out, Back_gray);    
    printf ("Gray background \ n"); Setconsoletextattribute (handle_out, Back_blue | back_red |    
    Back_green);    
    printf ("White background \ n"); Setconsoletextattribute (handle_out, Back_green | fore_red);    
Example: Green background red characters    printf ("Mixed green background with red characters \ n"); Setconsoletextattribute (handle_out, foreground_intensity |   fore_red);    
    Example: bright red character printf ("light generation, blending with enhanced color \ n");    
return 0; }

The example program above is best compiled with C + +, because there is a C-language compiler or the IDE does not support the way the constants are defined above. What needs to be learned from this example is that the blending of the three primary colors is a bitwise OR | In the C-language bit operation operator, the background color and character color are also defined using this operator fusion. In addition, will be any color and the corresponding reinforcement color (gray, have foreground and background two, need to correspond) after fusion will become the corresponding color of the highlight, such as red character and foreground enhancement color fusion will combine into bright red.

As for the inverse color, we can try, when I set the Text property is reversed color, the input characters are not displayed, but the subscript is still moving, I estimate that the inverse color will be white characters into black characters, and black background, so did not show it. As for the inverse color and other combinations and other color combinations, we also need to explore together 、、、

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.