C++ > CLI:コンテキストメニュー

基本プロジェクトにコンテキストメニューを配置します。


今回も適当に、それらしいメニューを入力しておきます。


フォームをクリックして、プロパティの ContextMenu に
作成したコンテキストメニューを選択します。
これで、右クリックするとメニューが表示されるようになります。


ビルドして実行してみましょう。

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::ContextMenuStrip^  contextMenuStrip1;
 protected:
 private: System::Windows::Forms::ToolStripMenuItem^  切り取りToolStripMenuItem;
 private: System::Windows::Forms::ToolStripMenuItem^  コピーToolStripMenuItem;
 private: System::Windows::Forms::ToolStripMenuItem^  全体ToolStripMenuItem;
 private: System::Windows::Forms::ToolStripMenuItem^  一部ToolStripMenuItem;
 private: System::Windows::Forms::ToolStripMenuItem^  貼り付けToolStripMenuItem;
 private: System::Windows::Forms::ToolStripMenuItem^  削除ToolStripMenuItem;
 private: System::ComponentModel::IContainer^  components;


 protected:

 private:
  /// <summary>
  /// 必要なデザイナー変数です。
  /// </summary>


#pragma region Windows Form Designer generated code
  /// <summary>
  /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
  /// コード エディターで変更しないでください。
  /// </summary>
  void InitializeComponent(void)
  {
   this->components = (gcnew System::ComponentModel::Container());
   this->contextMenuStrip1 = (gcnew System::Windows::Forms::ContextMenuStrip(this->components));
   this->切り取りToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
   this->コピーToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
   this->全体ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
   this->一部ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
   this->貼り付けToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
   this->削除ToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
   this->contextMenuStrip1->SuspendLayout();
   this->SuspendLayout();
   //
   // contextMenuStrip1
   //
   this->contextMenuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(4) {this->切り取りToolStripMenuItem,
    this->コピーToolStripMenuItem, this->貼り付けToolStripMenuItem, this->削除ToolStripMenuItem});
   this->contextMenuStrip1->Name = L"contextMenuStrip1";
   this->contextMenuStrip1->Size = System::Drawing::Size(117, 92);
   //
   // 切り取りToolStripMenuItem
   //
   this->切り取りToolStripMenuItem->Name = L"切り取りToolStripMenuItem";
   this->切り取りToolStripMenuItem->Size = System::Drawing::Size(116, 22);
   this->切り取りToolStripMenuItem->Text = L"切り取り";
   //
   // コピーToolStripMenuItem
   //
   this->コピーToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->全体ToolStripMenuItem,
    this->一部ToolStripMenuItem});
   this->コピーToolStripMenuItem->Name = L"コピーToolStripMenuItem";
   this->コピーToolStripMenuItem->Size = System::Drawing::Size(116, 22);
   this->コピーToolStripMenuItem->Text = L"コピー";
   //
   // 全体ToolStripMenuItem
   //
   this->全体ToolStripMenuItem->Name = L"全体ToolStripMenuItem";
   this->全体ToolStripMenuItem->Size = System::Drawing::Size(152, 22);
   this->全体ToolStripMenuItem->Text = L"全体";
   //
   // 一部ToolStripMenuItem
   //
   this->一部ToolStripMenuItem->Name = L"一部ToolStripMenuItem";
   this->一部ToolStripMenuItem->Size = System::Drawing::Size(152, 22);
   this->一部ToolStripMenuItem->Text = L"一部";
   //
   // 貼り付けToolStripMenuItem
   //
   this->貼り付けToolStripMenuItem->Name = L"貼り付けToolStripMenuItem";
   this->貼り付けToolStripMenuItem->Size = System::Drawing::Size(116, 22);
   this->貼り付けToolStripMenuItem->Text = L"貼り付け";
   //
   // 削除ToolStripMenuItem
   //
   this->削除ToolStripMenuItem->Name = L"削除ToolStripMenuItem";
   this->削除ToolStripMenuItem->Size = System::Drawing::Size(116, 22);
   this->削除ToolStripMenuItem->Text = L"削除";
   //
   // MyForm
   //
   this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
   this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
   this->ClientSize = System::Drawing::Size(284, 261);
   this->ContextMenuStrip = this->contextMenuStrip1;
   this->Name = L"MyForm";
   this->Text = L"Hello C++/CLI World !!";
   this->contextMenuStrip1->ResumeLayout(false);
   this->ResumeLayout(false);

  }
#pragma endregion

 };
}

 

 

 

 

 

 

最終更新:2013年09月24日 01:37