PageSetupDlg

The PageSetupDlg function displays the Windows Page Setup dialog box.

Syntax

BOOL PageSetupDlg(
  LPPSHPAGE a0
);

Parameters

Parameter Type Description
a0 LPPSHPAGE A pointer to a PSHPAGE structure that contains information used to initialize the dialog box and receive information about the user's selections. The lStructSize member of this structure must be set to sizeof(PSHPAGE).

Return Value

If the user clicks the OK button, the function returns a nonzero value, and the PSHPAGE structure pointed to by a0 contains the updated page setup information.

If the user clicks the Cancel button, the function returns zero. The global error handler CommDlgExtendedError can be called to retrieve more information about the error.

If the function fails for any other reason, it returns zero, and CommDlgExtendedError can be called to retrieve the error code.

Remarks

The PageSetupDlg function displays the Page Setup dialog box, which enables the user to select a printer and specify page orientation, paper source, and paper size.

The PSHPAGE structure can be used to specify:

This function requires that the common dialog box library (Comdlg32.dll) be loaded.

The PSHPAGE structure can also include flags that specify which controls are displayed in the dialog box. For example, you can use the PSD_PAGESETUPONLY flag to display only the page setup options and hide the printer selection options.

Example

The following code snippet demonstrates how to use PageSetupDlg:


#include <windows.h>
#include <commdlg.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    PSHPAGE psd;
    ZeroMemory(&psd, sizeof(PSHPAGE));
    psd.lStructSize = sizeof(PSHPAGE);
    psd.hInstance = hInstance;
    psd.hwndOwner = NULL; // Replace with your main window handle if available
    psd.Flags = PSD_INTHOUSANDSOFPOINTS | PSD_DEFAULTMINMARGINS | PSD_ALLPAGES;

    if (PageSetupDlg(&psd))
    {
        // User clicked OK. Process the updated page setup information.
        // psd.hDevMode contains information about the device
        // psd.hGlobalPrinterDC contains information about the printer DC

        // Example: Get paper size
        // POINT paperSize;
        // paperSize.x = psd.rcPaper.right - psd.rcPaper.left;
        // paperSize.y = psd.rcPaper.bottom - psd.rcPaper.top;
        // MessageBox(NULL, std::to_string(paperSize.x).c_str(), "Paper Width", MB_OK);

        // Remember to free the global memory for hDevMode and hGlobalPrinterDC if they are valid
        if (psd.hDevMode) GlobalFree(psd.hDevMode);
        if (psd.hGlobalPrinterDC) GlobalFree(psd.hGlobalPrinterDC);
    }
    else
    {
        // User clicked Cancel or an error occurred
        DWORD dwError = CommDlgExtendedError();
        // Handle error
    }

    return 0;
}
        

Requirements

Attribute Value
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header commdlg.h (include windows.h)
Library Comdlg32.lib
DLL Comdlg32.dll
Last modified: July 24, 2024