C++ > CLI:モーダルダイアログ

基本プロジェクトを立ち上げます。
モーダルダイアログというコントロールは無いので
モーダルダイアログを自作します。
プロジェクトの新しい項目の追加を選択します。


UI の Windowsフォームを選択し、名前を Dialog.h にして
追加ボタンを押します。


プロパティの MaximizeBox と MinimizeBox と ShowInTaskbar を
False にします。


プロパティの StartPosition を CenterParent にして
FormBorderStyle を FixedDialog にします。


MyForm に Button を配置します。


配置されたボタンをダブルクリックして以下のコードを追加します。

     Form^ Dialog = gcnew Form();
     Dialog->ShowDialog();  // モーダルダイアログとして表示


ビルドして実行してみましょう。
モーダルダイアログが表示されている間は、親ウインドウは
一切の操作を受け付けません。

MyForm.cpp

#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
#include "MyForm.h"

using namespace Project1;

[STAThreadAttribute]
int main(){
 MyForm ^form1 = gcnew MyForm;
 form1->ShowDialog();
 return 0;
}

MyForm.h

#pragma once

namespace Project1 {

 using namespace System;
 using namespace System::ComponentModel;
 using namespace System::Collections;
 using namespace System::Windows::Forms;
 using namespace System::Data;
 using namespace System::Drawing;

 /// <summary>
 /// MyForm の概要
 /// </summary>
 public ref class MyForm : public System::Windows::Forms::Form
 {
 public:
  MyForm(void)
  {
   InitializeComponent();
   //
   //TODO: ここにコンストラクター コードを追加します
   //
  }

 protected:
  /// <summary>
  /// 使用中のリソースをすべてクリーンアップします。
  /// </summary>
  ~MyForm()
  {
   if (components)
   {
    delete components;
   }
  }
 private: System::Windows::Forms::Button^  button1;
 protected:


 protected:

 private:
  /// <summary>
  /// 必要なデザイナー変数です。
  /// </summary>
  System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
  /// <summary>
  /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
  /// コード エディターで変更しないでください。
  /// </summary>
  void InitializeComponent(void)
  {
   this->button1 = (gcnew System::Windows::Forms::Button());
   this->SuspendLayout();
   //
   // button1
   //
   this->button1->Location = System::Drawing::Point(71, 69);
   this->button1->Name = L"button1";
   this->button1->Size = System::Drawing::Size(75, 23);
   this->button1->TabIndex = 0;
   this->button1->Text = L"button1";
   this->button1->UseVisualStyleBackColor = true;
   this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
   //
   // MyForm
   //
   this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
   this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
   this->ClientSize = System::Drawing::Size(284, 261);
   this->Controls->Add(this->button1);
   this->Name = L"MyForm";
   this->Text = L"Hello C++/CLI World !!";
   this->ResumeLayout(false);

  }
#pragma endregion

 private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
     Form^ Dialog = gcnew Form();
     Dialog->ShowDialog();  // モーダルダイアログとして表示
    }
 };
}

Dialog.h

#pragma once

namespace Project1 {

 using namespace System;
 using namespace System::ComponentModel;
 using namespace System::Collections;
 using namespace System::Windows::Forms;
 using namespace System::Data;
 using namespace System::Drawing;

 /// <summary>
 /// Dialog の概要
 /// </summary>
 public ref class Dialog : public System::Windows::Forms::Form
 {
 public:
  Dialog(void)
  {
   InitializeComponent();
   //
   //TODO: ここにコンストラクター コードを追加します
   //
  }

 protected:
  /// <summary>
  /// 使用中のリソースをすべてクリーンアップします。
  /// </summary>
  ~Dialog()
  {
   if (components)
   {
    delete components;
   }
  }

 private:
  /// <summary>
  /// 必要なデザイナー変数です。
  /// </summary>
  System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
  /// <summary>
  /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
  /// コード エディターで変更しないでください。
  /// </summary>
  void InitializeComponent(void)
  {
   this->SuspendLayout();
   //
   // Dialog
   //
   this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
   this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
   this->ClientSize = System::Drawing::Size(284, 261);
   this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedDialog;
   this->MaximizeBox = false;
   this->MinimizeBox = false;
   this->Name = L"Dialog";
   this->ShowInTaskbar = false;
   this->StartPosition = System::Windows::Forms::FormStartPosition::CenterParent;
   this->Text = L"Dialog";
   this->ResumeLayout(false);

  }
#pragma endregion
 };
}

 

 

 

 

 

 

最終更新:2013年10月06日 21:48