1 Attachment(s)
Progress Bar Not Updating
Hi, I have a Win32 c++ program in Visual Studio 2005.
My program creates a child dialog box, which displays a progress bar on a timer.
This timer is started when the SEND button is clicked.
For some strange reason, the progress bar doesn't update when I send the following windows message in my WM_TIMER handler:
SendMessage(hProgress, PBM_STEPIT, 0, 0 );
The timer works but that statement doesn't in that location. If I place this statement right when I set the progress bar range and step, then it works , but only as a one-shot. Why won't the progress bar update?
[CODE]
#include <windows.h>
#include <windowsx.h>
#include "Resource.h"
#include <commctrl.h>
#pragma comment(lib,"comctl32")
#define SEND_BUTTON 1000 /* control ID's */
#define ABORT_BUTTON 1001
#define RADIO1 1002
#define RADIO2 1003
#define BACK_BUTTON 1004
#define PROGRESS_TIMER 1005
#define DLG_TIMER 1006
int timer = 0;
//---------------------------------------------------------------------------
HWND hWnd;
HINSTANCE hInst;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK DlgAcknowledgedProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
hInst = hInstance;
DialogBox(hInst, MAKEINTRESOURCE(IDD_CONTROLS_DLG),
hWnd, reinterpret_cast<DLGPROC>(DlgProc));
return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
HWND hRad1, hRad2, hSend, hAbort, hProgress;
INITCOMMONCONTROLSEX InitCtrlEx;
InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCtrlEx.dwICC = ICC_PROGRESS_CLASS;
InitCommonControlsEx(&InitCtrlEx);
switch(Msg)
{
case WM_INITDIALOG:
hRad1 = CreateWindow ("BUTTON", "Auto Send 7 sec",
WS_CHILD | BS_AUTORADIOBUTTON,
20, 20, 150, 20,
hWndDlg, (HMENU)RADIO1, hInst, NULL) ;
hRad2 = CreateWindow ("BUTTON", "Manual Send",
WS_CHILD | BS_AUTORADIOBUTTON,
20, 40, 150, 20,
hWndDlg, (HMENU)RADIO2, hInst, NULL) ;
hSend = CreateWindow ("BUTTON", "SEND",
WS_CHILD | BS_DEFPUSHBUTTON,
250, 30, 80, 30,
hWndDlg, (HMENU)SEND_BUTTON, hInst, NULL) ;
hAbort = CreateWindow ("BUTTON", "ABORT",
WS_CHILD | BS_DEFPUSHBUTTON,
135, 155, 80, 30,
hWndDlg, (HMENU)ABORT_BUTTON, hInst, NULL) ;
// Button_Enable(hSend,FALSE); // Gray out a button
hProgress=CreateWindowEx(0, PROGRESS_CLASS, NULL,
WS_CHILD | WS_VISIBLE,
125, 130, 100, 20,
hWndDlg, NULL, hInst, NULL);
SendMessage(hProgress, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
SendMessage(hProgress,PBM_SETSTEP,(WPARAM) 10,0);
//SendMessage(hProgress, PBM_STEPIT, 0, 0 );
ShowWindow (hRad1, SW_SHOWNORMAL) ;
ShowWindow (hRad2, SW_SHOWNORMAL) ;
ShowWindow (hSend, SW_SHOWNORMAL) ;
ShowWindow (hAbort, SW_SHOWNORMAL) ;
ShowWindow (hProgress, SW_SHOWNORMAL) ;
/* start with radio button 1 checked */
SendMessage (hRad1, BM_SETCHECK, TRUE, 0);
ShowWindow(hWndDlg, SW_SHOW|SW_SHOWNORMAL);
UpdateWindow(hWndDlg);
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case RADIO1:
break ;
case RADIO2:
break ;
case ABORT_BUTTON:
DestroyWindow (hWndDlg) ; /* terminate application */
break ;
case SEND_BUTTON:
SetTimer(hWndDlg,DLG_TIMER,1000,NULL);
timer = 0;
break ;
}
break;
case WM_TIMER:
switch(wParam)
{
case DLG_TIMER:
if(timer == 7)
{
SendMessage(hProgress,PBM_SETPOS,0,0);
timer = 0;
}
else
{
SendMessage(hProgress, PBM_STEPIT, 0, 0 );
timer++;
}
break;
}
break;
}
return FALSE;
}
LRESULT CALLBACK DlgAcknowledgedProc(HWND hWndDlg2, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
HWND hBack;
switch(Msg)
{
case WM_INITDIALOG:
hBack = CreateWindow ("BUTTON", "BACK",
WS_CHILD | BS_DEFPUSHBUTTON,
135, 155, 80, 30,
hWndDlg2, (HMENU)BACK_BUTTON, hInst, NULL) ;
ShowWindow (hBack, SW_SHOWNORMAL) ;
ShowWindow(hWndDlg2, SW_SHOW|SW_SHOWNORMAL);
UpdateWindow(hWndDlg2);
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case BACK_BUTTON:
EndDialog(hWndDlg2, 0);
return TRUE;
}
break;
}
return FALSE;
}
[\CODE]
Re: Progress Bar Not Updating
Your hProgress variable is a stack variable for the DlgProc function. For all cases other than WM_INITDIALOG, it is not initialized to a valid value. The only time it has proper value is as soon as you create it in WM_INITDIALOG and when that is over, got all subsequent messages, hProgress is junk. You could make it a static variable, or you can use GetDlgItem to get the HWND value each time.
Re: Progress Bar Not Updating
Thank you so much.
I completely forgot about the scope of my progress bar.
Yes, I think static is appropriate.