Solved

Finding the page width of default Printer

Posted on 2004-09-27
4
249 Views
Last Modified: 2013-11-20


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!
0
Comment
Question by:ccm_paper
  • 3
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 12160209
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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12160342
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;
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12160666
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);
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 12160687
According to MSDN documentation, you must free the dc after using it:

::DeleteDC(m_pd.hDC);
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question