使用对象作为参数调用过程

时间:2015-08-16 08:46:02

标签: delphi

尝试保存代码。我想在OnActivate表单上的图像上显示文本等,然后在单击按钮上打印相同的文本(真正的程序更复杂)。为了保存两次编写代码,我尝试了附带的代码,但它不会在“Obj.Canvas”行编译。如果我注释掉这一行和封闭的行程序运行但是Obj值是()。

我尝试了其他几种方法但没有效果。谁能告诉我哪里出错了?

unit Unit7;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, printers;

type
  TForm7 = class(TForm)
    Print: TButton;
    Image1: TImage;
    PrintDialog1: TPrintDialog;
    procedure FormActivate(Sender: TObject);
    procedure PrintClick(Sender: TObject);
  private
    { Private declarations }
  public
    DH,DW:Extended;
    Procedure DoLayout(Obj:TObject);
    { Public declarations }
  end;

var
  Form7: TForm7;

implementation

{$R *.dfm}

procedure TForm7.FormActivate(Sender: TObject);
begin
DoLayout(Image1);
end;

procedure TForm7.PrintClick(Sender: TObject);
begin
  if PrintDialog1.Execute then
  begin
    printer.BeginDoc;
      DoLayout(Printer);
    Printer.EndDoc;
  end;
end;

procedure TForm7.DoLayout(Obj:TObject);
begin
  if  Obj =Printer then         //when you run the program Obj is ()
  begin
    DW:=Printer.PageWidth/Image1.Width;
    DH:=Printer.PageHeight/Image1.Height;
  end
  else
  begin
    DH:=1;
    DW:=1;
  end;
  With Obj.canvas do          //Error here when compiled   - tried commenting it out
  begin
    TextOut(Int(DH*50),Int(DW*30),'This is the text');    //commented this out too
  end;
end;

end.

0 个答案:

没有答案