I have a form containing a map viewer, a treeview and a couple of graphical panels.
As attached, I am able to dump a .png with no problems using the code below:
procedure TFormMain.MenuViewOutputAsPngClick(Sender: TObject);
var
defaultName: string;
png: TPortableNetworkGraphic;
everything: TRect;
begin
(* We want the default name to match what's currently on the status bar, if we *)
(* used the current time this might differ by a few seconds from what's being *)
(* displayed due to message backlog. *)
defaultName := StatusBar1.Panels[2].Text;
if Pos('.', defaultName) > 0 then
SetLength(defaultName, Pos('.', defaultName) - 1);
defaultName := DelChars(defaultName, '-');
defaultName := DelChars(defaultName, 'T');
defaultName := DelChars(defaultName, ':');
(* Capture the image to match, do this before faffing around trying to get the *)
(* name right in case it's an incident that needs to be recorded. *)
png := TPortableNetworkGraphic.Create;
try
everything.Top := 0;
everything.Left := 0;
everything.Bottom := Height - MainMenu1.Height - 2;
everything.Right := Width - 1;
png.Height := everything.Bottom + 1;
png.Width := everything.Right + 1;
png.Canvas.CopyRect(everything, Canvas, everything);
SaveDialog1.Filename := AirfieldShortname + '-' + defaultName;
SaveDialog1.DefaultExt := 'png';
SaveDialog1.InitialDir := UserHome();
if SaveDialog1.Execute then
png.SaveToFile(SaveDialog1.Filename)
finally
png.Free
end
end { TFormMain.MenuViewOutputAsPngClick } ;
Similar code works to the clipboard.
What do I have to do to instead send this to a printer? I've tried several variants of the code below:
procedure TFormMain.MenuViewPrintClick(Sender: TObject);
const
LEFTMARGIN = 100;
HEADLINE = 'I Printed My Very First Text On ';
var
YPos, LineHeight, VerticalMargin: Integer;
SuccessString: String;
var
everything: TRect;
workingBitmap: TBitMap;
fromLimits, toLimits: TRect;
begin
{
// This works.
// https://wiki.freepascal.org/Using_the_printer
if PrintDialog1.Execute() then begin // This works
with Printer do try
BeginDoc;
Canvas.Font.Name := 'Courier New';
Canvas.Font.Size := 10;
Canvas.Font.Color := clBlack;
LineHeight := Round(1.2 * Abs(Canvas.TextHeight('I')));
VerticalMargin := 4 * LineHeight;
// There we go
YPos := VerticalMargin;
SuccessString := HEADLINE + DateTimeToStr(Now);
Canvas.TextOut(LEFTMARGIN, YPos, SuccessString);
finally
EndDoc;
end;
end
}
// The best I can get from either of the below is a blank page.
workingBitMap := TBitMap.Create;
workingBitMap.Width := Width;
workingBitMap.Height := Height - MainMenu1.Height;
PaintTo(workingBitMap.Canvas, 0, 0);
fromLimits := Rect(0, 0, workingBitMap.Width, workingBitMap.Height);
toLimits := Rect(0, 0, Printer.PaperSize.Width, Printer.PaperSize.Height);
if PrintDialog1.Execute() then begin
Printer.BeginDoc;
Printer.Canvas.CopyRect(toLimits, workingBitMap.Canvas, fromLimits);
Printer.EndDoc;
workingBitMap.Free
end;
{
// https://forum.lazarus.freepascal.org/index.php/topic,62651.msg473978.html#msg473978
if PrintDialog1.Execute() then begin // This doesn't work
Printer.BeginDoc;
try
everything.Top := 0;
everything.Left := 0;
everything.Bottom := Min(Printer.PageHeight, Height - MainMenu1.Height) - 2;
everything.Right := Min(Printer.PageWidth, Width) - 1;
Printer.Canvas.CopyRect(everything, Canvas, everything)
finally
Printer.EndDoc
end
end
}
end { TFormMain.MenuViewPrintClick } ;
If nobody has any quick suggestions I'll drop this for the moment, since what I'm trying to focus on is generating a video for the airfield's "fly-in-" in three weeks time.
Lazarus 2.2.6, FPC 3.2.2, Linux with GTK2 on a KDE desktop (Debian 12), printer is a networked colour laser branded Dell which is generally trouble-free.
MarkMLl