Condividi tramite


Ottimizzazione della stampa fornendo un handle di stampante

Uno dei costruttori per la classe Graphics riceve un handle del contesto di dispositivo e un handle di stampante. Quando si inviano comandi GDI+ di Windows a determinate stampanti PostScript, le prestazioni saranno migliori se si crea l'oggetto Graphics con quel particolare costruttore.

L'applicazione console seguente chiama GetDefaultPrinter per ottenere il nome della stampante predefinita. Il codice passa il nome della stampante a CreateDC per ottenere un handle del contesto di dispositivo per la stampante. Il codice passa anche il nome della stampante a OpenPrinter per ottenere un handle di stampante. Sia l'handle del contesto di dispositivo che l'handle della stampante vengono passati al costruttore Graphics . Poi due figure vengono disegnate sulla stampante.

Nota

La funzione GetDefaultPrinter è supportata solo in Windows 2000 e versioni successive.

 

#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;

INT main()
{
   // Initialize GDI+.
   GdiplusStartupInput gdiplusStartupInput;
   ULONG_PTR gdiplusToken;
   GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

   DWORD   size;
   HDC     hdcPrint;
   HANDLE  printerHandle;

   DOCINFO docInfo;
   ZeroMemory(&docInfo, sizeof(docInfo));
   docInfo.cbSize = sizeof(docInfo);
   docInfo.lpszDocName = "GdiplusPrint";

   // Get the length of the printer name.
   GetDefaultPrinter(NULL, &size);
   TCHAR* buffer = new TCHAR[size];

   // Get the printer name.
   if(!GetDefaultPrinter(buffer, &size))
   {
      printf("Failure");
   }
   else
   {
      // Get a device context for the printer.
      hdcPrint = CreateDC(NULL, buffer, NULL, NULL);

      // Get a printer handle.
      OpenPrinter(buffer, &printerHandle, NULL);

      StartDoc(hdcPrint, &docInfo);
      StartPage(hdcPrint);
         Graphics* graphics = new Graphics(hdcPrint, printerHandle);
         Pen* pen = new Pen(Color(255, 0, 0, 0));
         graphics->DrawRectangle(pen, 200, 500, 200, 150);
         graphics->DrawEllipse(pen, 200, 500, 200, 150);
         delete(pen);
         delete(graphics);
      EndPage(hdcPrint);
      EndDoc(hdcPrint);

      ClosePrinter(printerHandle);
      DeleteDC(hdcPrint);
   }

   delete buffer;
   
   GdiplusShutdown(gdiplusToken);
   return 0;
}