프로그래밍/관리C++
이벤트 추가
에어버스
2018. 4. 8. 15:40
이벤트 추가
이벤트 추가는 위임(http://petra.tistory.com/1175)과 이벤트(http://petra.tistory.com/1178) 참조
이벤트 처리할 함수(51행)는 외부에서 호출되면 안되므로 private 로 지정하며
이벤트가 발생한 객체(컨트롤)의 포인터와 이벤트 정보는 갖는 EventArgs 포인터를 인수로 지정한다.
마지막으로, 34행처럼 클릭 이벤트에 이벤트 핸들러 인수로 Btn1_Clicked 를 지정하면 등록된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 |
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using namespace System::Drawing;
public delegate double NumericOp(double);
ref class Ops
{
public:
static double square(double d)
{
return d * d;
}
};
//__gc public CppForm : public Form // 1세대용
public ref class CppForm : public Form
{
private:
Button ^ btn1, ^btn2;
public:
CppForm()
{
Text = "Test Form";
Setup_Buttons();
Controls->Add(btn1);
Controls->Add(btn2);
btn1->Click += gcnew EventHandler(this, &CppForm::Btn1_Clicked);
//FormBorderStyle = ButtonBorderStyle::Fixed3D;
};
void Setup_Buttons()
{
btn1 = gcnew Button();
btn1->Text = "OK";
btn1->Size = System::Drawing::Size(70, 25);
btn1->Location = Point(130, 225);
btn2 = gcnew Button();
btn2->Text = "Cancel";
btn2->Size = System::Drawing::Size(70, 25);
btn2->Location = Point(210, 225);
}
private:
void Btn1_Clicked(Object^ pSender, EventArgs^ pArgs)
{
MessageBox::Show("OK 버튼 클릭", "메시지");
}
};
int main(array<System::String ^> ^args)
{
Application::Run(gcnew CppForm());
return 0;
} |
cs |
실행결과>