ClearScreen(VC 語言命令)

本詞條是多義詞,共2個義項
更多義項 ▼ 收起列表 ▲

ClearScreen,即清屏操作,方法是調用系統函式system傳送命令cls(system("cls");。

基本介紹

  • 中文名:清屏操作
  • 外文名:ClearScreen
  • 系統:windows
方法,具體實現代碼如下,

方法

VC開發環境下實現控制台程式清屏的方法目前只有兩種:
一種是眾所周知的調用系統函式system傳送命令cls(system("cls");)實現,但這種辦法有缺陷,程式需要執行系統程式完成清屏操作,這加大了運行負擔。對此,微軟MSDN有一個解決方法,用戶自己在程式中添加下列代碼,調用ClearScreen函式也可以完成清屏操作,類似BC和TC下的Clrscr函式。

具體實現代碼如下

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

相關詞條

熱門詞條

聯絡我們