Solved

Get the HDC for the printer

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

747 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

12 Experts available now in Live!

Get 1:1 Help Now