Using the Page Setup Common Dialog Box

Page Setup is a new common dialog box for Windows 95. (In earlier versions of Windows, the dialog template for the Print common dialog box included the code for page setup.) The Page Setup common dialog box, which is shown in Figure 6-15, allows the user to set the paper size, paper source, document orientation, and margins for printing. The sample representation of the page at the top of the dialog box gives the user an idea of what the printed output will look like.

Figure 6-15.

The Page Setup common dialog box.

You can customize the Page Setup dialog box to use a hook function to paint the sample page. To use the hook function, the application must specify the PSD_ENABLEPAGEPAINTHOOK flag in the Flags member of the PAGESETUPDLG structure and the name of the hook function in the lpfnPagePaintHook member. This hook function will receive messages for all the steps in the drawing process. Table 6-1 lists the messages that are sent to the page setup hook in the order that the hook receives them.

Message Description
WM_PSD_PAGESETUPDLG Notifies the hook function to carry out initialization tasks.
WM_PSD_FULLPAGERECT Specifies the bounding rectangle of the sample page.
WM_PSD_MINMARGINRECT Specifies the minimum margin rectangle.
WM_PSD_MARGINRECT Specifies the margin rectangle.
WM_PSD_GREEKTEXTRECT Specifies the Greek-text rectangle.
WM_PSD_ENVSTAMPRECT Specifies the envelope-stamp rectangle (for envelopes only).

Table 6-1. Page setup messages.

From a programming standpoint, using the Page Setup common dialog box is very much like programming the other common dialog boxes: you fill out a structure and make a function call. The new structure provided for the Page Setup dialog box is PAGESETUPDLG. When you have filled out this structure, a call to PageSetupDlg with a pointer to the structure displays the dialog box. This code demonstrates how the CMNDLG32 sample fills out the structure and produces the Page Setup common dialog box:

void PageSetup (HWND hWnd)
{
// Initialize the PAGESETUPDLG structure.
psDlg.lStructSize = sizeof (PAGESETUPDLG);
psDlg.hwndOwner = hWnd;
psDlg.hDevMode = (HANDLE)NULL;
psDlg.hDevNames = (HANDLE)NULL;
psDlg.hInstance = (HANDLE)hInst;
psDlg.lCustData = (LPARAM)NULL;
psDlg.hPageSetupTemplate = (HGLOBAL)NULL;
psDlg.Flags = PSD_DEFAULTMINMARGINS | PSD_INHUNDREDTHSOFMILLIMETERS;

switch (wMode)
{
case IDM_STANDARD:
psDlg.lpfnPageSetupHook = (LPPAGESETUPHOOK)(FARPROC)NULL;
psDlg.lpPageSetupTemplateName = (LPTSTR)NULL;
psDlg.lpfnPagePaintHook = (LPPAGEPAINTHOOK)(FARPROC)NULL;
break;

case IDM_HOOK:
psDlg.Flags |= PSD_ENABLEPAGESETUPHOOK;
psDlg.lpfnPageSetupHook = (LPPAGESETUPHOOK)(FARPROC)PageSetupHook;
psDlg.lpPageSetupTemplateName = (LPTSTR)NULL;
psDlg.lpfnPagePaintHook = (LPPAGEPAINTHOOK)(FARPROC)NULL;
break;

case IDM_CUSTOM:
psDlg.Flags |= PSD_ENABLEPAGESETUPHOOK |
PSD_ENABLEPAGESETUPTEMPLATE;
psDlg.lpfnPageSetupHook = (LPPAGESETUPHOOK)(FARPROC)PageSetupHook;
psDlg.lpPageSetupTemplateName = (LPTSTR)PRNSETUPDLGORD95;
psDlg.lpfnPagePaintHook = (LPPAGEPAINTHOOK)(FARPROC)NULL;
break;
}

// Call the Page Setup common dialog procedure.
if (PageSetupDlg (&psDlg) == FALSE)
ProcessCDError (CommDlgExtendedError (), hWnd);
}

Like the Open and Save As common dialog boxes, the three printing common dialog boxes have new templates. Here's what they look like:

