compatable clrscr

Started by
6 comments, last by EvilCrap 22 years, 5 months ago
In a win32 consol app under mvc++, how do you clear the screen? is there a cout member that does this? dos.h is barely recognized, and i cannot use clrscr()
Advertisement
Something like this:
  void Win32Console::clear()    {        //get the size of the window        CONSOLE_SCREEN_BUFFER_INFO info;        GetConsoleScreenBufferInfo( hStdOut, &info );                int noChars = info.dwSize.X * info.dwSize.Y;        //upper left        COORD coord;        coord.X = 0;        coord.Y = 0;        //fill the screen with spaces        DWORD written;        FillConsoleOutputCharacter( hStdOut, '' '', noChars, coord, &written );        //reset the cursor        setPosition( 0, 0 );    }  


"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours." - - Stephen Roberts
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
ok, i get

error C2653: ''Win32Console'' : is not a class or namespace name
error C2065: ''hStdOut'' : undeclared identifier
error C2065: ''setPosition'' : undeclared identifier

try

System("cls");
system("cls");

rather
quote:Original post by EvilCrap
error C2653: ''Win32Console'' : is not a class or namespace name
error C2065: ''hStdOut'' : undeclared identifier
error C2065: ''setPosition'' : undeclared identifier

Those are his own classes and variables. You need to obtain a handle to the console and possibly to its screenbuffer. Read up on the Win32 Console functions. Everything inside that clear() function except the setPosition() function is legal Win32 (as you can see from your error messages). setPosition() is probably just equivalent to SetConsoleCursorPosition().

And why you would think dos.h would be recognized under Windows is beyond me...
It took me a while to figure it out myself, but I believe this is the most appropriate way of doing it:

    void console::clrscr() {     HANDLE outH;     DWORD writtenchars, writtenattrs;     COORD top_left={0,0};     CONSOLE_SCREEN_BUFFER_INFO cbsi;     outH=GetStdHandle(STD_OUTPUT_HANDLE);     GetConsoleScreenBufferInfo(outH,&cbsi);     FillConsoleOutputCharacter(outH,0,cbsi.dwSize.X*cbsi.dwSize.Y,top_left,&writtenchars);     FillConsoleOutputAttribute(outH,cbsi.wAttributes,cbsi.dwSize.X*cbsi.dwSize.Y,top_left,&writtenattrs);     SetConsoleCursorPosition(outH,top_left);     CloseHandle(outH);}    



Edited by - Monolith on October 31, 2001 12:11:35 PM
My fault...I should have cleaned it up first. I took this straight out of a console wrapper class I wrote, and sort of thought you could extrapolate the missing bits(I forgot that hStdOut wasnt defined in there). Anyway, this will work:

          //get the handle to standard output        HANDLE hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );        //get the size of the window        CONSOLE_SCREEN_BUFFER_INFO info;        GetConsoleScreenBufferInfo( hStdOut, &info );                int noChars = info.dwSize.X * info.dwSize.Y;        //upper left        COORD coord;        coord.X = 0;        coord.Y = 0;        //fill the screen with spaces        DWORD written;        FillConsoleOutputCharacter( hStdOut, '' '', noChars, coord, &written );                SetConsoleCursorPosition( hStdOut, coord );                   



"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours." - - Stephen Roberts
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement