プリンターへの 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;
}
前のコードでは、3 つの GDI+ 描画コマンドは 、StartDoc 関数と EndDoc 関数の呼び出しの間にあり、それぞれがプリンター デバイス コンテキスト ハンドルを受け取ります。 StartDoc と EndDoc の間のすべてのグラフィックス コマンドは、一時的なメタファイルにルーティングされます。 EndDoc の呼び出し後、プリンター ドライバーは、メタファイル内のデータを、使用されている特定のプリンターに必要な形式に変換します。
Note
使用されているプリンターに対してスプールが有効になっていない場合、グラフィックス出力はメタファイルにルーティングされません。 代わりに、個々のグラフィックス コマンドがプリンター ドライバーによって処理され、プリンターに送信されます。
一般に、前のコンソール アプリケーションで行われたように、プリンターの名前をハードコーディングする必要はありません。 名前をハードコーディングする代わりに、 GetDefaultPrinter を 呼び出して既定のプリンターの名前を取得する方法があります。 GetDefaultPrinter を呼び出す前に、プリンター名を保持するのに十分な大きさのバッファーを割り当てる必要があります。 GetDefaultPrinter を呼び出し、最初の引数として NULL を 渡すことで、必要なバッファーのサイズを確認できます。
Note
GetDefaultPrinter 関数は、Windows 2000 以降でのみサポートされています。
次のコンソール アプリケーションは、既定のプリンターの名前を取得し、そのプリンターに四角形と楕円を描画します。 Graphics::D rawRectangle 呼び出しは 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;
}