共找到2條詞條名為ClearScreen的結果 展開
- VC 語言命令
- PC Logo 語言命令
ClearScreen
VC 語言命令
ClearScreen,即清屏操作,方法是調用系統函數system發送命令cls(system("cls");。
VC開發環境下實現控制台程序清屏的方法目前只有兩種:
一種是眾所周知的調用系統函數system發送命令cls(system("cls");)實現,但這種辦法有缺陷,程序需要執行系統程序完成清屏操作,這加大了運行負擔。對此,微軟MSDN有一個解決方法,用戶自己在程序中添加下列代碼,調用ClearScreen函數也可以完成清屏操作,類似BC和TC下的Clrscr函數。
#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 };
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;
}