How to Change Default Printer Settings in an MFC ApplicationID: Q126897
|
To change the default printer settings in an MFC application, you must retrieve the system default settings in a CWinApp derived object and modify those defaults before a print job is invoked.
Default printer driver settings in an MFC application are maintained in the
CWinApp object. These settings take the form of the following two protected
member variables:
void CMyWinApp::SetLandscape()
{
// Get default printer settings.
PRINTDLG pd;
pd.lStructSize = (DWORD) sizeof(PRINTDLG);
if (GetPrinterDeviceDefaults(&pd))
{
// Lock memory handle.
DEVMODE FAR* pDevMode =
(DEVMODE FAR*)::GlobalLock(m_hDevMode);
LPDEVNAMES lpDevNames;
LPTSTR lpszDriverName, lpszDeviceName, lpszPortName;
HANDLE hPrinter;
if (pDevMode)
{
// Change printer settings in here.
pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
// Unlock memory handle.
lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
lpszDriverName = (LPTSTR )lpDevNames + lpDevNames->wDriverOffset;
lpszDeviceName = (LPTSTR )lpDevNames + lpDevNames->wDeviceOffset;
lpszPortName = (LPTSTR )lpDevNames + lpDevNames->wOutputOffset;
::OpenPrinter(lpszDeviceName, &hPrinter, NULL);
::DocumentProperties(NULL,hPrinter,lpszDeviceName,pDevMode,
pDevMode, DM_IN_BUFFER|DM_OUT_BUFFER);
// Sync the pDevMode.
// See SDK help for DocumentProperties for more info.
::ClosePrinter(hPrinter);
::GlobalUnlock(m_hDevNames);
::GlobalUnlock(m_hDevMode);
}
}
}
If you want landscape to be the default orientation for the application,
you would call this function before any print job was invoked. A good place
to do this would be in your CWinApp derived class's InitInstance()
function.
Additional query words: kbinf 1.52 2.00 2.10 2.52 3.00 3.10 printing
Keywords : kbcode kbMFC kbPrinting kbVC
Version : 1.50 1.51 1.52 | 1.00 2.00 2.10
Platform : NT WINDOWS
Issue type :
Last Reviewed: July 21, 1999