Solved

Get the HDC for the printer

Posted on 2002-04-11
3
434 Views
Last Modified: 2008-02-01
Hi

How do you get the HDC for the default printer in windows ?
0
Comment
Question by:win32
[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
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6934067
You'd usually use 'PrintDlg()' to do this, e.g.

PRINTDLG pdlg;

ZeroMemory ( &pdlg, sizeof ( PRINTDLG));

pdlg.Flags = PD_RETURNDC | PD_RETURNDEFAULT;

PrintDlg ( &pdlg);

HDC hDC = pdlg.hDC;
0
 
LVL 32

Accepted Solution

by:
jhance earned 75 total points
ID: 6934199
To do it without need for the printer dialog...

HDC GetPrinterDC(void)
{
     DWORD dwNeeded, dwReturned;
     HDC hdc;
     PRINTER_INFO_5 *pinfo5;
     TCHAR *lpszPrinter;

     if(GetVersion() & 0x800000000){
          // Win9x
          EnumPrinters(PRINTER_ATTRIBUTE_DEFAULT, NULL, 5, NULL, 0, &dwNeeded, &dwReturned);
          pinfo5 = (PRINTER_INFO_5 *)malloc(dwNeeded);

          EnumPrinters(PRINTER_ATTRIBUTE_DEFAULT, NULL, 5, (PBYTE)pinfo5, dwNeeded, &dwNeeded, &dwReturned);

          hdc = CreateDC(NULL, pinfo5->pPrinterName, NULL, NULL);

          free(pinfo5);
     }
     else{
          // WinNT/2000/XP

          GetDefaultPrinter(NULL, &dwNeeded);
          lpszPrinter = new TCHAR[dwNeeded];
          GetDefaultPrinter(lpszPrinter, &dwNeeded);
          hdc = CreateDC(NULL, lpszPrinter, NULL, NULL);
          delete [] lpszPrinter;
     }

     return hdc;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 6934231
>>To do it without need for the printer dialog...

The dialog doesn't even show up using this method...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

756 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