Solved

Finding the page width of default Printer

Posted on 2004-09-27
4
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ODBC Connection Logging, ADO.NET 6 71
sameEnds challenge 3 187
sum13 challenge 24 134
wordmultiple challenge 12 157
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.

726 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