본문 바로가기
C++

[C++] Windows Form 버튼 리스트 만들기

by 알래스카비버 2021. 6. 25.
반응형

 

C++ Windows Form에서 버튼들을 리스트에 넣고 클릭하면 버튼에 해당하는 내용의 메시지박스가 뜨는 프로그램입니다.

혼자서 찾아보고 만들었었던 것이라 이 방법이 맞는 방법이 아닐 수도 있습니다.

 

실행영상

 

Form 이름은 MyForm 으로 했습니다.

 

MyForm.h를 코드로 열어줍니다.

코드를 보는 방법은 MyForm.h[디자인] 에서 F7을 누르거나

솔루션 탐색기 - 헤더파일 - MyForm.h에서 우클릭을 하고 코드 보기를 눌러줍니다.

 

 

MyForm 클래스 안에 다음 코드를 추가해줍니다. 

button_list라는 버튼 리스트를 선언하는 코드입니다.

private: System::Collections::Generic::List< System::Windows::Forms::Button^> button_list;

 

버튼을 만들어 리스트에 추가하는 MyForm_Load 라는 메서드도 작성해줍니다.

private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
	int xPos = 10, yPos = 10;		// 버튼의 x, y 위치
	for (int x = 0; x < 6; x++)		// 버튼 6개 생성
	{
		button_list.Add(gcnew Button); // 버튼 리스트에 버튼을 추가
		button_list[x]->Location = Point(xPos, yPos); // 버튼의 위치
		button_list[x]->Text = "button " + x; // 버튼의 내용 ("button 0", "button 1" ...)
		button_list[x]->Size = System::Drawing::Size(90, 50); // 버튼의 크기
		button_list[x]->Click += gcnew System::EventHandler(this, &MyForm::ButtonClick); // 버튼을 클릭했을 때 이벤트
		this->Controls->Add(button_list[x]);
		xPos += 100;
	}
}

 

MyForm 클래스 안 InitializeComponent 메서드에 MyForm이 시작되었을 때 MyForm_Load 라는 메서드가 실행될 수 있게 다음 코드를 추가해줍니다.

this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);

 

버튼을 클릭했을 때 ButtonClick() 메서드가 실행되고 메시지박스가 띄워집니다.

private: System::Void ButtonClick(System::Object^ sender, System::EventArgs^ e)
	{
		System::Windows::Forms::Button^ btn = (System::Windows::Forms::Button^)sender;	// 클릭한 버튼
		MessageBox::Show(btn->Text);
	}

 

이제 완성되었습니다.

실행을 해보면 버튼이 6개가 있는 창이 뜨고 버튼을 클릭하면 버튼의 내용이 나오는 것을 확인할 수 있습니다.

 

 

전체코드입니다.

더보기

#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::Collections::Generic::List< System::Windows::Forms::Button^> button_list;

protected:

private:
/// <summary>
/// 필수 디자이너 변수입니다.
/// </summary>
System::ComponentModel::Container^ components;

#pragma region Windows Form Designer generated code
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다. 
/// 이 메서드의 내용을 코드 편집기로 수정하지 마세요.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
// 
// MyForm
// 
this->AutoScaleDimensions = System::Drawing::SizeF(8, 15);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(701, 388);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
this->ResumeLayout(false);

}
#pragma endregion

private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
int xPos = 10, yPos = 10; // 버튼의 x, y 위치
for (int x = 0; x < 6; x++) // 버튼 6개 생성
{
button_list.Add(gcnew Button); // 버튼 리스트에 버튼을 추가
button_list[x]->Location = Point(xPos, yPos); // 버튼의 위치
button_list[x]->Text = "button " + x; // 버튼의 내용 ("button 0", "button 1" ...)
button_list[x]->Size = System::Drawing::Size(90, 50); // 버튼의 크기
button_list[x]->Click += gcnew System::EventHandler(this, &MyForm::ButtonClick); // 버튼을 클릭했을 때 이벤트
this->Controls->Add(button_list[x]);
xPos += 100;
}
}

private: System::Void ButtonClick(System::Object^ sender, System::EventArgs^ e)
{
System::Windows::Forms::Button^ btn = (System::Windows::Forms::Button^)sender; // 클릭한 버튼
MessageBox::Show(btn->Text);
}
};
}

 

반응형

'C++' 카테고리의 다른 글

[C++] Windows Form Class 사용  (0) 2021.07.02
[C++] Windows Forms 프로젝트 시작 - Visual Studio 2019  (7) 2021.06.02

댓글