Windows Wizard Control Example

To create a wizard the PROPSHEETHEADER dwflas field must be set to one of the following values PSH_WIZARD97, PSH_WIZARD, PSH_WIZARD_LITE & PSH_WIZARD | PSH_AEROWIZARD. Setting one of these values means the dialog boxes will automatically form a first to last sequence

The following short program displays a simple wizard that contains 3 property pages. Right-clicking anywhere in the window initiates the wizard.

/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_DIALOG1 DIALOG DISCARDABLE 0, 0, 186, 90
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Sans Serif"
BEGIN
DEFPUSHBUTTON "OK",IDOK,129,7,50,14
PUSHBUTTON "Cancel",IDCANCEL,129,24,50,14
END
page1 DIALOG DISCARDABLE 0, 0, 150, 70
STYLE WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Page 1"
FONT 16, "MS Sans Serif"
BEGIN
LTEXT "First wizard page",-1,0,0,100,10
END
Page2 DIALOG DISCARDABLE 0, 0, 150, 70
STYLE WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Page 2"
FONT 16, "MS Sans Serif"
BEGIN
LTEXT "Second wizard page",-1,2,0,100,10
END
page3 DIALOG DISCARDABLE 0, 0, 150, 70
STYLE WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Page 3"
FONT 16, "MS Sans Serif"
BEGIN
LTEXT "Last wizard page",-1,2,0,100,10
END
/////////////////////////////////////////////////////////////////////////////
//Wizard example
#pragma comment( lib, "user32.lib" )
#pragma comment( lib, "comctl32.lib" )
#include <windows.h>
#include <commctrl.h>
#include "resource.h"

BOOL CALLBACK PageProc1(HWND hWnd , UINT msg , WPARAM wp , LPARAM lp) {
static int iCount = 0;

switch (msg) {
case WM_NOTIFY:
switch (((NMHDR *)lp)->code) {
//Notifies a page that the user has clicked the next button in a wizard.
case PSN_WIZNEXT:
iCount++;
break;
//Notifies a page that the user has clicked the back button in a wizard.
case PSN_WIZBACK:
if (iCount != 0) iCount--;
break;
//Notifies a page that the user has clicked the finish button in a wizard.
case PSN_WIZFINISH:
iCount = 0;
break;
}
if (iCount == 2)
PropSheet_SetFinishText(GetParent(hWnd) , TEXT("CLOSE"));
return TRUE;
}
return FALSE;
}

LRESULT CALLBACK WndProc(HWND hWnd , UINT msg , WPARAM wp , LPARAM lp) {
PROPSHEETPAGE psp;
PROPSHEETHEADER psh;
HPROPSHEETPAGE hPsp[3];

switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_CREATE:
InitCommonControls();
return 0;
case WM_RBUTTONUP:
//respond to right mouse click by creating wizard sheets
psp.dwSize = sizeof (PROPSHEETPAGE);
psp.dwFlags = PSP_DEFAULT | PSP_USEICONID;
psp.pszIcon = TEXT("PAGEICON");
psp.hInstance = (HINSTANCE)GetWindowLong(hWnd , GWL_HINSTANCE);

psp.pszTemplate = TEXT("Page1");
psp.pfnDlgProc = (DLGPROC)PageProc1;
hPsp[0] = CreatePropertySheetPage(&psp);

psp.pszTemplate = TEXT("Page2");
hPsp[1] = CreatePropertySheetPage(&psp);

psp.pszTemplate = TEXT("Page3");
hPsp[2] = CreatePropertySheetPage(&psp);

psh.dwSize = sizeof (PROPSHEETHEADER);
psh.dwFlags = PSH_DEFAULT | PSH_USEHICON | PSH_WIZARD;
psh.hwndParent = hWnd;
psh.hIcon = LoadIcon(NULL , IDI_ASTERISK);
psh.pszCaption = TEXT("Wizard Demo");
psh.nPages = 3;
psh.phpage = hPsp;
PropertySheet(&psh);
return 0;
}
return DefWindowProc(hWnd , msg , wp , lp);
}

int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance ,
PSTR lpCmdLine , int nCmdShow ) {
HWND hWnd;
MSG msg;
WNDCLASS winc;

winc.style = CS_HREDRAW | CS_VREDRAW;
winc.lpfnWndProc = WndProc;
winc.cbClsExtra = winc.cbWndExtra = 0;
winc.hInstance = hInstance;
winc.hIcon = LoadIcon(NULL , IDI_APPLICATION);
winc.hCursor = LoadCursor(NULL , IDC_ARROW);
winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
winc.lpszMenuName = NULL;
winc.lpszClassName = TEXT("myWindowClass");

if (!RegisterClass(&winc)) return -1;

hWnd = CreateWindow(
TEXT("myWindowClass") , TEXT("Wizard Demo") ,
WS_OVERLAPPEDWINDOW | WS_VISIBLE ,
CW_USEDEFAULT , CW_USEDEFAULT ,
CW_USEDEFAULT , CW_USEDEFAULT ,
NULL , NULL , hInstance , NULL
);
if (hWnd == NULL) return -1;

while(GetMessage(&msg , NULL , 0 , 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}