BOOL PrintDlgEx(LPPRINTDLGEX pPrintDlgEx);
The PrintDlgEx function displays the extended Print dialog box. This function is the extended version of the PrintDlg function and allows for greater customization and control over the print dialog.
pPrintDlgExPRINTDLGEX structure that contains information used to initialize the dialog box and receive information from it.
If the user clicks the OK button, the return value is TRUE. Otherwise, it is FALSE.
To get extended error information, call GetLastError.
The PrintDlgEx function is designed to be flexible and support various print-related operations. It allows developers to:
The PRINTDLGEX structure is central to the operation of PrintDlgEx. It contains fields for:
PRINTDLGEX StructureThe PRINTDLGEX structure is used with the PrintDlgEx function.
typedef struct tagPrintDlgEx {
DWORD cbSize;
HWND hwndOwner;
HANDLE hDevMode;
HANDLE hDevNames;
HDC hDC;
DWORD Flags;
DWORD ExFlags;
LPUNKNOWN lpfnSetupCallBack;
LPUNKNOWN lpfnPrintCallBack;
LPCSTR lpszName;
DWORD nCopies;
DWORD nFromPage;
DWORD nToPage;
DWORD nMinPage;
DWORD nMaxPage;
DWORD nCopies;
HINSTANCE hInstance;
LPCSTR lpszCaption;
LPVOID pPrintParent;
LPCSTR lpszPrintTemplateName;
LPVOID pDevMode;
LPVOID pDevNames;
} PRINTDLGEX, *LPPRINTDLGEX;
Fields:
cbSizehwndOwnerNULL if the dialog box has no owner.hDevModeDEVMODE structure that specifies the device mode for the device identified by the hDevNames member. If this member is NULL, the system uses the device mode information associated with the default printer.hDevNamesDEVNAMES structure that specifies the properties of the printer driver, device, and output port. If this member is NULL, the system uses the printer name information associated with the default printer.hDCNULL, the system creates a device context. It is recommended to set this to NULL and let the system create the DC.FlagsExFlagslpfnSetupCallBacklpfnPrintCallBacklpszNameNULL, the default printer is used.nCopiesnFromPagenToPagenMinPagenMaxPagehInstancelpszCaptionpPrintParentlpszPrintTemplateNamepDevModeDEVMODE structure.pDevNamesDEVNAMES structure.Here's a simplified example demonstrating how to call PrintDlgEx:
#include <windows.h>
#include <commdlg.h>
#include <stdio.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
PRINTDLGEX pd;
HPROPSHEETPAGE ahpage[1];
HPROPSHEETPAGE *lphpage = ahpage;
INT nReturn;
LPPROPSHEETPAGE lpPage = NULL;
ZeroMemory(&pd, sizeof(pd));
pd.dwSize = sizeof(pd);
pd.Flags = PD_ALLPAGES | PD_USEDIALOGEX; // Use extended dialog, print all pages
pd.nMinPage = 1;
pd.nMaxPage = 100;
pd.nCopies = 1;
pd.hInstance = hInstance;
pd.lpszCaption = L"Select Printer Options";
// If you have custom pages, you would populate lphpage here.
// For this example, we assume no custom pages.
pd.nPageRanges = 1;
pd.nFromPage = 1;
pd.nToPage = 1;
nReturn = PrintDlgEx(&pd);
if (nReturn == IDOK) {
// User clicked OK. Retrieve print job information.
// You would typically use pd.hDevMode and pd.hDevNames
// to get printer and device information.
wprintf(L"Printer selected: %s\n", (LPWSTR)GlobalLock(pd.hDevNames) + ((LPDEVMODEW)GlobalLock(pd.hDevMode))->dmDeviceName);
wprintf(L"Copies: %d\n", pd.nCopies);
if (pd.Flags & PD_PAGECOLORS) {
wprintf(L"Page Colors selected.\n");
}
if (pd.Flags & PD_COLLATE) {
wprintf(L"Collate selected.\n");
}
GlobalUnlock(pd.hDevNames);
GlobalUnlock(pd.hDevMode);
// Now you would typically start a print job using the retrieved information.
} else if (nReturn == IDCANCEL) {
// User clicked Cancel
MessageBox(NULL, L"Print operation cancelled.", L"Cancelled", MB_OK);
} else {
// Some error occurred
MessageBox(NULL, L"An error occurred during the print dialog.", L"Error", MB_OK);
}
return 0;
}
hDevMode and hDevNames members to NULL and allow the system to create and manage these structures.Flags member can be set to PD_USEDIALOGEX to indicate that the extended dialog box should be used.lpszPrintTemplateName member.