clrscr

清除文本模式視窗 清屏的意思 就是把之前顯示出的文字字元去掉 跟cmd裡面的清屏的功能是一樣的 實際上是clear screen的簡寫

用 法:

void clrscr(void);

程式例:

#include <conio.h>

int main ()

{

int i;

clrscr();

for (i = 0; i < 20; i++);

cprintf("%d\r\n", i);

cprintf("\r\nPress any key to clear screen");

getch();

clrscr();

cprintf("The screen has been cleared!");

getch();

return 0;

}

注意:

①只有在Turbo C 中可以運行 !

②在Turbo C++ 中,需要先另外儲存為(save as).C格式,才能使用。

另解:

在VC中無法調用該函式,有下列辦法:

1. #include <stdlib.h>

system("cls");

這種辦法的缺點是程式額外運行系統程式執行清屏操作,延長了程式執行時間。

2.自己寫函式,這種辦法比較快。

這是從微軟MSDN得到的方法:

/* Standard error macro for reporting API errors */

#define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %d from %s \

on line %d\n", __FILE__, GetLastError(), api, __LINE__);}

void cls(HANDLE hConsole)

{

COORD coordScreen = {0, 0}; /* here's where we'll home the cursor */

BOOL bSuccess;

DWORD cCharsWritten;

CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */

DWORD dwConSize; /* number of character cells in the current buffer */

/* get the number of character cells in the current buffer */

bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);

PERR(bSuccess, "GetConsoleScreenBufferInfo");

dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

/* fill the entire screen with blanks */

bSuccess = FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',

dwConSize, coordScreen, &cCharsWritten);

PERR(bSuccess, "FillConsoleOutputCharacter");

/* get the current text attribute */

bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);

PERR(bSuccess, "ConsoleScreenBufferInfo");

/* now set the buffer's attributes accordingly */

bSuccess = FillConsoleOutputAttribute(hConsole, csbi.wAttributes,

dwConSize, coordScreen, &cCharsWritten);

PERR(bSuccess, "FillConsoleOutputAttribute");

/* put the cursor at (0, 0) */

bSuccess = SetConsoleCursorPosition(hConsole, coordScreen);

PERR(bSuccess, "SetConsoleCursorPosition");

return;

}

或者

/*清屏函式*/

#include <stdio.h>

#include <windows.h>

#define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %d from %s on line %d\n", __FILE__,GetLastError(), api, __LINE__);}

void MyCls(HANDLE) ;

inline void clrscr(void)

{

HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDLE);

MyCls(hStdOut);

return;

}

void MyCls(HANDLE hConsole)

{

COORD coordScreen={0,0};//設定清屏後游標返回的螢幕左上角坐標

BOOL bSuccess;

DWORD cCharsWritten;

CONSOLE_SCREEN_BUFFER_INFO csbi;//保存緩衝區信息

DWORD dwConSize;//當前緩衝區可容納的字元數

bSuccess=GetConsoleScreenBufferInfo(hConsole,&csbi);//獲得緩衝區信息

PERR(bSuccess,"GetConsoleScreenBufferInfo");

dwConSize=csbi.dwSize.X * csbi.dwSize.Y;//緩衝區容納字元數目

//用空格填充緩衝區

bSuccess=FillConsoleOutputCharacter(hConsole,(TCHAR)' ',dwConSize,coordScreen,&cCharsWritten);

PERR(bSuccess,"FillConsoleOutputCharacter");

bSuccess=GetConsoleScreenBufferInfo(hConsole,&csbi);//獲得緩衝區信息

PERR(bSuccess,"ConsoleScreenBufferInfo");

//填充緩衝區屬性

bSuccess=FillConsoleOutputAttribute(hConsole,csbi.wAttributes,dwConSize,coordScreen,&cCharsWritten);

PERR(bSuccess,"FillConsoleOutputAttribute");

//游標返回螢幕左上角坐標

bSuccess=SetConsoleCursorPosition(hConsole,coordScreen);

PERR(bSuccess,"SetConsoleCursorPosition");

return;

}

/*測試*/

int main()

{

printf("1111") ;

clrscr() ;

return 0;

}

相關詞條

相關搜尋

熱門詞條

聯絡我們