J
Jister
Guest
Hello,
I'm developing an application using the win32 API and I'm having some trouble with the DocumentProperties() function. I have a function called Print() which prints various things depending on the options the user chose. Inside the Print() function I get a handle to the printer by calling PrintDlG(). The hDevMode member of the PRINTDLG structure is set to NULL so that PrintDlg() allocates memory for the DEVMODE structure, initialises its members and returns a handle to the structure.
When PrintDlg() returns I want to force landscape orientation for printing certain things. However, when I call DocumentProperties() after changing the page orientation to landscape the function returns -1. Here is the sample code:
// Fill out the PRINTDLG structure.
pd.lStructSize = sizeof(PRINTDLG);
pd.hwndOwner = hWnd;
pd.hDevMode = NULL;
pd.hDevNames = NULL;
pd.hDC = NULL;
pd.Flags = PD_ALLPAGES | PD_RETURNDC | PD_NOSELECTION;
pd.nFromPage = 0;
pd.nToPage = 0;
pd.nMinPage = 0;
pd.nMaxPage = 0;
pd.nCopies = 1;
pd.hInstance = NULL;
pd.lCustData = 0L;
pd.lpfnPrintHook = NULL;
pd.lpfnSetupHook = NULL;
pd.lpPrintTemplateName = NULL;
pd.lpSetupTemplateName = NULL;
pd.hPrintTemplate = NULL;
pd.hSetupTemplate = NULL;
// Invoke the Print common dialog box.
if (!PrintDlg(&pd))
return TRUE; // Return true if the user cancelled the Print dialog box. This stops the error message being displayed.
ptrDevMode = (LPDEVMODE)GlobalLock(pd.hDevMode);
if (ptrDevMode->dmOrientation == DMORIENT_PORTRAIT)
{
ptrDevMode->dmOrientation = DMORIENT_LANDSCAPE;
ptrDevMode->dmFields |= DM_ORIENTATION;
iError = DocumentProperties(hWnd, pd.hDC, ptrDevMode->dmDeviceName, ptrDevMode, ptrDevMode, DM_IN_BUFFER | DM_OUT_BUFFER);
if (iError < 0)
{
GlobalUnlock(pd.hDevMode);
wsprintf(szTCBuffer, _T("DocumentProperties() failed with error code %d"), iError);
MessageBox(NULL, szTCBuffer, _T("Print Error"), MB_ICONERROR | MB_OK);
return FALSE;
}
}
GlobalUnlock(pd.hDevMode);
I know I haven't followed the method described in the msdn documentation for using the DocumentProperties() function, but that's because I'm using the DEVMODE structure returned by PrintDlg().
I actually have tried using the method described in the msdn documentation, using a printer handle obtained from OpenPrinter() and manually typing in the name of the printer, and it succeeds returning 2224 bytes for the required size of the DEVMODE structure.
However, when using the printer handle and DEVMODE structure handle returned by PrintDlg(), DocumentProperties() always fails returning -1. So is it not possible to use DocumentProperties() with handles returned from PrintDlg() or am I doing something wrong?
Btw I'm fairly new to win32 programming.
Thanks for any help.
Continue reading...
I'm developing an application using the win32 API and I'm having some trouble with the DocumentProperties() function. I have a function called Print() which prints various things depending on the options the user chose. Inside the Print() function I get a handle to the printer by calling PrintDlG(). The hDevMode member of the PRINTDLG structure is set to NULL so that PrintDlg() allocates memory for the DEVMODE structure, initialises its members and returns a handle to the structure.
When PrintDlg() returns I want to force landscape orientation for printing certain things. However, when I call DocumentProperties() after changing the page orientation to landscape the function returns -1. Here is the sample code:
// Fill out the PRINTDLG structure.
pd.lStructSize = sizeof(PRINTDLG);
pd.hwndOwner = hWnd;
pd.hDevMode = NULL;
pd.hDevNames = NULL;
pd.hDC = NULL;
pd.Flags = PD_ALLPAGES | PD_RETURNDC | PD_NOSELECTION;
pd.nFromPage = 0;
pd.nToPage = 0;
pd.nMinPage = 0;
pd.nMaxPage = 0;
pd.nCopies = 1;
pd.hInstance = NULL;
pd.lCustData = 0L;
pd.lpfnPrintHook = NULL;
pd.lpfnSetupHook = NULL;
pd.lpPrintTemplateName = NULL;
pd.lpSetupTemplateName = NULL;
pd.hPrintTemplate = NULL;
pd.hSetupTemplate = NULL;
// Invoke the Print common dialog box.
if (!PrintDlg(&pd))
return TRUE; // Return true if the user cancelled the Print dialog box. This stops the error message being displayed.
ptrDevMode = (LPDEVMODE)GlobalLock(pd.hDevMode);
if (ptrDevMode->dmOrientation == DMORIENT_PORTRAIT)
{
ptrDevMode->dmOrientation = DMORIENT_LANDSCAPE;
ptrDevMode->dmFields |= DM_ORIENTATION;
iError = DocumentProperties(hWnd, pd.hDC, ptrDevMode->dmDeviceName, ptrDevMode, ptrDevMode, DM_IN_BUFFER | DM_OUT_BUFFER);
if (iError < 0)
{
GlobalUnlock(pd.hDevMode);
wsprintf(szTCBuffer, _T("DocumentProperties() failed with error code %d"), iError);
MessageBox(NULL, szTCBuffer, _T("Print Error"), MB_ICONERROR | MB_OK);
return FALSE;
}
}
GlobalUnlock(pd.hDevMode);
I know I haven't followed the method described in the msdn documentation for using the DocumentProperties() function, but that's because I'm using the DEVMODE structure returned by PrintDlg().
I actually have tried using the method described in the msdn documentation, using a printer handle obtained from OpenPrinter() and manually typing in the name of the printer, and it succeeds returning 2224 bytes for the required size of the DEVMODE structure.
However, when using the printer handle and DEVMODE structure handle returned by PrintDlg(), DocumentProperties() always fails returning -1. So is it not possible to use DocumentProperties() with handles returned from PrintDlg() or am I doing something wrong?
Btw I'm fairly new to win32 programming.
Thanks for any help.
Continue reading...