終端實現局部清屏效果 WindowsAPI

#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
    int n;
    cout << "請輸入倒計時時間:";
    cin >> n;
    for(int i = 0; i < 10; i++)
        cout << "===============\n";
    // HANDLE GetStdHandle( DWORD nStdHandle );
    // GetStdHandle()返回標準的輸入、輸出或錯誤的設備的句柄,也就是獲得輸入、輸出/錯誤的屏幕緩衝區的句柄。
    // STD_OUTPUT_HANDLE 標準輸出的句柄
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    // COORD是Windows API中定義的一種結構,表示一個字符在控制檯屏幕上的座標。其定義爲:
    // typedef struct _COORD {
    // SHORT X; // horizontal coordinate
    // SHORT Y; // vertical coordinate
    // } COORD;
    COORD pos;
    pos.X = 2, pos.Y = 5;

    /* 隱藏光標
        這個結構包含的是控制檯光標的信息
        typedef struct _CONSOLE_CURSOR_INFO {
            DWORD dwSize;// 光標百分比厚度(1~100)
            BOOL  bVisible;// 是否可見
        } CONSOLE_CURSOR_INFO, *PCONSOLE_CURSOR_INFO;
    */
    CONSOLE_CURSOR_INFO curInfo;
    curInfo.dwSize = 1;
    curInfo.bVisible= FALSE;
    // SetConsoleCursorInfo是用CONSOLE_CURSOR_INFO結構體的信息 設置控制檯光標信息,
    SetConsoleCursorInfo(hOut, &curInfo);

    while(n >= 0)
    {
        // 設置光標的位置
        SetConsoleCursorPosition(hOut, pos);
        cout << "       ";
        SetConsoleCursorPosition(hOut, pos);
        printf("    %-6d ", n--);
        Sleep(1000);
    }
    pos.X = 3, pos.Y = 11;
    SetConsoleCursorPosition(hOut, pos);
    cout << "Time over!\n";

    return 0;
}


發佈了42 篇原創文章 · 獲贊 15 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章