To create a progress bar control specify PROGRESS_CLASS as the window class and register the class by specifying the ICC_PROGRESS_CLASS bit flag in the accompanying INITCOMMONCONTROLSEX structure.
//progress bar example
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#define ID_TIMER 2
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void CreateControls(HWND);
HWND hwndPrgBar;
HWND hbtn;
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PWSTR lpCmdLine, int nCmdShow) {
HWND hwnd;
MSG msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT("Progress bar");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);
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 int c = 0;//counter
switch(msg) {
case WM_CREATE:
CreateControls(hwnd);
break;
//Used to respond to timer function after specified interval
case WM_TIMER:
//Advances the current position for a progress bar by the step increment and redraws the bar to reflect the new position
SendMessage(hwndPrgBar, PBM_STEPIT, 0, 0);
c++;
if (c == 100) {
KillTimer(hwnd, ID_TIMER);
SendMessage(hbtn, WM_SETTEXT, (WPARAM) NULL, (LPARAM) TEXT("Start"));
c = 0;
}
break;
case WM_COMMAND:
if (c == 0)
{
c = 1;
SendMessage(hwndPrgBar, PBM_SETPOS, 0, 0);
SetTimer(hwnd, ID_TIMER, 5, NULL);
SendMessage(hbtn, WM_SETTEXT, (WPARAM) NULL, (LPARAM) TEXT("In progress"));
}
break;
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
void CreateControls(HWND hwnd) {
// load common control class ICC_PROGRESS_CLASS from the dynamic-link library (DLL).
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_PROGRESS_CLASS;
InitCommonControlsEx(&icex);
//create pager window
hwndPrgBar = CreateWindowEx(0, PROGRESS_CLASS, NULL, WS_CHILD | WS_VISIBLE | PBS_SMOOTH,30, 20, 190, 25, hwnd, NULL, NULL, NULL);
hbtn = CreateWindow(TEXT("Button"),TEXT( "Start"), WS_CHILD | WS_VISIBLE,85, 90, 85, 25, hwnd, NULL, NULL, NULL);
//set range of pager windows to 100
SendMessage(hwndPrgBar, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
//set step increment in page window progression
SendMessage(hwndPrgBar, PBM_SETSTEP, 1, 0);
}