-
November 2nd, 2013, 04:04 PM
#1
[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
-
November 2nd, 2013, 04:57 PM
#2
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)
-
November 2nd, 2013, 05:02 PM
#3
Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions
Originally Posted by 2kaud
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?
-
November 2nd, 2013, 05:12 PM
#4
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)
-
November 2nd, 2013, 05:15 PM
#5
Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions
Originally Posted by 2kaud
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
-
November 2nd, 2013, 05:17 PM
#6
Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions
Originally Posted by Cambalinho
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)
-
November 2nd, 2013, 05:19 PM
#7
Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions
Originally Posted by 2kaud
sorry about that... i mean WriteConsoleOutputAttribute()
-
November 2nd, 2013, 05:25 PM
#8
Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions
Originally Posted by Cambalinho
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)
-
November 3rd, 2013, 06:33 AM
#9
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?
-
November 3rd, 2013, 07:01 AM
#10
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)
-
November 3rd, 2013, 09:20 AM
#11
Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions
Originally Posted by 2kaud
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?
-
November 3rd, 2013, 09:32 AM
#12
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?
-
November 3rd, 2013, 09:34 AM
#13
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)
-
November 3rd, 2013, 09:53 AM
#14
Re: about WriteConsoleOutput() and WriteConsoleOutputAttribute() functions
Originally Posted by 2kaud
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?
-
November 3rd, 2013, 10:27 AM
#15
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|