CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    [RESOLVED] about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    i understand that i need use WriteConsoleOutputAttribute() for change the colors and WriteConsoleOutput() for write the text. can anyone explain to me how use them?
    i have read about them but i don't understand some parameters

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    Quote Originally Posted by 2kaud View Post
    thanks for that example.
    theres 1 thing that i'm confused: WriteConsoleOutputAttribute() is for format the text(colors) and the WriteConsoleOutput() is for show the tex?

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    WriteConsoleOutputAttribute changes the attributes of text in the screen buffer without changing the text
    WriteConsoleOutput writes text and attributes to the screen buffer
    WriteConsoleOutputCharacter writes text to the screen buffer and keeps the existing text attributes
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    Quote Originally Posted by 2kaud View Post
    WriteConsoleOutputAttribute changes the attributes of text in the screen buffer without changing the text
    WriteConsoleOutput writes text and attributes to the screen buffer
    WriteConsoleOutputCharacter writes text to the screen buffer and keeps the existing text attributes
    sorry i can't find the WriteConsoleOutputAttribute () example

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    Quote Originally Posted by Cambalinho View Post
    sorry i can't find the WriteConsoleOutput() example
    I gave you the reference for sample code
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    Quote Originally Posted by 2kaud View Post
    I gave you the reference for sample code
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    sorry about that... i mean WriteConsoleOutputAttribute()

  8. #8
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    Quote Originally Posted by Cambalinho View Post
    sorry i can't find the WriteConsoleOutputAttribute () example
    Its was with the SDK. This is a copy


    Writing Characters or Colors to Consecutive Cells
    Characters or color attributes can be written to specified character cells in a screen buffer. The following example uses the WriteConsoleOutputCharacter function to write a string of characters beginning at the upper left corner of a screen buffer. Then the example uses the WriteConsoleOutputAttribute function to write a string of color attributes to the first 51 cells of the same row. The coord parameter for both functions specifies the character cell in the console screen buffer at which writing begins. The location in the console window where these characters or colors appear depends on the current window rectangle of the console screen buffer. For additional information about the relationship between a screen buffer and its windows, see Window and Screen Buffer Size and Scrolling the Screen Buffer.


    Note that MyErrorExit is a placeholder for an application-defined function to display and handle error conditions.
    Code:
        HANDLE hOutput; 
        LPTSTR lpszString = "Character String"; 
        DWORD cWritten; 
        BOOL fSuccess; 
        COORD coord; 
        WORD wColors[3], wColor; 
        CHAR chFillChar; 
     
    // Write a string of characters to a screen buffer. 
     
        coord.X = 0;            // start at first cell 
        coord.Y = 0;            //   of first row      
        fSuccess = WriteConsoleOutputCharacter( 
            hOutput,              // screen buffer handle 
            lpszString,           // pointer to source string 
            lstrlen(lpszString),  // length of string 
            coord,                // first cell to write to 
            &cWritten);           // actual number written 
        if (! fSuccess) 
            MyErrorExit("WriteConsoleOutputCharacter"); 
     
    // Write a string of colors to a screen buffer. 
     
        wColors[0] = BACKGROUND_RED; 
        wColors[1] = BACKGROUND_RED |     // white background 
                     BACKGROUND_GREEN | 
                     BACKGROUND_BLUE; 
        wColors[2] = BACKGROUND_BLUE; 
    
        for (;fSuccess && coord.X < 50; coord.X += 3) 
        {
            fSuccess = WriteConsoleOutputAttribute( 
                hOutput,          // screen buffer handle 
                wColors,          // pointer to source string 
                3,                // length of string 
                coord,            // first cell to write to 
                &cWritten);       // actual number written 
        }
        if (! fSuccess) 
            MyErrorExit("WriteConsoleOutputAttribute");
    The same character or color attribute can be written to a specified number of consecutive screen buffer cells beginning at a specified location. The following example uses the FillConsoleOutputCharacter function to clear a 80-by-50-character screen buffer, and then it uses the FillConsoleOutputAttribute function to set the color attributes of the same cells.
    Code:
    // Fill an 80-by-50-character screen buffer with the space character. 
     
        coord.X = 0;            // start at first cell 
        coord.Y = 0;            //       of first row 
        chFillChar = ' '; 
     
        fSuccess = FillConsoleOutputCharacter( 
            hStdout,          // screen buffer handle 
            chFillChar,       // fill with spaces 
            80*50,            // number of cells to fill 
            coord,            // first cell to write to 
            &cWritten);       // actual number written 
    
        if (! fSuccess) 
            MyErrorExit("FillConsoleOutputCharacter"); 
     
    // Set 80-by-50-character screen buffer colors to white text on red. 
     
        wColor = BACKGROUND_RED | 
                 FOREGROUND_RED | 
                 FOREGROUND_GREEN | 
                 FOREGROUND_BLUE; 
    
        fSuccess = FillConsoleOutputAttribute( 
            hStdout,          // screen buffer handle 
            wColor,           // color to fill with 
            80*50,            // number of cells to fill 
            coord,            // first cell to write to 
            &cWritten);       // actual number written 
    
        if (! fSuccess) 
            MyErrorExit("FillConsoleOutputAttribute");
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    how use CHAR_INFO structure?

    typedef struct _CHAR_INFO {
    union {
    WCHAR UnicodeChar;
    CHAR AsciiChar;
    } Char;
    WORD Attributes;
    } CHAR_INFO, *PCHAR_INFO;



    fSuccess = WriteConsoleOutput(
    hNewScreenBuffer, // screen buffer to write to
    chiBuffer, // buffer to copy from
    coordBufSize, // col-row size of chiBuffer
    coordBufCoord, // top left src cell in chiBuffer
    &srctWriteRect); // dest. screen buffer rectangle

    where i put the string?

  10. #10
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    You don't directly. You use an array of CHAR_INFO with each element of the array holding one char (AsciiChar) and one attribute for the char (Attributes).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    Quote Originally Posted by 2kaud View Post
    You don't directly. You use an array of CHAR_INFO with each element of the array holding one char (AsciiChar) and one attribute for the char (Attributes).
    finally i put it to work. but when i put it on my code, i get unexpected print results:
    Code:
    Blink *p = static_cast<Blink *>(params);
        SHORT tlen = (SHORT)p->Text.length();
        SHORT x=p->x, y=p->y;
        HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
        const char *text=(char*)p->Text.c_str();
        SHORT stringlen=strlen(text);
        CHAR_INFO *ConsoleText= new CHAR_INFO[tlen];
        CHAR_INFO *EmptyConsoleText= new CHAR_INFO[tlen];
        COORD a={tlen,1}, b={0,0};
        SMALL_RECT c={x,y,x+tlen,y+(SHORT)1};
    
        int i=0;
        for (i=0; i<=stringlen;i++)
        {
            ConsoleText[i].Char.AsciiChar  =text[i];
            ConsoleText[i].Attributes=FOREGROUND_GREEN;
            ConsoleText[i].Char.AsciiChar  =' ';
        }
    what wrong is on these code, that give me diferent characters? is about string terminator?

  12. #12
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    finally i fix all errors:
    Code:
    Blink *p = static_cast<Blink *>(params);
        SHORT tlen = (SHORT)p->Text.length();
        SHORT x=p->x, y=p->y;
        HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
        const char *text=(char*)p->Text.c_str();
        SHORT stringlen=strlen(text);
        CHAR_INFO *ConsoleText= new CHAR_INFO[tlen];
        CHAR_INFO *EmptyConsoleText= new CHAR_INFO[tlen];
        COORD a={tlen,1}, b={0,0};
        SMALL_RECT c={x,y,x+tlen,y+(SHORT)1};
    
        int i=0;
        for (i=0; i<stringlen;i++)
        {
            ConsoleText[i].Char.AsciiChar  =text[i];
            ConsoleText[i].Attributes=FOREGROUND_GREEN;
            EmptyConsoleText[i].Char.AsciiChar  =' ';
            EmptyConsoleText[i].Attributes=FOREGROUND_GREEN;
        }
    
        while (true)
        {
            WriteConsoleOutput(hout,ConsoleText,a,b,&c);
            Sleep(500);
            WriteConsoleOutput(hout,EmptyConsoleText,a,b,&c);
            Sleep(500);
        }
    but i get 2 warnings in these line:
    Code:
    SMALL_RECT c={x,y,x+tlen,y+(SHORT)1};
    1 - "warning: narrowing conversion of '(((int)x) + ((int)tlen))' from 'int' to 'SHORT {aka short int}' inside { } [-Wnarrowing]";

    2 - "warning: narrowing conversion of '(((int)y) + 1)' from 'int' to 'SHORT {aka short int}' inside { } [-Wnarrowing]".

    how can i fix theses warnings?

  13. #13
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    Code:
    for (i=0; i<=stringlen;i++)
    Index starts at 0 and finishes 1 less than the number of characters.

    stringlen should have the same value as tlen!
    Code:
    ConsoleText[i].Char.AsciiChar  =' ';
    don't you mean
    Code:
    EmptyConsoleText[i].Char.AsciiChar = ' ';
    also for EmptyConsoleText, you haven't set the attribute value.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    Quote Originally Posted by 2kaud View Post
    Code:
    for (i=0; i<=stringlen;i++)
    Index starts at 0 and finishes 1 less than the number of characters.

    stringlen should have the same value as tlen!
    Code:
    ConsoleText[i].Char.AsciiChar  =' ';
    don't you mean
    Code:
    EmptyConsoleText[i].Char.AsciiChar = ' ';
    also for EmptyConsoleText, you haven't set the attribute value.
    thanks for correct me, but i continue having that 2 warnings
    about Attributes: i don't know the combinations colors, can you give the list please?

  15. #15
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions

    about Attributes: i don't know the combinations colors, can you give the list please?
    See http://msdn.microsoft.com/en-us/libr...ter_attributes

    You need to be able to find your way around microsoft's msdn web site. There are loads of stuff on there re development. It would often be quicker to try searching there rather than posting a question here.

    I use these extra definitions

    Code:
    #define FOREGROUND_WHITE		(FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN)
    #define FOREGROUND_YELLOW		(FOREGROUND_RED | FOREGROUND_GREEN)
    #define FOREGROUND_CYAN			(FOREGROUND_BLUE | FOREGROUND_GREEN)
    #define FOREGROUND_MAGENTA		(FOREGROUND_RED | FOREGROUND_BLUE)
    #define FOREGROUND_BLACK		0
    
    #define FOREGROUND_INTENSE_RED	        (FOREGROUND_RED | FOREGROUND_INTENSITY)
    #define FOREGROUND_INTENSE_GREEN	(FOREGROUND_GREEN | FOREGROUND_INTENSITY)
    #define FOREGROUND_INTENSE_BLUE	        (FOREGROUND_BLUE | FOREGROUND_INTENSITY)
    #define FOREGROUND_INTENSE_WHITE	(FOREGROUND_WHITE | FOREGROUND_INTENSITY)
    #define FOREGROUND_INTENSE_YELLOW	(FOREGROUND_YELLOW | FOREGROUND_INTENSITY)
    #define FOREGROUND_INTENSE_CYAN	        (FOREGROUND_CYAN | FOREGROUND_INTENSITY)
    #define FOREGROUND_INTENSE_MAGENTA	(FOREGROUND_MAGENTA | FOREGROUND_INTENSITY)
    
    #define BACKGROUND_WHITE		(BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN)
    #define BACKGROUND_YELLOW		(BACKGROUND_RED | BACKGROUND_GREEN)
    #define BACKGROUND_CYAN	        	(BACKGROUND_BLUE | BACKGROUND_GREEN)
    #define BACKGROUND_MAGENTA		(BACKGROUND_RED | BACKGROUND_BLUE)
    #define BACKGROUND_BLACK		0
    
    #define BACKGROUND_INTENSE_RED   	(BACKGROUND_RED | BACKGROUND_INTENSITY)
    #define BACKGROUND_INTENSE_GREEN	(BACKGROUND_GREEN | BACKGROUND_INTENSITY)
    #define BACKGROUND_INTENSE_BLUE	        (BACKGROUND_BLUE | BACKGROUND_INTENSITY)
    #define BACKGROUND_INTENSE_WHITE	(BACKGROUND_WHITE | BACKGROUND_INTENSITY)
    #define BACKGROUND_INTENSE_YELLOW	(BACKGROUND_YELLOW | BACKGROUND_INTENSITY)
    #define BACKGROUND_INTENSE_CYAN	        (BACKGROUND_CYAN | BACKGROUND_INTENSITY)
    #define BACKGROUND_INTENSE_MAGENTA	(BACKGROUND_MAGENTA | BACKGROUND_INTENSITY)
    Last edited by 2kaud; November 3rd, 2013 at 10:34 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured