如何插入或连接两个过程



嗨,我有一个菜单,其中有一个备忘录的打印部分,现在我不知道如何单击该部分来运行我找到的打印代码
如何链接或可能插入
PrintTStrings过程(Lst:TStrings);在程序菜单部分
TForm1.MenuItemPrintClick(发件人:TObject);开始终止

procedure TForm1.MenuItemPrinter(Sender: TObject);
begin
end; 
procedure PrintTStrings(Lst : TStrings) ;
var
I,
Line : Integer;
begin
I := 0;
Line := 0 ;
Printer.BeginDoc ;
for I := 0 to Lst.Count - 1 do begin
Printer.Canvas.TextOut(0, Line, Lst[I]);
{Font.Height is calculated as -Font.Size * 72 / Font.PixelsPerInch which returns
a negative number. So Abs() is applied to the Height to make it a non-negative
value}
Line := Line + Abs(Printer.Canvas.Font.Height);
if (Line >= Printer.PageHeight) then
Printer.NewPage;
end;
Printer.EndDoc;
end;                       

您必须先声明PrintStrings过程,然后才能使用它。正确的方法是通过在表单声明的private部分声明PrintTStrings,使其成为表单的方法:

type
TForm1 = class(TForm)
// Items you've dropped on the form, and methods assigned to event handlers
private
procedure PrintTStrings(Lst: TStrings);
end;

然后,您可以直接从菜单项的OnClick事件中调用它:

procedure TForm1.MenuItemPrinter(Sender: TObject);
begin
PrintTStrings(PrinterMemo.Lines);
end;

如果由于某种原因,你不能将其作为一种表单方法,你可以重新排列你的代码:

procedure PrintTStrings(Lst : TStrings) ;
var
I,
Line : Integer;
begin
I := 0;
Line := 0 ;
Printer.BeginDoc ;
for I := 0 to Lst.Count - 1 do begin
Printer.Canvas.TextOut(0, Line, Lst[I]);
{Font.Height is calculated as -Font.Size * 72 / Font.PixelsPerInch which returns
a negative number. So Abs() is applied to the Height to make it a non-negative
value}
Line := Line + Abs(Printer.Canvas.Font.Height);
if (Line >= Printer.PageHeight) then
Printer.NewPage;
end;
Printer.EndDoc;
end;  
procedure TForm1.MenuItemPrinter(Sender: TObject);
begin
PrintTStrings(PrinterMemo.Lines);
end; 

最新更新