다음을 통해 공유


프린터 핸들을 제공하여 인쇄 최적화

Graphics 클래스의 생성자 중 하나는 디바이스 컨텍스트 핸들과 프린터 핸들을 받습니다. 특정 PostScript 프린터에 Windows GDI+ 명령을 보내면 해당 특정 생성자를 사용하여 Graphics 개체를 만들면 성능이 향상됩니다.

다음 콘솔 애플리케이션은 GetDefaultPrinter 를 호출하여 기본 프린터의 이름을 가져옵니다. 코드는 프린터 이름을 CreateDC 에 전달하여 프린터에 대한 디바이스 컨텍스트 핸들을 가져옵니다. 또한 코드는 프린터 이름을 OpenPrinter 에 전달하여 프린터 핸들을 가져옵니다. 디바이스 컨텍스트 핸들과 프린터 핸들은 모두 그래픽 생성자에 전달됩니다. 그런 다음 프린터에 두 개의 그림이 그려집니다.

참고

GetDefaultPrinter 함수는 Windows 2000 이상에서만 지원됩니다.

 

#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;
}