2
« on: 10-Nov-2006, 16:44:33 »
I attach some Delphi code in the hope it may help.
BTW the reference to canvas is a Delphi property of any graphical device of any size or resolution, so once you have established its properties any graphics function can write to any device's canvas
The first InitPrinter is from a text program, and the second is from a graphics program. The second is the later program, and it implies that the graphics needed an apparently redundant call to the PrinterIndex property to initialise properly.
As you can see from the {comments}, I am confused why I needed to do this second assignment to PrinterIndex. Whether it was Delphi's implementation of the Microsoft functions or the underlying functions I cannot tell, although the forums I visited suggested it was a Microsoft problem (well, they would, wouldn't they!)
Please excuse the diagnostics commented out, plainly I was confused and irritable at the time!
Procedure InitPrinter;
{
for GetPrinter.... if used...
var
hDevMode:THandle;
Device,Driver,Port:array[0..79] of Char;
}
begin
{
Assumes A4 throughout, to calculate layout dimensions.
Has to use WINAPI calls to get pixels per inch.
}
with printer do
begin
printerindex:=-1;
{
The following 2 lines are said to be necessary to ensure change is fully
implemented:-
GetPrinter(device,Driver,Port,hDevMode);
SetPrinter(device,Driver,Port,hDevMode);
I have also seen:-
Printer.PrinterIndex:=Printer.PrinterIndex;
Probably(?) equivalent. I am not using them (apparently without ill effect)
as I am setting Printer.PrinterIndex anyway.
}
begindoc;
with canvas do
begin
InchPixW:=GetDeviceCaps(printer.canvas.Handle,LogPixelsX);
InchPixH:=GetDeviceCaps(printer.canvas.Handle,LogPixelsY);
with font do
begin
{
Is this a Windows bug, or a VCL bug(!?)
The following line is necessary to ensure font scaling is initialised:-
}
pixelsperInch:=InchPixH;
name:='Arial';
Size:=LmkFontSize;
end;
ThinLine:=round(inchpixh / 300);
if thinline<1 then
ThinLine:=1;
BorderWidth:=Thinline*2;
SetFontSize(LmkFontSize);
{
showmessage('Horizontal pixels/in '+inttostr(inchpixw)+#13+
'Vertical pixels/in '+inttostr(inchpixh)+#13+
'Thin line width '+inttostr(thinline)+#13+
'Line height '+inttostr(lineheight));
}
pen.color:=clblack;
end;
end;
end;
Procedure InitPrinter;
begin
with printer do
begin
printerindex:=-1;
{
The following seems to be necessary (it might be VCL magic!)to ensure
printer change is fully implemented:-
}
PrinterIndex:=PrinterIndex;
begindoc;
XPixPerMm:=round(GetDeviceCaps(printer.canvas.Handle,LogPixelsX)/InToMm);
YPixPerMm:=round(GetDeviceCaps(printer.canvas.Handle,LogPixelsY)/InToMm);
{
showmessage(floattostr(GetDeviceCaps(printer.canvas.Handle,HorzSize)));
showmessage(floattostr(GetDeviceCaps(printer.canvas.Handle,VertSize)));
}
end;
end;