ClrScrn function using winapi not working.

Any idea why this clear screen function isn't working?

I took the code from http://stackoverflow.com/questions/14295570/why-is-console-animation-so-slow-on-windows-and-is-there-a-way-to-improve-spee (scroll down a bit)

Anyway, the 'written' variable shows 0 (as in 0 cells were filled) and the cells arent being filled.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  void ClrScrn(char attrib) {
    COORD pos = { 0, 0 };
    DWORD written;
    unsigned size_;

    size_ = line_max * column_max;

    FillConsoleOutputCharacter(console, ' ', size_, pos, &written);

    cout << written;


    //FillConsoleOutputAttribute(console, attrib, size_, pos, &written);
    //SetConsoleCursorPosition(console, pos);
}
Try this :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
#include <windows.h>
#include <conio.h>
void _clrscr()
{
    HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD Written;
    COORD Home;
    Home.X=0;
    Home.Y=0;
    FillConsoleOutputCharacterA(hOut,' ',80*300,Home,&Written);
    SetConsoleCursorPosition(hOut,Home);
}
int main()
{
    cout<<"Hello World !"<<endl;
    cout<<"Press any key to clear the screen";
    _getch();
    _clrscr();
    return 0;
}
Fixed. The 'console' variable was not good. I needed to initialize it like
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
Topic archived. No new replies allowed.