次の方法で共有


プリンター ハンドルを提供して印刷を最適化

Graphics クラスのコンストラクターの 1 つは、デバイス コンテキスト ハンドルとプリンター ハンドルを受け取ります。 特定の PostScript プリンターに Windows GDI+ コマンドを送信すると、その特定のコンストラクターを使用して Graphics オブジェクトを作成すると、パフォーマンスが向上します。

次のコンソール アプリケーションは 、GetDefaultPrinter を 呼び出して、既定のプリンターの名前を取得します。 このコードでは、プリンター名を CreateDC に渡して、プリンターのデバイス コンテキスト ハンドルを取得します。 また、このコードはプリンター名を OpenPrinter に渡して、プリンター ハンドルを取得します。 デバイス コンテキスト ハンドルとプリンター ハンドルの両方が Graphics コンストラクターに渡されます。 その後、プリンターに2つのフィギュアが描かれます。

注意

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