// Print dialog box
PRINTDLGORD DIALOG DISCARDABLE 32, 32, 288, 186
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU |
DS_CONTEXTHELP | DS_3DLOOK
CAPTION "Print"
FONT 8, "MS Sans Serif"
BEGIN
GROUPBOX "Printer",grp4,8,4,272,84,WS_GROUP
LTEXT "&Name:",stc6,16,20,36,8
COMBOBOX cmb4,52,18,152,152,CBS_DROPDOWNLIST | CBS_SORT |
WS_VSCROLL | WS_GROUP | WS_TABSTOP
PUSHBUTTON "&Properties",psh2,212,17,60,14,WS_GROUP
LTEXT "Status:",stc8,16,36,36,10,SS_NOPREFIX
LTEXT "",stc12,52,36,224,10,SS_NOPREFIX |
SS_LEFTNOWORDWRAP
LTEXT "Type:",stc7,16,48,36,10,SS_NOPREFIX
LTEXT "",stc11,52,48,224,10,SS_NOPREFIX |
SS_LEFTNOWORDWRAP
LTEXT "Where:",stc10,16,60,36,10,SS_NOPREFIX
LTEXT "",stc14,52,60,224,10,SS_NOPREFIX |
SS_LEFTNOWORDWRAP
LTEXT "Comment:",stc9,16,72,36,10,SS_NOPREFIX
LTEXT "",stc13,52,72,152,10,SS_NOPREFIX |
SS_LEFTNOWORDWRAP
CONTROL "Print to fi&le",chx1,"Button",BS_AUTOCHECKBOX |
WS_GROUP | WS_TABSTOP,212,70,64,12
GROUPBOX "Print range",grp1,8,92,144,64,WS_GROUP
CONTROL "&All",rad1,"Button",BS_AUTORADIOBUTTON | WS_GROUP |
WS_TABSTOP,16,106,64,12
CONTROL "Pa&ges",rad3,"Button",BS_AUTORADIOBUTTON,
16,122,36,12
CONTROL "&Selection",rad2,"Button",BS_AUTORADIOBUTTON,
16,138,64,12
RTEXT "&from:",stc2,52,124,20,8
EDITTEXT edt1,74,122,26,12,WS_GROUP | ES_NUMBER
RTEXT "&to:",stc3,100,124,16,8
EDITTEXT edt2,118,122,26,12,WS_GROUP | ES_NUMBER
GROUPBOX "Copies",grp2,160,92,120,64,WS_GROUP
LTEXT "Number of &copies:",stc5,168,108,68,8
EDITTEXT edt3,240,106,32,12,WS_GROUP | ES_NUMBER
ICON "",ico3,162,124,76,24,WS_GROUP | SS_CENTERIMAGE
CONTROL "C&ollate",chx2,"Button",BS_AUTOCHECKBOX |
WS_GROUP | WS_TABSTOP,240,130,36,12
DEFPUSHBUTTON "OK",IDOK,180,164,48,14,WS_GROUP
PUSHBUTTON "Cancel",IDCANCEL,232,164,48,14
END

