在Windows C ++中更新进度栏

时间:2019-05-05 16:35:45

标签: c++ winapi

我用C ++创建了一个简单的Windows控制台程序,该程序进行一些处理,并创建一个带有进度条的窗口,该进度条会随着处理的进行而更新。问题在于进度条不会更新。

下面是我的示例应用程序的代码。

#include <iostream>
#include <stdlib.h>
#include "atlstr.h"
#include "pch.h"
#include <string.h>
#include "tchar.h"
#include <vector>
#define ISOLATION_AWARE_ENABLED 1
#include <windows.h>
#include <commctrl.h>

using namespace std;

#define WM_UPDATEPROGRESS WM_USER + 1

typedef struct {
    HWND    window;
    HWND    hwndPB;
} OPERATION_INFO;

void DoOperation(LPVOID lpThreadParams) {
    //reading ThreadParams
    OPERATION_INFO *pOperationInfo = (OPERATION_INFO *)lpThreadParams;
    //run a test loop that updates bar
    for (int i = 1; i <= 10; i += 1) {
        // do something
        Sleep(500); // dummily waiting some time
        // send message to update progress bar
        PostMessage(
            pOperationInfo->window, WM_UPDATEPROGRESS, 0, NULL);
    }
}

LONG_PTR CALLBACK WindowProcedure(
    HWND window, unsigned int msg, WPARAM wp, LPARAM lp)
{
    RECT rcClient;
    HWND hwndPB = NULL;
    OPERATION_INFO operationInfo;
    static bool flag_activate = FALSE;

    switch (msg)
    {
    case WM_CREATE:

        // create progress bar
        GetClientRect(window, &rcClient);
        hwndPB = CreateWindowEx(0, PROGRESS_CLASS,
            (LPCWSTR)NULL, (WS_CHILD | WS_VISIBLE),
            20,
            20,
            rcClient.right - rcClient.left - 40,
            20,
            window, (HMENU)0, GetModuleHandle(0), NULL);

        // progress bar settings
        SendMessage(hwndPB, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
        SendMessage(hwndPB, PBM_SETPOS, (WPARAM)0, 0);
        SendMessage(hwndPB, PBM_SETSTEP, (WPARAM)10, 0);

    case WM_ACTIVATEAPP:
        // if message already fired then do nothing:
        // because in this message we have code to initialize thread 
        // that updates progress bar and the thread initialization, 
        // obviously, must be done once.
        if (flag_activate)
            break;
        else
        {
            // store message MW_ACTIVEAPP fired
            flag_activate = TRUE;

            // initialize thread
            OPERATION_INFO operationInfo = { window, hwndPB };
            HANDLE threadHandle;
            // make thread call DoOperation routine
            threadHandle = CreateThread(
                NULL, 0, 
                (LPTHREAD_START_ROUTINE)DoOperation, 
                (LPVOID)&operationInfo, 0, 0);
        }
        break;

    case WM_UPDATEPROGRESS:
        //get info with HWND of the progress bar to update
        //update the progress bar
        SendMessage(hwndPB, PBM_STEPIT, NULL, 0);
        //release memory
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0L;

    case WM_LBUTTONDOWN:
        break;

    default:
        return DefWindowProc(window, msg, wp, lp);
    }
}

int main()
{
    // Window creation

    // structure initialization
    LPCWSTR myclass = L"myclass";
    WNDCLASSEX wndclass = { 
        sizeof(WNDCLASSEX), CS_DBLCLKS, 
        WindowProcedure, 0, 0, GetModuleHandle(0), 
        LoadIcon(0,IDI_APPLICATION), LoadCursor(0,IDC_ARROW), 
        HBRUSH(COLOR_WINDOW + 1), 0, myclass, 
        LoadIcon(0,IDI_APPLICATION) };
    // preliminar action
    if (RegisterClassEx(&wndclass))
    {
        // create window
        HWND window = CreateWindowEx(0, myclass, L"Processing",
            WS_OVERLAPPED | WS_SYSMENU,
            CW_USEDEFAULT, CW_USEDEFAULT,
            300, 150,
            0, 0, GetModuleHandle(0), 0);
        // if OK
        if (window)
        {
            // show window
            ShowWindow(window, SW_SHOWDEFAULT);
            MSG msg;
            while (GetMessage(&msg, 0, 0, 0)) 
                DispatchMessage(&msg);
        }
    }
}

我希望进度条每秒更新一次,但这不会发生:进度条什么也不做。

怎么了?

2 个答案:

答案 0 :(得分:1)

您的代码有两个类似的问题:

  1. 在WM_ACTIVATEAPP处理程序中,您正在分配CreateThread(...) lpParameter参数,要传递给线程的变量, 指向在堆栈上定义的本地结构的指针。尽快 else子句,调用结束于struct超出范围,并且 指针无效。

  2. 在WM_UPDATEPROGRESS处理程序中,您正在SendMessage-ing到 窗口句柄,它是未定义的变量。该变量仅在WM_CREATE处理程序中定义。

在真实代码中,您希望以更健壮的方式处理这些问题,但是为了使此代码达到您的期望,请将两个状态变量设为静态:

static HWND hwndPB = NULL;
static OPERATION_INFO operationInfo;

并修改WM_ACTIVATEAPP处理程序,以使其将指针传递给此静态变量,而不是其自身的本地OPERATION_INFO。

基本上,您需要记住WNDPROC是适用常规范围规则的常规C函数。它们不是任何种类的类定义。如果您想保持与某个窗口相关联的状态,而这种状态在调用其窗口过程的整个过程中都是很好的,则必须显式维护这种状态。

答案 1 :(得分:-2)

我遇到了同样的问题,但是当我在Delphi工作时。最可能的原因是处理需要花费大量时间,并且由于处理和钢筋更新是在同一线程上进行的,因此在处理完成之后钢筋会填满100%。

解决方案:尝试在单独的线程中更新进度栏。每秒(或您希望的次数)从主线程调用更新函数。