C++【观察系列】模拟进度条

#include <windows.h>
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;

HANDLE handle = GetStdHandle ( STD_OUTPUT_HANDLE );
COORD coord;
void SetPosition ( int x, int y ) { coord.X = x; coord.Y = y; SetConsoleCursorPosition ( handle, coord ); }
class Draw {
    public:
        void Load ( ) {
            CONSOLE_CURSOR_INFO info = { 1, 0 };
            SetConsoleCursorInfo ( handle, &info );
            system ( "mode con cols=100 lines=31" );
            system ( "color f0" );
            SetConsoleTitle ( TEXT ( "C2D" ) );
            HWND hWnd = GetConsoleWindow ( );
            RECT rc;
            GetWindowRect ( hWnd, &rc );
            SetWindowLongPtr ( hWnd, GWL_STYLE, GetWindowLong ( hWnd, GWL_STYLE )
                                             & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX );
            SetWindowPos ( hWnd, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, NULL );

        }
        void PrintRectangle ( int x, int y, int width, int height, int color ) {
            SetConsoleTextAttribute ( handle, color );
            for ( int i = 0; i < height; i++ ) {
                SetPosition ( x, ( y + i ) );
                cout << setw ( width ) << '\0';
            }
        }
        void PrintButton ( int x, int y, int width, int height, int color, string text ) {
            PrintRectangle ( x, y, width, height, color );
            PrintText ( x + ( ( width - text.length ( ) ) / 2 ), y + ( height / 2 ), text );
        }
        void PrintText ( int x, int y, string text ) {
            SetPosition ( x, y );
            cout << text;
        }
        void Signel ( ) {
            while ( 1 ) {
                while ( kbhit ( ) == 0) {
                    if ( getch ( ) == 13 ) {
                        Loding ( );
                    }
                }
            }
        }
        void Loding ( ) {
            PrintButton( 40, 15, 20, 3, 0x3f, "检测中" );
            PrintRectangle ( 25, 20, 50, 1, 0x80 );
            for ( int i = 0; i < 50; i ++ ) {
                PrintRectangle ( 25, 20, ( 1 + i ), 1, 0xc0 );
                Sleep ( 10 );
            }
            PrintButton ( 40, 15, 20, 3, 0x3f, "开始检测" );
            for ( int i = 0; i < 25; i++ ) {
                PrintRectangle ( 25, 20, ( 1 + i ), 1, 0xf0 );
                PrintRectangle ( 75 - 1 * i, 20, ( 1 + i ), 1, 0xf0 );
				Sleep( 1 );
            }
            PrintText ( 42, 20, "检测完成: 无异常" );
        }
};
int main ( ) {
    Draw draw;

    draw.Load ( );
    draw.PrintRectangle ( 0, 0, 101, 10, 0x30 );
    draw.PrintButton( 40, 15, 20, 3, 0x3f, "开始检测" );
    draw.Signel ( );

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值