Solved

Get the HDC for the printer

Posted on 2002-04-11
3
429 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

831 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