PrintDlg Function (Win32)
ComDlg32.dll
Note: The Common Dialog Box Library (Comdlg32.dll) provides a standard set of dialog boxes that applications can use to perform common tasks, such as opening files, printing, and selecting fonts. These dialog boxes ensure a consistent user experience across Windows applications.
Syntax
BOOL PrintDlg(
LPPRINTDLG lpPrintDlg
);
Parameters
Parameter | Description |
---|---|
lpPrintDlg |
A pointer to a PRINTDLG structure that contains information used to initialize the Print dialog box and receive information about the user's selection. |
Return Value
If the user clicks the OK button, the return value is nonzero. If the user clicks the Cancel button or an error occurs, the return value is zero. To get extended error information, call GetLastError
.
Remarks
The PrintDlg
function displays the Print dialog box. The structure pointed to by lpPrintDlg
is used to specify the initialization parameters and to receive the user's selections.
Before calling PrintDlg
, you must initialize the PRINTDLG
structure:
- Set the
lStructSize
member to the size of the structure. - Set the
hwndOwner
member to the handle of the owner window. - Set the
hInstance
member to the instance handle of the application. - Set the
Flags
member to specify the options for the dialog box. For example, to display the printer selection page, setPD_PRINTTOFILE
. - If you want to provide a custom template for the dialog box, set the
lpPrintTemplateName
member to the name of the template.
After PrintDlg
returns, you can examine the PRINTDLG
structure to retrieve the user's selections. For example, the hDevMode
and hDevName
members contain handles to global memory objects that hold the device mode and device name information, respectively.
Example
The following code snippet demonstrates how to initialize and display the Print dialog box:
#include <windows.h>
#include <commdlg.h>
// ...
PRINTDLG pd;
ZeroMemory(&pd, sizeof(pd));
pd.lStructSize = sizeof(pd);
pd.hwndOwner = GetActiveWindow();
pd.Flags = PD_ALLPAGES | PD_USESELECTION | PD_NOPAGENUMBERS | PD_PRINTTOFILE;
pd.nCopies = 1;
pd.nFromPage = 0;
pd.nToPage = 0;
if (PrintDlg(&pd) == TRUE) {
// User clicked OK.
// Use pd.hDevMode and pd.hDevNames to get printer settings.
// You would typically pass these to your printing code.
// Free the memory allocated by PrintDlg
GlobalFree(pd.hDevMode);
GlobalFree(pd.hDevNames);
} else {
// User cancelled or an error occurred.
DWORD dwError = GetLastError();
if (dwError) {
// Handle error
}
}