Link to home
Start Free TrialLog in
Avatar of ccm_paper
ccm_paper

asked on

Finding the page width of default Printer



Hi all,
I need to find the width of the paper loaded in the user's default printer.
The printer may be a local one or a network printer. The width to be found
should be available even if user specifies paper of custom size.

Thanks in advance!
Avatar of AlexFM
AlexFM

Try this:

   PRINTDLG pd;
   pd.lStructSize=(DWORD)sizeof(PRINTDLG);
   BOOL bRet=AfxGetApp()->GetPrinterDeviceDefaults(&pd);
   if(bRet)
   {
      // protect memory handle with ::GlobalLock and ::GlobalUnlock
      DEVMODE FAR *pDevMode=(DEVMODE FAR *)::GlobalLock(m_hDevMode);

      // test here pDevMode->dmPaperSize and pDevMode->dmPaperWidth


      ::GlobalUnlock(m_hDevMode);
   }

See also MSDN topics DEVMODE, CWinApp::GetPrinterDeviceDefaults.
You need a printer DC from your default printer, and the use GetDeviceCaps:

int width = GetDeviceCaps(hPrtDC,PHYSICALWIDTH) / GetDeviceCaps(hPrtDC,LOGPIXELSX) * 1000;
int height = GetDeviceCaps(hPrtDC,PHYSICALHEIGHT) / GetDeviceCaps(hPrtDC,LOGPIXELSY) * 1000;
Here is a better code I have tested:

CPrintDialog dlg(FALSE);
dlg.GetDefaults();
HDC hPrtDC = dlg.m_pd.hDC;

// Page dimensions in inches
double width = (double)GetDeviceCaps(hPrtDC,PHYSICALWIDTH) / GetDeviceCaps(hPrtDC,LOGPIXELSX);
double height = (double)GetDeviceCaps(hPrtDC,PHYSICALHEIGHT) / GetDeviceCaps(hPrtDC,LOGPIXELSY);
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial