Solved

Finding the page width of default Printer

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now