Thread: Question about clrscr() in FAQ (Win32)

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    Question about clrscr() in FAQ (Win32)

    here's the clrscr() function found here :
    Code:
    void clrscr(void)
    {
        COORD                       coordScreen = { 0, 0 };
        DWORD                       cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO  csbi;
        DWORD                       dwConSize;
        HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter(hConsole, TEXT(' '), 
                                   dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
                                   dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
    I got three questions about it :

    1. Why did the author call GetConsoleScreenBufferInfo() again although csbi already contains the screen attributes? (red text)

    2. Whats the difference between using ' ' and TEXT(' ') ? i searched mdsn but couldn't find anything. (blue text)

    3. And finally , is the following clrscr() function equivalent to the above or not (if not , wich one is better) :

    Code:
    void clrscr(void)
    {
        COORD                       coordScreen = { 0, 0 };
        DWORD                       cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO  csbi;
        DWORD                       dwConSize;
        HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    
        FillConsoleOutputCharacter(hConsole, ' ', 
                                   dwConSize, coordScreen, &cCharsWritten);
       
        FillConsoleOutputAttribute(hConsole, 0 , dwConSize, coordScreen, &cCharsWritten);
    
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
    I just took off the other GetConsoleScreenBufferInfo() and specified the character attributes myself.


    Thanks for your time.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    1. Remeber he is only declaring a CONSOLE_SCREEN_BUFFER_INFO type that contains no data, so when he calls that function it puts that data into cbsi.
    2. I am not sure if there is a difference but TEXT is a macro defined as
    Code:
    //this is from the Win32 Programmers Reference
    TEXT(LPTSTR string);	// address of ANSI or Unicode string
    3. I'll let someone else answer
    Last edited by prog-bman; 01-11-2005 at 03:28 AM.
    Woop?

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    1. Remeber he is only declaring a stuct so that contains no data, so when he calls that function it put that data into cbsi.
    I think you missed the first call to GetConsoleScreenBufferInfo(). Re-read the code and you'll see that he actually filled csbi twice , my question was why did he do that
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Ah I see that now, I don't see the need also.
    As for yours I was looking at it your might want the 0 in the FillConsoleOutputAttribute
    to be what the author used as the declaration for it is
    Code:
    BOOL FillConsoleOutputAttribute(
    
        HANDLE hConsoleOutput,	// handle to screen buffer 
        WORD wAttribute,	// color attribute to write 
        DWORD nLength,	// number of character cells to write to 
        COORD dwWriteCoord,	// x- and y-coordinates of first cell 
        LPDWORD lpNumberOfAttrsWritten 	// pointer to number of cells written to 
       );
    Woop?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The TEXT macro is used so the string is made up of TCHARs instead of chars.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Also, your first method is better since it preserves the console attributes.

  7. #7
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    As for yours I was looking at it your might want the 0 in the FillConsoleOutputAttribute
    to be what the author used as the declaration for it is
    Also, your first method is better since it preserves the console attributes.
    but why? 0 means black (for both foreground and background color) and since all console windows are black (AFAIK) how are the two codes different?


    The TEXT macro is used so the string is made up of TCHARs instead of chars.
    Whats the difference between TCHAR and the normal 'char'? (i googled it but couldn't find anything useful)
    Last edited by Brain Cell; 01-11-2005 at 05:58 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    but why? 0 means black (for both foreground and background color) and since all console windows are black (AFAIK) how are the two codes different?
    console windows are black by default, but they can be many different colors.

    Whats the difference between TCHAR and the normal 'char'?
    A tchar is something that is defined in tchar.h. It is used so that a programmer doesn't have to have two sets of source code for both unicode and ascii. When you work with TCHARs, you should use the correct string functions.
    Like _tcscpy instead of strcpy, and _tcslen instead of strlen. If you correctly code using TCHARs instead of chars, then all you need to do is define _UNICODE to make your program unicode instead of ASCII.

  9. #9
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Thanks bithub , that cleared it up. I still don't see the need of gathering the color attribute TWICE. (does anyone have ANY idea about it? )
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I dont think the second call to GetConsoleScreenBufferInfo() is neccessary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  2. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  3. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  4. win32 DLL question
    By DLL User in forum Windows Programming
    Replies: 1
    Last Post: 09-01-2002, 03:12 PM
  5. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM