Windowsフォームの閉じるボタンが押されたことを知る

Windowsフォームの閉じるボタンが押されたことを知るには、

まずは、イベントハンドラを作成し

private: System::Void Form_Closing( System::Object^ sender, System::ComponentModel::CancelEventArgs^ e) {
    MessageBox::Show( “閉じるボタンが押された!” );
}

これを、Closingに関連づける。

this->Closing += gcnew System::ComponentModel::CancelEventHandler(this, &Form::Form_Closing);

これで、おしまい。

おまけ。閉じるボタンが押されたとき、終了するかユーザに問い合わせる

private: System::Void Form_Closing( System::Object^ sender, System::ComponentModel::CancelEventArgs^ e) {
    // メッセージボックスを表示
    ::DialogResult result = MessageBox::Show( “終了する?”, “イベント発生”, MessageBoxButtons::YesNo, MessageBoxIcon::Question );

    // 「いいえ」が押されたら終了しない
    if( result == ::DialogResult::No )
    {
        e->Cancel = true;
    }
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です