-
August 21st, 2013, 09:56 AM
#1
how use WriteConsoleOutputAttribute() function?
i'm trying use the WriteConsoleOutputAttribute() function for create the blink effect, but seems not working
seems that i'm confuse the the parameters types
Code:
void BlinkText(char *Text, COORD TextPos, int BackColor, int Forecolor) {
WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),Forecolor|BackColor<<4,strlen(Text),TextPos,Text);
}
can anyone advice me?
-
August 21st, 2013, 10:56 AM
#2
Re: how use WriteConsoleOutputAttribute() function?
Look at the definition and use of WriteConsoleOutputAttribute on MSDN
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
You will see that param 2 is a pointer to an array of WORD of attributes to be used and that the last param is a pointer to a DWORD that returns the number of attributes actually written.
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)
-
August 21st, 2013, 11:12 AM
#3
Re: how use WriteConsoleOutputAttribute() function?
Originally Posted by 2kaud
Look at the definition and use of WriteConsoleOutputAttribute on MSDN
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
You will see that param 2 is a pointer to an array of WORD of attributes to be used and that the last param is a pointer to a DWORD that returns the number of attributes actually written.
now i'm more confused
if the 2nd is a pointer, how can i change the colors?
and i think that the last parameter is '1'.
but where is puted the text?
i'm very confused.. sorry
-
August 21st, 2013, 11:27 AM
#4
Re: how use WriteConsoleOutputAttribute() function?
Originally Posted by Cambalinho
now i'm more confused
if the 2nd is a pointer, how can i change the colors?
You define a WORD variable, set the desired character attributes to it, and then pass in the pointer of this variable to the WriteConsoleOutputAttribute:
Code:
WORD attr = Forecolor|BackColor<<4;
WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), &attr, ...);
Originally Posted by Cambalinho
and i think that the last parameter is '1'.
No, it is (from MSDN)
A pointer to a variable that receives the number of attributes actually written to the console screen buffer.
Originally Posted by Cambalinho
but where is puted the text?
What text?
This function is used to set attributes to consecutive cells of a console screen buffer, not the characters!
Victor Nijegorodov
-
August 21st, 2013, 11:42 AM
#5
Re: how use WriteConsoleOutputAttribute() function?
Originally Posted by VictorN
You define a WORD variable, set the desired character attributes to it, and then pass in the pointer of this variable to the WriteConsoleOutputAttribute:
Code:
WORD attr = Forecolor|BackColor<<4;
WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), &attr, ...);
No, it is (from MSDN)
What text?
This function is used to set attributes to consecutive cells of a console screen buffer, not the characters!
sorry i'm trying but i don't understand the last parameter
-
August 21st, 2013, 11:49 AM
#6
Re: how use WriteConsoleOutputAttribute() function?
Originally Posted by Cambalinho
sorry i'm trying but i don't understand the last parameter
Code:
DWORD NumberOfAttrsWritten;
WORD attr = Forecolor|BackColor<<4;
WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), &attr, ..., ..., &NumberOfAttrsWritten);
Victor Nijegorodov
-
August 21st, 2013, 11:50 AM
#7
Re: how use WriteConsoleOutputAttribute() function?
Originally Posted by Cambalinho
sorry i'm trying but i don't understand the last parameter
ok.. no errors but i don't know how test it
see these code:
Code:
void BlinkText(string Text, COORD TextPos, int BackColor, int Forecolor)
{
WORD attr = Forecolor|BackColor<<4;
DWORD cWritten;
WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),&attr,sizeof(attr),TextPos,&cWritten);
}
and how i use it:
Code:
int _tmain(int argc, _TCHAR* argv[])
{
Console a;
COORD b;
b.X=1;
b.Y=1;
a.BlinkText("Hello world",b,RED, BLUE);
a.Write("hello");
a.Read();
return 0;
}
i see 2 diferent rectangules but i don see how can i blink
-
August 21st, 2013, 11:56 AM
#8
-
August 21st, 2013, 12:28 PM
#9
Re: how use WriteConsoleOutputAttribute() function?
Code:
void BlinkText(string Text, COORD TextPos, int BackColor, int Forecolor)
{
WORD attr = Forecolor|BackColor<<4;
DWORD cWritten;
WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),&attr,sizeof(attr),TextPos,&cWritten);
}
No! attr has 1 attribute so the third parameter should be 1 not sizeof(attr)! But this isn't going to blink your text. As Victor says in post #8, I've already suggested a way for you to achieve this.
There is no default mechanism to blink a character on screen. if you want this, it can be done by first reversing (exchanging) foreground and background attributes (COMMON_LVB_REVERSE_VIDEO) then setting a timer for the required blink period then reversing foreground and background attributes when timer triggers.
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)
-
August 21st, 2013, 12:36 PM
#10
Re: how use WriteConsoleOutputAttribute() function?
Originally Posted by 2kaud
Code:
void BlinkText(string Text, COORD TextPos, int BackColor, int Forecolor)
{
WORD attr = Forecolor|BackColor<<4;
DWORD cWritten;
WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),&attr,sizeof(attr),TextPos,&cWritten);
}
No! attr has 1 attribute so the third parameter should be 1 not sizeof(attr)! But this isn't going to blink your text. As Victor says in post #8, I've already suggested a way for you to achieve this.
when i will understand what you mean
heres the code:
Code:
void SetColorAndBackground(int ForgC, int BackC=0)
{
for (;;)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ForgC|BackC<<4 );
Sleep(1000);
cout << "oi";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),COMMON_LVB_REVERSE_VIDEO );
Sleep(1000);
}
}
but how i don't lose the caret position?
-
August 21st, 2013, 01:10 PM
#11
Re: how use WriteConsoleOutputAttribute() function?
Try this. This will output oi at position 0,0 on the console and blink it. Note that this is not really the way to do it as the sleeps make the program unresponsive. This should be done using a timer.
Code:
void SetColorAndBackground(int ForgC, int BackC=0)
{
COORD pos = {0,0};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
for (;;) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ForgC|(BackC<<4) );
cout << "oi";
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
Sleep(1000);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BackC|(ForgC<<4) );
cout << "oi";
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
Sleep(1000);
}
}
You have to write the text twice, once with one set of attributes and then again with the second set of attributes. Just changing the attribute doesn't change text that is already on the screen. Once attribute is changed, only text subsequently written gets written with the new attribute.
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)
-
August 21st, 2013, 01:17 PM
#12
Re: how use WriteConsoleOutputAttribute() function?
Originally Posted by 2kaud
Try this. This will output oi at position 0,0 on the console and blink it. Note that this is not really the way to do it as the sleeps make the program unresponsive. This should be done using a timer.
Code:
void SetColorAndBackground(int ForgC, int BackC=0)
{
COORD pos = {0,0};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
for (;;) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ForgC|(BackC<<4) );
cout << "oi";
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
Sleep(1000);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BackC|(ForgC<<4) );
cout << "oi";
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
Sleep(1000);
}
}
You have to write the text twice, once with one set of attributes and then again with the second set of attributes. Just changing the attribute doesn't change text that is already on the screen. Once attribute is changed, only text subsequently written gets written with the new attribute.
thanks. i have another question for close: is possible save the actual position?
see these sample
Code:
here blinks
but you are here
can i do these?
-
August 21st, 2013, 01:25 PM
#13
Re: how use WriteConsoleOutputAttribute() function?
To set the cursor position and get the current cursor position, try these
Code:
//Get cursor position
DWORD GetCursorPosition(COORD& pos)
{
CONSOLE_SCREEN_BUFFER_INFO sinfo;
if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sinfo)) {
return (GetLastError());
}
pos.X = sinfo.dwCursorPosition.X;
pos.Y = sinfo.dwCursorPosition.Y;
return (0);
}
//Set cursor position
DWORD SetCursorPosition(COORD pos)
{
return (!SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos) ? GetLastError() : 0);
}
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)
-
August 21st, 2013, 01:45 PM
#14
Re: how use WriteConsoleOutputAttribute() function?
Originally Posted by 2kaud
To set the cursor position and get the current cursor position, try these
Code:
//Get cursor position
DWORD GetCursorPosition(COORD& pos)
{
CONSOLE_SCREEN_BUFFER_INFO sinfo;
if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sinfo)) {
return (GetLastError());
}
pos.X = sinfo.dwCursorPosition.X;
pos.Y = sinfo.dwCursorPosition.Y;
return (0);
}
//Set cursor position
DWORD SetCursorPosition(COORD pos)
{
return (!SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos) ? GetLastError() : 0);
}
thanks for all. tomorrow i will share the code... i'm C++ tired lol
thanks for all
-
August 21st, 2013, 02:55 PM
#15
Re: how use WriteConsoleOutputAttribute() function?
A couple of thoughts for you to think about - now that you're refreshed.
1) As Victor mentioned in a previous post, you are not checking for an error using GetStdHandle. You are assuming that a console is present and that you can get its handle. What happens if no console is present? Why not obtain the handle once in the class constructor and if not valid allocate a console? Something like this
Code:
//in private
HANDLE hout;
...
//In constructor
if ((hout = GetStdhandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
FreeConsole();
if (!AllocConsole()) {
MessageBox(0, "Cannot allocate console", "Console class", MB_OK | MV_ICONSTOP);
} else {
if ((hout = GetStdhandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
MessageBox(0, "Cannot obtain console handle", "Console class", MB_OK | MV_ICONSTOP);
}
}
}
2) What if 2 or more instances of your console class are created? IMO at present with your class design they would independently perform their functions on the same console window. So you might have one class instance writing data and another instance clearing the console or trying to write different data to the same locations etc etc? Have you thought about having a differen console window for each instance - or do you want each class instance to share the same console?
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
|