다음을 통해 공유


GDI+ 출력을 프린터로 보내기

Windows GDI+를 사용하여 프린터에 그리는 것은 GDI+를 사용하여 컴퓨터 화면에 그리는 것과 유사합니다. 프린터에서 그리려면 프린터의 디바이스 컨텍스트 핸들을 가져온 다음 해당 핸들을 Graphics 생성자에 전달합니다.

다음 콘솔 애플리케이션은 MyPrinter라는 프린터에 선, 사각형 및 줄임표를 그립니다.

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

   // Get a device context for the printer.
   HDC hdcPrint = CreateDC(NULL, TEXT("\\\\printserver\\print1"), NULL, NULL);

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

   StartDoc(hdcPrint, &docInfo);
   StartPage(hdcPrint);
      Graphics* graphics = new Graphics(hdcPrint);
      Pen* pen = new Pen(Color(255, 0, 0, 0));
      graphics->DrawLine(pen, 50, 50, 350, 550);
      graphics->DrawRectangle(pen, 50, 50, 300, 500);
      graphics->DrawEllipse(pen, 50, 50, 300, 500);
      delete pen;
      delete graphics;
   EndPage(hdcPrint);
   EndDoc(hdcPrint);
   
   DeleteDC(hdcPrint);
   GdiplusShutdown(gdiplusToken);
   return 0;
}

앞의 코드에서 세 가지 GDI+ 그리기 명령은 각각 프린터 디바이스 컨텍스트 핸들을 수신하는 StartDocEndDoc 함수에 대한 호출 사이에 있습니다. StartDoc와 EndDoc 사이의 모든 그래픽 명령은 임시 메타파일로 라우팅됩니다. EndDoc를 호출한 후 프린터 드라이버는 메타파일의 데이터를 사용 중인 특정 프린터에 필요한 형식으로 변환합니다.

메모

사용 중인 프린터에 스풀링을 사용할 수 없으면 그래픽 출력이 메타파일로 라우팅되지 않습니다. 대신 개별 그래픽 명령은 프린터 드라이버에서 처리한 다음 프린터로 전송됩니다.

 

일반적으로 이전 콘솔 응용 프로그램에서 수행한 대로 프린터 이름을 하드 코딩하지 않습니다. 이름을 하드 코딩하는 한 가지 대안은 GetDefaultPrinter 호출하여 기본 프린터의 이름을 가져오는 것입니다. GetDefaultPrinter를 호출하기 전에 프린터 이름을 저장할 수 있을 만큼 큰 버퍼를 할당해야 합니다. GetDefaultPrinter를 호출하고 NULL 첫 번째 인수로 전달하여 필요한 버퍼의 크기를 확인할 수 있습니다.

메모

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

 

다음 콘솔 애플리케이션은 기본 프린터의 이름을 가져오고 해당 프린터에 사각형과 타원을 그립니다. Graphics::DrawRectangle 호출은 StartPage 호출과 EndPage호출 사이에 있으므로 사각형이 독립된 페이지에 있습니다. 마찬가지로 줄임표는 페이지 자체에 있습니다.

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

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

   // Get the size of the default printer name.
   GetDefaultPrinter(NULL, &size);

   // Allocate a buffer large enough to hold the printer name.
   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);

      StartDoc(hdcPrint, &docInfo);
         Graphics* graphics;
         Pen* pen = new Pen(Color(255, 0, 0, 0));

         StartPage(hdcPrint);
            graphics = new Graphics(hdcPrint);
            graphics->DrawRectangle(pen, 50, 50, 200, 300);
            delete graphics;
         EndPage(hdcPrint);

         StartPage(hdcPrint);
            graphics = new Graphics(hdcPrint);
            graphics->DrawEllipse(pen, 50, 50, 200, 300);
            delete graphics;
         EndPage(hdcPrint);

         delete pen;
      EndDoc(hdcPrint);

      DeleteDC(hdcPrint);
   }

   delete buffer;

   GdiplusShutdown(gdiplusToken);
   return 0;
}