热情软件屋

 

如何在控制台程序中清屏


编号:QA000889
建立日期: 1999年4月30日 最后修改日期:1999年4月30日
所属类别:

James Chang:
    在Borland C++ 里, clrscr(); 可以清屏幕. 但在Microsoft Visual C++控制台程序里,不工作。请问, 在Microsoft Visual C++ 里, 如何清屏幕? 多谢!

回答:

    没有API来做这个工作,但可以使用下面的代码实现:
     /* 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;
     }

此问题由李海回答。

 
把这个问题推荐给朋友
   
   
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)
 
 

版权所有 1997-2008 热情软件屋
如果您有任何建议和意见, 请给我发个电子邮件 askpro@china-askpro.com
Web Designed by ZebraStudio