• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 445
  • Last Modified:

Get the HDC for the printer

Hi

How do you get the HDC for the default printer in windows ?
0
win32
Asked:
win32
  • 2
1 Solution
 
jkrCommented:
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
 
jhanceCommented:
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
 
jkrCommented:
>>To do it without need for the printer dialog...

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now