How to draw a progress bar?

Asked

Viewed 404 times

0

Hello. I am using the following code to draw the window background and progress bar:

case WM_PAINT: {
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hWnd, &ps);

    BITMAP bm;
    HDC hdcMem = CreateCompatibleDC(hdc);
    HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, imagemdeteste);
    GetObject(imagemdeteste, sizeof(bm), &bm);

    BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, hbmOld);
    DeleteDC(hdcMem);

    SelectObject(hdc, GetStockObject(DC_PEN));
    SetDCPenColor(hdc, RGB(75, 75, 75));
    SelectObject(hdc, GetStockObject(NULL_BRUSH));
    Rectangle(hdc, 0, 0, WindowSize.cx, WindowSize.cy);

    /* AQUI COMEÇA O DESENHO DA BARRA DE PROGRESSO */
    SelectObject(hdc, GetStockObject(DC_PEN));
    SetDCPenColor(hdc, RGB(75, 75, 75));
    SelectObject(hdc, GetStockObject(NULL_BRUSH));
    Rectangle(hdc,
        ProgressBarLocation.x, ProgressBarLocation.y,
        ProgressBarLocation.x + ProgressBarSize.cx, ProgressBarLocation.y + ProgressBarSize.cy
    );

    SelectObject(hdc, GetStockObject(DC_PEN));
    SetDCPenColor(hdc, RGB(75, 75, 75));
    SelectObject(hdc, GetStockObject(DC_BRUSH));
    SetDCBrushColor(hdc, RGB(75, 75, 75));
    Rectangle(hdc,
        (ProgressBarLocation.x + 1) + iProgressBarPadding, (ProgressBarLocation.y + 1) + iProgressBarPadding,
        (ProgressBarLocation.x + 1) + iProgressBarPadding + (((ProgressBarSize.cx - (2 * (1 + iProgressBarPadding))) * ProgressBarValue) / 100), (ProgressBarLocation.y - 1) - iProgressBarPadding + ProgressBarSize.cy
    );

    EndPaint(hWnd, &ps);
}

To update the progress bar I use the following code:

ProgressBarValue = QUALQUER VALOR DE 0 A 100;
RedrawWindow(hWnd, 0, 0, RDW_INVALIDATE);

This code will call WM_PAINT and I would be drawing the whole window again, even the background. I would like to change only the rectangles used to make the progress bar. Or this is how it is done?

What’s the right way to do it?

  • 2

    The way you are using above is manual (you even draw the rectangle that is used as progress bar), but there is another way using PROGRESS_CLASS which is a control of Windows itself and which is much easier (Just suggestion).

  • Thank you. I will search on PROGRESS_CLASS.

  • 1

    Hello, at the end of the page http://zetcode.com/gui/winapi/controlsIIII/ there is a good explanation of the use of PROGRESS_CLASS

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.