2

だから私はDelphiを初めて使い、ボタンがあり、クリックするとOpenPictureDialogが開きます。次に、その画像が読み込まれたポップアップ ボックスを作成します。これに対する最善のアプローチが何であるかわかりませんか?

ボタン クリック イベントで新しいフォームを作成し、そこに画像を配置することを考えていましたが、TImage をフォーム コンストラクターに渡す方法がわかりません。

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

これを行う方法や、私がやろうとしていることを修正する方法について、誰かがより良い考えを持っていますか?

ありがとう。

4

1 に答える 1

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;
于 2013-01-18T17:09:35.193 に答える