S.l.e!ep.¢%

像打了激速一样,以四倍的速度运转,开心的工作
简单、开放、平等的公司文化;尊重个性、自由与个人价值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Win32 控制台清屏方法

Posted on 2010-07-06 09:10 S.l.e!ep.¢% 阅读(1091) 评论(0)  编辑 收藏 引用 所属分类: VC

Win32 控制台清屏方法

最后觉得项目中的控制台不大好用,就添加了一些小功能,比如清屏;当然最简单的方法是调用系统自带的函数system(“cls”);,这里提 供一个方法一样可以清屏(测试比cls要快一些):

 

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

 


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理