how use WriteConsoleOutputAttribute() function?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: how use WriteConsoleOutputAttribute() function?

  1. #1
    Join Date
    Apr 2009
    Posts
    638

    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?

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,364

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2009
    Posts
    638

    Re: how use WriteConsoleOutputAttribute() function?

    Quote Originally Posted by 2kaud View Post
    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

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,386

    Re: how use WriteConsoleOutputAttribute() function?

    Quote Originally Posted by Cambalinho View Post
    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, ...);
    Quote Originally Posted by Cambalinho View Post
    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.
    Quote Originally Posted by Cambalinho View Post
    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

  5. #5
    Join Date
    Apr 2009
    Posts
    638

    Re: how use WriteConsoleOutputAttribute() function?

    Quote Originally Posted by VictorN View Post
    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

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,386

    Re: how use WriteConsoleOutputAttribute() function?

    Quote Originally Posted by Cambalinho View Post
    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

  7. #7
    Join Date
    Apr 2009
    Posts
    638

    Re: how use WriteConsoleOutputAttribute() function?

    Quote Originally Posted by Cambalinho View Post
    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

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,386

    Re: how use WriteConsoleOutputAttribute() function?

    Quote Originally Posted by Cambalinho View Post
    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.
    Victor Nijegorodov

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,364

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Apr 2009
    Posts
    638

    Re: how use WriteConsoleOutputAttribute() function?

    Quote Originally Posted by 2kaud View Post
    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?

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,364

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #12
    Join Date
    Apr 2009
    Posts
    638

    Re: how use WriteConsoleOutputAttribute() function?

    Quote Originally Posted by 2kaud View Post
    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?

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,364

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Apr 2009
    Posts
    638

    Re: how use WriteConsoleOutputAttribute() function?

    Quote Originally Posted by 2kaud View Post
    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

  15. #15
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,364

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center