Solved

Finding the page width of default Printer

Posted on 2004-09-27
4
251 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 67
set up trigger (or something) to rename files (starting with ATL) in a linux directory 6 55
has77  challenge 9 88
unix example issues 18 98
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…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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