// Print Setup dialog box
PRNSETUPDLGORD DIALOG DISCARDABLE 32, 32, 288, 178
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU |
DS_CONTEXTHELP | DS_3DLOOK
CAPTION "Print Setup"
FONT 8, "MS Sans Serif"
BEGIN
GROUPBOX "Printer",grp4,8,4,272,84,WS_GROUP
LTEXT "&Name:",stc6,16,20,36,8
COMBOBOX cmb1,52,18,152,152,CBS_DROPDOWNLIST | CBS_SORT |
WS_VSCROLL | WS_GROUP | WS_TABSTOP
PUSHBUTTON "&Properties",psh2,212,17,60,14,WS_GROUP
LTEXT "Status:",stc8,16,36,36,10,SS_NOPREFIX
LTEXT "",stc12,52,36,224,10,SS_NOPREFIX |
SS_LEFTNOWORDWRAP
LTEXT "Type:",stc7,16,48,36,10,SS_NOPREFIX
LTEXT "",stc11,52,48,224,10,SS_NOPREFIX |
SS_LEFTNOWORDWRAP
LTEXT "Where:",stc10,16,60,36,10,SS_NOPREFIX
LTEXT "",stc14,52,60,224,10,SS_NOPREFIX |
SS_LEFTNOWORDWRAP
LTEXT "Comment:",stc9,16,72,36,10,SS_NOPREFIX
LTEXT "",stc13,52,72,224,10,SS_NOPREFIX |
SS_LEFTNOWORDWRAP
GROUPBOX "Paper",grp2,8,92,164,56,WS_GROUP
LTEXT "Si&ze:",stc2,16,108,36,8
COMBOBOX cmb2,52,106,112,112,CBS_DROPDOWNLIST | CBS_SORT |
WS_VSCROLL | WS_GROUP | WS_TABSTOP
LTEXT "&Source:",stc3,16,128,36,8
COMBOBOX cmb3,52,126,112,112,CBS_DROPDOWNLIST | CBS_SORT |
WS_VSCROLL | WS_GROUP | WS_TABSTOP
GROUPBOX "Orientation",grp1,180,92,100,56,WS_GROUP
ICON "",ico1,195,112,18,20,WS_GROUP
CONTROL "P&ortrait",rad1,"Button",BS_AUTORADIOBUTTON |
WS_GROUP | WS_TABSTOP,224,106,52,12
CONTROL "L&andscape",rad2,"Button",BS_AUTORADIOBUTTON,
224,126,52,12
DEFPUSHBUTTON "OK",IDOK,180,156,48,14,WS_GROUP
PUSHBUTTON "Cancel",IDCANCEL,232,156,48,14
END

// Page Setup dialog box
PAGESETUPDLGORD DIALOG DISCARDABLE 32, 32, 240, 240
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION |
WS_SYSMENU | 0x4 | DS_CONTEXTHELP
CAPTION "Page Setup"
FONT 8, "MS Sans Serif"
BEGIN
CONTROL "",rct1,"Static",SS_WHITERECT | WS_GROUP,80,8,80,80
CONTROL "",rct2,"Static",SS_GRAYRECT | WS_GROUP,160,12,4,80
CONTROL "",rct3,"Static",SS_GRAYRECT | WS_GROUP,84,88,80,4
GROUPBOX "Paper",grp2,8,96,224,56,WS_GROUP
LTEXT "Si&ze:",stc2,16,112,36,8
COMBOBOX cmb2,64,110,160,160,CBS_DROPDOWNLIST | CBS_SORT |
WS_VSCROLL | WS_GROUP | WS_TABSTOP
LTEXT "&Source:",stc3,16,132,36,8
COMBOBOX cmb3,64,130,160,160,CBS_DROPDOWNLIST | CBS_SORT |
WS_VSCROLL | WS_GROUP | WS_TABSTOP
GROUPBOX "Orientation",grp1,8,156,64,56,WS_GROUP
CONTROL "P&ortrait",rad1,"Button",BS_AUTORADIOBUTTON |
WS_GROUP | WS_TABSTOP,16,170,52,12
CONTROL "L&andscape",rad2,"Button",BS_AUTORADIOBUTTON,
16,190,52,12
GROUPBOX "Margins",grp4,80,156,152,56,WS_GROUP
LTEXT "&Left:",stc15,88,172,32,8
EDITTEXT edt4,120,170,28,12,WS_GROUP
LTEXT "&Right:",stc16,164,172,32,8
EDITTEXT edt6,196,170,28,12,WS_GROUP
LTEXT "&Top:",stc17,88,192,32,8
EDITTEXT edt5,120,190,28,12,WS_GROUP
LTEXT "&Bottom:",stc18,164,192,32,8
EDITTEXT edt7,196,190,28,12,WS_GROUP
DEFPUSHBUTTON "OK",IDOK,80,220,48,14,WS_GROUP
PUSHBUTTON "Cancel",IDCANCEL,132,220,48,14
PUSHBUTTON "&Printer...",psh3,184,220,48,14
END