C++ > CLI:スクロールバー

基本プロジェクトに HScrollBar と Label を配置します。


VScrollBar と Label も配置します。


HScrollBar をダブルクリックして以下のコードを追加します。

label1->Text = hScrollBar1->Value.ToString();


VScrollBar をダブルクリックして以下のコードを追加します。

label2->Text = vScrollBar1->Value.ToString();


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

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::HScrollBar^  hScrollBar1;
 private: System::Windows::Forms::Label^  label1;
 private: System::Windows::Forms::VScrollBar^  vScrollBar1;
 private: System::Windows::Forms::Label^  label2;
 protected:


 protected:

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

#pragma region Windows Form Designer generated code
  /// <summary>
  /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
  /// コード エディターで変更しないでください。
  /// </summary>
  void InitializeComponent(void)
  {
   this->hScrollBar1 = (gcnew System::Windows::Forms::HScrollBar());
   this->label1 = (gcnew System::Windows::Forms::Label());
   this->vScrollBar1 = (gcnew System::Windows::Forms::VScrollBar());
   this->label2 = (gcnew System::Windows::Forms::Label());
   this->SuspendLayout();
   //
   // hScrollBar1
   //
   this->hScrollBar1->Location = System::Drawing::Point(34, 221);
   this->hScrollBar1->Name = L"hScrollBar1";
   this->hScrollBar1->Size = System::Drawing::Size(80, 17);
   this->hScrollBar1->TabIndex = 0;
   this->hScrollBar1->Scroll += gcnew System::Windows::Forms::ScrollEventHandler(this, &MyForm::hScrollBar1_Scroll);
   //
   // label1
   //
   this->label1->AutoSize = true;
   this->label1->Location = System::Drawing::Point(32, 169);
   this->label1->Name = L"label1";
   this->label1->Size = System::Drawing::Size(35, 12);
   this->label1->TabIndex = 1;
   this->label1->Text = L"label1";
   //
   // vScrollBar1
   //
   this->vScrollBar1->Location = System::Drawing::Point(241, 22);
   this->vScrollBar1->Name = L"vScrollBar1";
   this->vScrollBar1->Size = System::Drawing::Size(17, 80);
   this->vScrollBar1->TabIndex = 2;
   this->vScrollBar1->Scroll += gcnew System::Windows::Forms::ScrollEventHandler(this, &MyForm::vScrollBar1_Scroll);
   //
   // label2
   //
   this->label2->AutoSize = true;
   this->label2->Location = System::Drawing::Point(204, 131);
   this->label2->Name = L"label2";
   this->label2->Size = System::Drawing::Size(35, 12);
   this->label2->TabIndex = 3;
   this->label2->Text = L"label2";
   //
   // 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->label2);
   this->Controls->Add(this->vScrollBar1);
   this->Controls->Add(this->label1);
   this->Controls->Add(this->hScrollBar1);
   this->Name = L"MyForm";
   this->Text = L"Hello C++/CLI World !!";
   this->ResumeLayout(false);
   this->PerformLayout();

  }
#pragma endregion

 private: System::Void hScrollBar1_Scroll(System::Object^  sender, System::Windows::Forms::ScrollEventArgs^  e) {
     label1->Text = hScrollBar1->Value.ToString();
    }
 private: System::Void vScrollBar1_Scroll(System::Object^  sender, System::Windows::Forms::ScrollEventArgs^  e) {
     label2->Text = vScrollBar1->Value.ToString();
    }
};
}
 

 

 

 

 

 

 

最終更新:2013年10月10日 16:07
添付ファイル