Windows的GDI(图形设备接口)是一个允许应用程序以一种近乎设备独立的方式与图形
输出设备交流的界面。GDI提供给应用程序一个指向显示上下文(display context或缩
定DC)或信息上下文(information context缩写IC)的句柄。
DC句柄允许用户使用GDI调用以有给定设备环境(矩形、图形、文本等)绘图。而IC句柄
使用应用程序获得一个给定设备的有关信息(每一个in的象素点,页尺寸等等)
Delphi通过TPrinter对象的属性和方法封装了许多绘图和信息调用,由TPrinter对象来
调用Windows的GDI。
HorizontalInch := GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSX);
VerticalInch := GetDeviceCaps(Printer.Canvas.Handle,LOGPIXELSY);
var PointsX:do
uble;
var PointsY:do
uble;
PointsX := GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSX) / 72;
PointsY := GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY) / 72;
下面例子是画一个2in宽,3in高矩形,其放置在离页左上角1in的位置(没有考虑打印页边)
with Printer.Canvasdo
Rectangle(Round(PointesX * 72),
Round(PointsY * 72),
Round(PointsX * 216),
Round(PointsY * 288));
彩色转黑白
Red := Round(0.3 * red) + (0.59 * green) + (0.11 * blue);
Green := Red;
Blue := Red;
FDevice : PChar;
FDriver : PChar;
FPort : PChar;
DeviceMode : THandle;
DevMode : PDeviceMode;
constructor TPrinterSettings.Create;
begin
inherited Create;
GetMem(FDevice, 128);
GetMem(FDriver, 128);
GetMem(FPort, 128);
FPaperSize := A4;
FPrinter := nil;
end;
function TPrinterSettings.Supported(Setting : integer) : boolean;
begin
if assigned(FPrinter) then
Supported := (DevMode^.dmFields and Setting) = Setting
else
Supported := false;
end;
procedure TPrinterSettings.SetField(aField : integer);
begin
DevMode^.dmFields := DevMode^.dmFields or aField;
end;
procedure TPrinterSettings.ApplySettings;
begin
FPrinter.GetPrinter(FDevice, FDriver, FPort, DeviceMode);
DevMode := GlobalLock(DeviceMode);
if PaperSize = Custom then
begin
SetField(dm_paperlength);
DevMode^.dmPaperLength := PaperLength;
SetField(dm_paperwidth);
DevMode^.dmPaperWidth := PaperWidth;
{$ifdef FANG}
if Supported(dm_formname) then
begin
SetField(dm_formname);
StrPCopy(DevMode^.dmFormName, PaperFormName);
end;
//fang 900327 end
{$endif}
end;
if FDuplex then
begin
SetField(dm_duplex);
DevMode^.dmDuplex := dmdup_horizontal;
end;
if Supported(dm_PaperSize) and (PaperSize <> Default) then
begin
SetField(dm_papersize);
{$ifdef FANG}
if GetVersion() < $80000000 then
begin
if GetPrnPaperSize(FDevice, PaperFormName, iPaperSize) then
DevMode^.dmPaperSize := iPaperSize
else
DevMode^.dmPaperSize := cQRPaperTranslate[PaperSize];
end
else
{$endif}
DevMode^.dmPaperSize := cQRPaperTranslate[PaperSize];
end;
SetField(dm_copies);
DevMode^.dmCopies := FCopies;
SetField(dm_defaultsource);
DevMode^.dmDefaultSource := (DevMode^.dmDefaultSource and 256) or cQRBinTranslate[OutputBin];
SetField(dm_orientation);
if Orientation=poPortrait then
DevMode^.dmOrientation := dmorient_portrait
else
DevMode^.dmOrientation := dmorient_landscape;
FPrinter.SetPrinter(FDevice, FDriver, FPort, DeviceMode);
GlobalUnlock(DeviceMode);
end;