|
Printing exact size
TPrintJob component has many useful methods those allow to transfer the units between pixels, inches and millimeters.
function InchToMm(Inches: Double): Double;
function MmToInch(Millimeters: Double): Double;
function InchToPix(Inches: Double; Direction: TDirection): Double;
function MmToPix(Millimeters: Double; Direction: TDirection): Double;
function PixToMm(Pixels: Double; Direction: TDirection): Double;
function PixToInch(Pixels: Double; Direction: TDirection): Double;
All functions related to pixels has an additional Direction parameter of the TDirection type.
TDirection = (dirHorizontal,dirVertical);
This parameter is used for retreiving the vertical or horizontal resolution of the printer to correct calculation, because the vertical and horizontal resultions can be different.
TPrintJob has the ConvertUnits method that can convert the values between any units.
type
TUnits = (unPixels,unPercents,unInches,unMillimeters);
function ConvertUnits(
Source: Double; // source value
FromUnits,ToUnits: TUnits; // source and target units
Dir: TDirection; // direction
FullRange: Double): Double; // full range for percent units,
// usually page size
Any of these functions can be used for printing the graphic objects with the exact size. TCanvas and WinAPI functions receves all sizes in pixels, so to print the inch square just get the pixel size of such square using the InchToPix function.
procedure TForm1.PrintJob1Draw(Sender: TObject; TheCanvas: TCanvas;
PageIndex: Integer; TheRect: TRect; Area: TDrawArea;
Target: TDrawTarget);
begin
with Sender as TPrintJob,TheCanvas,TheRect do
if Area=daPage then
begin
Rectangle(
Left,
Top,
Left+Trunc(InchToPix(1,dirHorizontal)),
Top+Trunc(InchToPix(1,dirVertical)));
end;
end;
|