複製鏈接
請複製以下鏈接發送給好友

clrscr

鎖定
清除文本模式窗口 清屏的意思 就是把之前顯示出的文字字符去掉 跟cmd裏面的清屏的功能是一樣的 實際上是clear screen的簡寫
中文名
清除文本模式窗口
外文名
clrscr
解    釋
清屏的意思
全    稱
clear screen

目錄

clrscr用 法

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;
}

clrscr注意

①只有在Turbo C 中可以運行 !
②在Turbo C++ 中,需要先另存為(save as).C格式,才能使用。

clrscr另解

在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;
}