FireMonkey モーダル ダイアログ ボックスの使用

提供: RAD Studio
移動先: 案内検索

FireMonkey アプリケーションの設計 への移動


このトピックでは、FireMonkey ダイアログ ボックスの作成、設定、表示の方法について説明します。また、コールバック メソッドを使用した戻り値の処理方法や、モーダル ダイアログによって確保されたメモリを、ダイアログが閉じた後に解放する方法についても扱います。

モーダル ダイアログ ボックスの表示

以下のコードを使用すると、FireMonkey アプリケーションでモーダル ダイアログ ボックスを表示することができます。

メモ: Android は、モーダル ダイアログをサポートしていません。

Delphi の場合:

procedure MyCurrentForm.MyButtonClick(Sender: TObject);
var
  dlg: TMyModalForm;
begin
  // Create an instance of a form.
  dlg := TMyModalForm.Create(nil);

  // Configure the form. For example, give it a display name.
  dlg.Caption := 'My Modal Dialog Box';

  // Show your dialog box and provide an anonymous method that handles the closing of your dialog box.
  dlg.ShowModal(
    procedure(ModalResult: TModalResult)
    begin
      // Do something.
    end
  );
end;
C++ の場合:
1. TProc__1 インターフェイスを受け取るクラスを定義し、ダイアログ ボックスを閉じる処理を行う関数を定義します。
class TModalFormCallback : public TCppInterfacedObject<TProc__1<TModalResult> > {
public:
  TMyModalForm *dlg;
  TMyCurrentForm *MyCurrentForm;

  void __fastcall Invoke(TModalResult ModalResult) {
    // Do something.
  }
};
2. その後、このクラスのインスタンスを ShowModal に渡します。
void __fastcall TMyCurrentForm::MyButtonClick(TObject *Sender) {
  // Create an instance of a form.
  TMyModalForm *dlg = new TMyModalForm(NULL);

  // Configure the form. For example, give it a display name.
  dlg->Caption = "My Modal Dialog Box";

  // Create and configure an instance of your callback method.
  TModalFormCallback* ModalFormCallback = new TModalFormCallback();
  ModalFormCallback->dlg = dlg;
  ModalFormCallback->MyCurrentForm = this;

  // Show your dialog box and provide an anonymous method that handles the closing of your dialog box.
  dlg->ShowModal(ModalFormCallback);
}

モーダル ダイアログ ボックスの解放

モーダル ダイアログ ボックス フォームを閉じる処理を行うメソッドの中で、モーダル ダイアログ ボックス フォーム用に割り当てられたメモリを解放することはできません。モーダル ダイアログ ボックス フォームを解放するには、フォームの OnClose イベントを次のように処理する必要があります。

Delphi の場合:

procedure TMyModalForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := TCloseAction.caFree;
end;
C++ の場合:
void __fastcall TMyModalForm::FormClose(TObject *Sender, TCloseAction &Action) {
  Action = TCloseAction::caFree;
}

関連項目