德尔福弹出框与图像

时间:2013-01-18 17:00:45

标签: image forms delphi button popup

所以我是Delphi的新手,我有一个按钮,单击它时会打开一个OpenPictureDialog。然后我想创建一个弹出框,其中加载了该图片。我不确定最好的方法是什么?

我想在按钮点击事件上创建一个新表单,然后将图像放入其中,但我无法弄清楚如何将TImage传递给表单构造函数。

OpenPictureDialog1.Execute;
img.Picture.LoadFromFile(OpenPictureDialog1.FileName);
TForm2.Create(Application, img).Show;

有没有人更好地了解如何做到这一点或某种方法来解决我正在尝试做的事情?

感谢。

1 个答案:

答案 0 :(得分:6)

您最好将TImage组件放在辅助表单中并传递文件名,例如,为您的表单创建一个新的构造函数,如下所示:

type
  TForm2 = class(TForm)
    Image1: TImage;
  private
  public
    constructor CreateWithImage(AOwner: TComponent; AImgPath: string);
  end;

...
implementation
...

constructor TForm2.CreateWithImage(AOwner: TComponent; AImgPath: string);
begin
  Create(AOwner);
  Image1.Picture.LoadFromFile(AImgPath);
end;

然后,您可以创建并显示如下表单:

procedure TForm1.Button1Click(Sender: TObject);
var
  Form2: TForm2;
begin
  if OpenPictureDialog1.Execute then
  begin
    Form2 := TForm2.CreateWithImage(Self, OpenPictureDialog1.FileName);
    Form2.Show;
  end;
end;

修改

如果要在运行时创建图像,可以这样做:

type
  TForm2 = class(TForm)
  private
    FImage: TImage; //this is now in the private section, 
                     //and not the default (published)
  public
    constructor CreateWithImage(AOwner: TComponent; AImgPath: string);
  end;

...
implementation
...

constructor TForm2.CreateWithImage(AOwner: TComponent; AImgPath: string);
begin
  Create(AOwner);
  FImage := TImage.Create(Self);
  FImage.Parent := Self;
  FImage.Align := alClient;
  FImage.Picture.LoadFromFile(AImgPath);
end;