CPrintDialog::GetDefaults
Retrieves the device defaults of the default printer without displaying a dialog box.
BOOL GetDefaults( );
Return Value
Nonzero if the function was successful; otherwise 0.
Remarks
The retrieved values are placed in the m_pd structure.
In some cases, a call to this function will call the constructor for CPrintDialog with bPrintSetupOnly set to FALSE. In these cases, a printer DC and hDevNames and hDevMode (two handles located in the m_pd data member) are automatically allocated.
If the constructor for CPrintDialog was called with bPrintSetupOnly set to FALSE, this function will not only return hDevNames and hDevMode (located in m_pd.hDevNames and m_pd.hDevMode) to the caller, but will also return a printer DC in m_pd.hDC. It is the responsibility of the caller to delete the printer DC and call the Windows GlobalFree function on the handles when you are finished with the CPrintDialog object.
Example
This code fragment gets the default printer's device context and reports to the user the resolution of the printer in dots per inch. (This attribute of the printer's capabilities is often referred to as DPI.)
CPrintDialog dlg(FALSE);
if (!dlg.GetDefaults())
{
AfxMessageBox(_T("You have no default printer!"));
}
else
{
// attach to the DC we were given
CDC dc;
dc.Attach(dlg.m_pd.hDC);
// ask for the measurements
int nHorz = dc.GetDeviceCaps(LOGPIXELSX);
int nVert = dc.GetDeviceCaps(LOGPIXELSY);
// almost always the same in both directions, but sometimes not!
CString str;
if (nHorz == nVert)
{
str.Format(_T("Your printer supports %d pixels per inch"), nHorz);
}
else
{
str.Format(_T("Your printer supports %d pixels per inch ")
_T("horizontal resolution, and %d pixels per inch vertical ")
_T("resolution"), nHorz, nVert);
}
// tell the user
AfxMessageBox(str);
// Note: no need to call Detach() because we want the CDC destructor
// to call FreeDC() on the DC we borrowed from the common dialog
}
Requirements
Header: afxdlgs.h