Win32 Progress Bar

Hey guys!

I need to know how to handle a progress bar. Creating one is easy with the VS 2010 Toolbox.
But I can't get it working. Most tutorials I found were for MFC, I dont want to use that.
So, I need to know how to send commands to the progress bar, eg. move one Step forward.
I found this: http://msdn.microsoft.com/en-us/library/bb760818%28VS.85%29.aspx concerning Control Commands; so I can figure that out by myself.
Ive tried using the SendMessage(); but I seem to have missed something:
SendMessage(HWND(IDC_PROGRESS1), PBM_SETPOS, 1, 0); for example gets an error that "PBM_SETPOS" is undefined.
Any help would be greaty appreciated!
Did you include commctrl.h?
I must have missed that one, I've included it now.
Still, calling the function produces no result, the bar remains empty.
Code Snippet:

1
2
3
4
5
6
7
8
case IDC_BUTTON3:

SetDlgItemText(hDlg, IDC_STATICg1, (LPCWSTR)L"IDC_BUTTON3");

SendMessage(HWND(IDC_PROGRESS1), PBS_SMOOTH, 0, 0);
SendMessage(HWND(IDC_PROGRESS1), PBM_SETSTEP,10, 0); 
SendMessage(HWND(IDC_PROGRESS1), PBM_STEPIT,0, 0);
return (INT_PTR)TRUE;
HWND(IDC_PROGRESS1)
This doesn't convert your control ID to HWND. For this you have to use GetDlgItem:
SendMessage(GetDlgItem(hDlg,IDC_PROGRESS1), PBS_SMOOTH, 0, 0);
Last edited on
I tried that; the program compiles without errors, yet when I hit the button a runtime error occurs:
Unhandled exception at 0x71906edd in Test07.exe: 0xC0000005: Access violation reading location 0x00000020.
Last edited on
Can you post more code?
Sure, this is the main .cpp file. Includes are in the stdafx.h file.
The buttons etc. are in a .rc file.
Im using this program to test stuff; a part of the code is generated by VS 2010 Prof.

main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// test07.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "test07.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);


	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_TEST07, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEST07));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST07));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_TEST07);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
	
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

BOOL WINAPI SetDlgItemText(__in  HWND hDlg, __in  int nIDDlgItem, __in  LPCTSTR lpString);
void WINAPI Sleep(__in  DWORD dwMilliseconds);

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:

		switch (LOWORD(wParam))
		{
		case IDC_BUTTON1:

			SetDlgItemText(hDlg, IDC_STATICt1, (LPCWSTR)L"testing...1");
			Sleep(1000);
			SetDlgItemText(hDlg, IDC_STATICt1, (LPCWSTR)L"testing...2");

			return (INT_PTR)TRUE;

		case IDC_BUTTON3:

			SetDlgItemText(hDlg, IDC_STATICg1, (LPCWSTR)L"IDC_BUTTON3!!");

			SendMessage(GetDlgItem(hDlg,IDC_PROGRESS1), PBS_SMOOTH, 0, 0);
			SendMessage(GetDlgItem(hDlg,IDC_PROGRESS1), PBM_SETSTEP, 10, 0);
			SendMessage(GetDlgItem(hDlg,IDC_PROGRESS1), PBM_STEPIT, 0, 0);
			
			return (INT_PTR)TRUE;

		case IDC_BUTTON4:

			SetDlgItemText(hDlg, IDC_STATICg1, (LPCWSTR)L"IDC_BUTTON4");
			return (INT_PTR)TRUE;
		
		case IDC_BUTTON2:

		EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;

		}
		
		return (INT_PTR)TRUE;
		break;
	}
	return (INT_PTR)FALSE;
}


stdafx.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>

// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <commctrl.h>

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // some CString constructors will be explicit

#include <atlbase.h>
#include <atlstr.h>
Line 202 should be
SendMessage(GetDlgItem(hDlg,IDC_PROGRESS1), PBS_SMOOTH, 0, 1);
Last edited on
I'm still getting an error...I tracked it down now.
-hDlg 0x002203b0 {unused=??? }	HWND__ *
unused	CXX0030: Error: expression cannot be evaluated	


Found this about it: http://msdn.microsoft.com/en-us/library/360csw6a%28v=VS.100%29.aspx
Does this error refer to line 202? Is this a compiler or a debugger error?
Try SendDlgItemMessage(hDlg,IDC_PROGRESS1, PBS_SMOOTH, 0, 1); (although this shouldn't make any difference)

I never used VS so I can't really help here.
Its a runtime error.
I tried executing the programm without VS running; it crashed when I clicked the button.
This is the Win7 Error Report:

Problem signature:
  Problem Event Name:	APPCRASH
  Application Name:	Window Operations.exe
  Application Version:	0.0.0.0
  Application Timestamp:	4ccaf5ca
  Fault Module Name:	comctl32.dll  // <-- Im guessing this Dynamic Link Library is the problem...
  Fault Module Version:	5.82.7600.16661
  Fault Module Timestamp:	4c6f635d
  Exception Code:	c0000005
  Exception Offset:	00006edd
  OS Version:	6.1.7600.2.0.0.256.1
  Locale ID:	**** //hidden for safety
  Additional Information 1:	0a9e
  Additional Information 2:	0a9e372d3b4ad19135b953a78882e789
  Additional Information 3:	0a9e
  Additional Information 4:	0a9e372d3b4ad19135b953a78882e789


VS Debugger stops running with
 Unhandled exception at 0x71786edd in Window Operations.exe: 0xC0000005: Access violation reading location 0x00000021.
and stops executing before line 169.
Last edited on
How about setting PBS_SMOOTH style in the resource file?
CONTROL "", IDC_PROGRESS1, PROGRESS_CLASS, PBS_SMOOTH, xpos, ypos, width, height

SendDlgItemMessage(hDlg,IDC_PROGRESS1, PBS_SMOOTH, 0, 1) is wrong because 2nd parameter must be a message code, not a window style . (I spotted this just now, sorry) . I know that to set styles you need to use SetindowLong function but I'm not sure how.
Okay, thanks. I did what you said and set PBS-SMOOTH in the resource file and used these commands to control the message bar.

1
2
SendDlgItemMessage(hDlg,IDC_PROGRESS1, PBM_SETSTEP, 10, 1);
SendDlgItemMessage(hDlg,IDC_PROGRESS1, PBM_STEPIT, 0, 1);

This works!! :)
As for SetWindowLong; I found this on MSDN: http://msdn.microsoft.com/en-us/library/ms644898%28v=VS.85%29.aspx

THANKS Null!!!!!
Topic archived. No new replies allowed.