zoukankan      html  css  js  c++  java
  • 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;
     }
    
    

  • 相关阅读:
    python学习-基础-day5-文件操作和函数
    (转) 什么是立即执行函数?有什么作用?
    AngularJS 从DOM中获取scope
    AngularJS中Directive指令系列
    AngularJS中Directive指令系列
    angularJS ng-repeat中的directive 动态加载template
    (转) 前端面试之js相关问题(一)
    ES6 localStorage 类库
    underscore.js 源码分析5 基础函数和each函数的使用
    记录使用jQuery和Python抓取采集数据的一个实例
  • 原文地址:https://www.cnblogs.com/flying_bat/p/1768599.html
Copyright © 2011-2022 走看看