Progress Bar

Progress bar là cửa sổ cho người dùng biết quá trình (cài đặt phần mềm, download tài liệu, quét virut,…) đã thực hiện được bao nhiêu % và còn lại bao nhiêu %.

#include <windows.h>
#include <commctrl.h>

#define ID_BUTTON 1
#define ID_TIMER 2

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hinst;

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HWND hwnd;
    MSG  msg ;
    WNDCLASS wc = {0};
    wc.lpszClassName = TEXT("Application");
    wc.hInstance     = hInstance ;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc   = WndProc ;
    wc.hCursor       = LoadCursor(0,IDC_ARROW);
    g_hinst = hInstance;

    RegisterClass(&wc);
    hwnd = CreateWindow(wc.lpszClassName, TEXT("Progress bar"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 260, 170, 0, 0, hInstance, 0);
    while( GetMessage(&msg, NULL, 0, 0)) 
    {
        DispatchMessage(&msg);
    }
    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    static HWND hwndPrgBar;
    static int i = 1;
    INITCOMMONCONTROLSEX InitCtrlEx;
    InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
    InitCtrlEx.dwICC  = ICC_PROGRESS_CLASS;
    InitCommonControlsEx(&InitCtrlEx);

    switch(msg)
    {
    case WM_CREATE:
        hwndPrgBar = CreateWindowEx(0, PROGRESS_CLASS, NULL, WS_CHILD | WS_VISIBLE | PBS_SMOOTH, 30, 20, 190, 25, hwnd, NULL, g_hinst, NULL);
        CreateWindow(TEXT("button"), TEXT("Start"), WS_CHILD | WS_VISIBLE, 85, 90, 80, 25, hwnd, (HMENU) 1, g_hinst, NULL);
        SendMessage(hwndPrgBar, PBM_SETRANGE, 0, MAKELPARAM(0, 150));
        SendMessage(hwndPrgBar, PBM_SETSTEP, 1, 0 ); 
        break;
    case WM_TIMER:
        SendMessage( hwndPrgBar, PBM_STEPIT, 0, 0 );
        i++;
        if ( i == 150 )
            KillTimer(hwnd, ID_TIMER);
        break;
    case WM_COMMAND:
        i = 1;
        SendMessage( hwndPrgBar, PBM_SETPOS, 0, 0 );
        SetTimer(hwnd, ID_TIMER, 5, NULL);
        break;
    case WM_DESTROY:
        KillTimer(hwnd, ID_TIMER);
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

Giải thích:
Trong bài viết này, chúng ta tạo 1 progress bar và 1 button. Button dùng để start progress bar. Chúng ta có sử dụng 1 bộ timer (định thời gian) để update progress bar.

hwndPrgBar = CreateWindowEx(0, PROGRESS_CLASS, NULL, WS_CHILD | WS_VISIBLE | PBS_SMOOTH, 30, 20, 190, 25, hwnd, NULL, g_hinst, NULL);

Chúng ta tạo progress bar với tên cửa sổ PROGRESS_CLASS và với tham số PBS_SMOOTH

SendMessage(hwndPrgBar, PBM_SETRANGE, 0, MAKELPARAM(0, 150));
SendMessage(hwndPrgBar, PBM_SETSTEP, 1, 0 );

Hai câu lệnh này dùng để set phạm vi và bước nhảy cho progress bar.

i = 1;
SendMessage( hwndPrgBar, PBM_SETPOS, 0, 0 );
SetTimer(hwnd, ID_TIMER, 5, NULL);

Khi chúng ta ấn button “Start”, chúng ta set giá trị i = 1, set vị trí ban đầu cho progress bar, khởi tạo cho bộ timer. Theo chu kì, bộ timer sẽ gửi các message WM_TIMER tới procedure của HĐH Windows.

SendMessage( hwndPrgBar, PBM_STEPIT, 0, 0 );
i++;
if ( i == 150 )
    KillTimer(hwnd, ID_TIMER);

Trong suốt quá trình nhận message WM_TIMER, chúng ta sẽ update progress bar bằng cách gửi message PBM_STEPIT tới progress bar. Khi progress bar chạy hết, chúng ta sẽ kill timer.

Kết quả:

Progress bar

4 Comments on Progress Bar

  1. Chào bạn, Sao chương trình của mình khi có dùng #include “commctrl.h” thì nó bị lỗi không chạy được.

    Severity Code Description Project File Line Suppression State
    Error LNK2019 unresolved external symbol __imp__InitCommonControlsEx@4 referenced in function “long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)” (?WndProc@@YGJPAUHWND__@@IIJ@Z) Win32Project2 D:\TAI\Win32Project\Win32Project2\Source.obj 1

    Cám ơn ban.

Leave a Reply to VietVH Cancel reply