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?
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.
Re: how use WriteConsoleOutputAttribute() function?
Quote:
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: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
Re: how use WriteConsoleOutputAttribute() function?
Quote:
Originally Posted by
Cambalinho
now i'm more confused: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, ...);
Quote:
Originally Posted by
Cambalinho
and i think that the last parameter is '1'.
No, it is (from MSDN)
Quote:
A pointer to a variable that receives the number of attributes actually written to the console screen buffer.
Quote:
Originally Posted by
Cambalinho
but where is puted the text?:(
What text? :confused:
This function is used to set attributes to consecutive cells of a console screen buffer, not the characters!
Re: how use WriteConsoleOutputAttribute() function?
Quote:
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? :confused:
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:(
Re: how use WriteConsoleOutputAttribute() function?
Quote:
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);
Re: how use WriteConsoleOutputAttribute() function?
Quote:
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:(
Re: how use WriteConsoleOutputAttribute() function?
Quote:
Originally Posted by
Cambalinho
i see 2 diferent rectangules but i don see how can i blink:(
2kaud already suggested you a way to achieve it. In some of your other threads. :cool:
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)!:rolleyes: 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.
Quote:
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.
Re: how use WriteConsoleOutputAttribute() function?
Quote:
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)!:rolleyes: 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?
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.
Re: how use WriteConsoleOutputAttribute() function?
Quote:
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?
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);
}
Re: how use WriteConsoleOutputAttribute() function?
Quote:
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
Re: how use WriteConsoleOutputAttribute() function?
A couple of thoughts for you to think about - now that you're refreshed:D.
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?