C++ winform 이벤트 기능

5tr1ker·2022년 12월 31일
0

winform 디자인 수정하기

참고 : https://howbeautifulworld.tistory.com/34

먼저 프로젝트 생성 -> CLR 빈 프로젝트를 생성 -> 솔루션 탐색기에서 프로젝트 우클릭 속성 -> 구성속성/링커/고급에 진입점을 main 으로 설정 , 시스템의 하위 시스템을 창 으로 설정

이후 새 항목에서 winform 파일 생성 후 밑의 코드를 입력 후 실행

using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void main(array<String^>^ args) {
	Application::SetCompatibleTextRenderingDefault(false);
	Application::EnableVisualStyles();
	Project1::MyForm form;	// Project1 - 본인 프로젝트 이름
	Application::Run(% form);
}

라벨의 텍스트를 수정하기

string sb = "hello";
		cout << sb;
		label1->Text = gcnew String(sb.c_str()); // str::string를 system 으로

int형을 string으로 변환 후 라벨의 텍스트 수정

label1->Text = gcnew String(to_string(cl.Add(5, 15)).c_str());

textbox의 텍스트 데이터를 가져오기

#include <msclr\marshal_cppstd.h> 헤더를 추가해주고,

string message = msclr::interop::marshal_as<std::string>(textBox1->Text); // system::str을 std::str로
		label2->Text = textBox1->Text;

새로운 창 띄우기

MyForm2 f2;
	f2.ShowDialog();

MyForm2의 경우 새로운 form 헤더를 생성 후 #include 를 하여 객체화 시켜준다.

TableLayoutPanel에 새로운 라벨 추가하기

System::Windows::Forms::Label^ the_label1;
	the_label1 = (gcnew System::Windows::Forms::Label());
	the_label1->Text = L"메시지";
	the_label1->AutoSize = true;
	tableLayoutPanel1->Controls->Add(the_label1, 0, 0);

초기 생성자 코드에 작성 시 디자인 폼 수정 시 오류가 발생할 수 있다.

Form 실행 즉시 실행되는 코드 작성

void InitializeComponent(void) 함수 안에 this->Load += gcnew System::EventHandler(this, &resourceDetail::MyForm_Load); 코드 작성 후
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) { 함수로 이벤트 작성하기

참고 : https://howbeautifulworld.tistory.com/43

profile
https://github.com/5tr1ker

0개의 댓글