포스트

(MFC) 메시지 박스 프로그램 -3

MessageBox 프로그램 작성

[MFC] -3

  • 최초 작성일: 2022년 2월 23일 (수)

##

1. ‘MessageBoxes’ .

2. .


1. .

image


2. [MFC?] .

image


3. 6 .

image


##

###

아래와 같이 MFC Application을 생성해준다. 프로젝트명은 MessageBoxes로 했다.

image


그리고, Application type (응용 프로그램 종류)는 ‘Dialog based (대화 상자 기반)’를 선택하고 [Finish] 버튼을 클릭한다.

image


###

그러면 아래와 같은 창이 나오는데, 안 나온다면 [Ctrl]-[Shift]-[R] 혹은 [메뉴]-[보기]-[다른 창]-[리소스 뷰]를 클릭하면 된다.

image


그러면, 가운데 Dialog에 있는 모든 컨트롤들을 삭제해준다. ( [Ctrl + A] + [Delete] )

image


컨트롤과 속성값은 아래와 같이 설정한다.

image image


###

버튼을 누를 때마다 Edit Control에 출력하기 위해 멤버 변수를 추가해주어야 한다.

멤버 함수를 추가하기 위해서 [메뉴]-[프로젝트]-[클래스 마법사] 또는, [Ctrl + Shift + X] 키를 눌러 [클래스 마법사]를 실행시킨다.

그럼, 아래의 창이 뜨는데 거기서 [Member Variables(멤버 변수)] 탭에서 ‘IDC_EDIT_RESULT’를 클릭한 후 ‘Add Variable(변수 추가)’ 버튼을 클릭한다.

Control Variable(제어 변수) 창에서 다음과 같이 설정하고 Finish 버튼을 클릭한다. 그 다음, 멤버 변수 ‘m_strResult’가 추가된 것을 확인하고 확인 버튼을 누른다.

image

image


또한 [클래스 뷰]-[MessageBoxes]-[CMessageBoxesDlg] 에서도 ‘m_strResult’가 추가된 것을 확인할 수 있다.

image


###

이제, 버튼을 눌렀을 때 함수를 실행하기 위해 멤버 함수를 추가해주어야 한다.

클래스 마법사를 키고 IDC_BUTTON_YN [명령], BN_CLICKED [메시지] 를 클릭하고 [처리기 추가] 를 눌러 멤버 함수들을 아래와 같이 추가한다.

image


그 다음, 아래와 같이 코드를 삽입해주고, 프로그램을 빌드한 후 실행한다.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
void CMessageBoxesDlg::OnClickedButtonYnc()
{
	// TODO: Add your control notification handler code here
	int iResults;
	m_strResult = _T("YES/NO/CANCEL 버튼을 눌렀습니다.");
	UpdateData(FALSE);
	iResults = AfxMessageBox(_T("YES/NO/CANCEL 버튼을 눌렀습니다."), (MB_YESNOCANCEL | MB_ICONINFORMATION));

	if (iResults == IDYES)
	{
		m_strResult = _T("OK 버튼을 눌렀습니다!");
		UpdateData(FALSE);
	}
	if (iResults == IDNO)
	{
		m_strResult = _T("NO 버튼을 눌렀습니다1");
		UpdateData(FALSE);
	}
	if (iResults == IDCANCEL)
	{
		m_strResult = _T("CANCEL 버튼을 눌렀습니다!");
		UpdateData(FALSE);
	}
}

void CMessageBoxesDlg::OnClickedButtonYn()
{
	// TODO: Add your control notification handler code here
	int iResults;
	m_strResult = _T("YES/NO 버튼을 눌렀습니다.");
	UpdateData(FALSE);
	iResults = AfxMessageBox(_T("YES/NO 버튼을 눌렀습니다."), (MB_YESNO | MB_ICONWARNING));

	if (iResults == IDYES)
	{
		m_strResult = _T("YES 버튼을 눌렀습니다!");
		UpdateData(FALSE);
	}
	if (iResults == IDNO)
	{
		m_strResult = _T("NO 버튼을 눌렀습니다!");
		UpdateData(FALSE);
	}
}

void CMessageBoxesDlg::OnClickedButtonRc()
{
	// TODO: Add your control notification handler code here
	int iResults;
	m_strResult = _T("RETRY/CANCEL 버튼을 눌렀습니다.");
	UpdateData(FALSE);
	iResults = AfxMessageBox(_T("RETRY/CANCEL 버튼을 눌렀습니다."), (MB_RETRYCANCEL | MB_ICONQUESTION));

	if (iResults == IDRETRY)
	{
		m_strResult = _T("RETRY 버튼을 눌렀습니다!");
		UpdateData(FALSE);
	}
	if (iResults == IDCANCEL)
	{
		m_strResult = _T("CANCEL 버튼을 눌렀습니다!");
		UpdateData(FALSE);
	}
}

void CMessageBoxesDlg::OnClickedButtonOk()
{
	// TODO: Add your control notification handler code here
	int iResults{};
	m_strResult = _T("OK 버튼을 눌렀습니다!");
	UpdateData(FALSE);

	AfxMessageBox(_T("OK 버튼을 눌렀습니다."), MB_ICONERROR);
}

void CMessageBoxesDlg::OnClickedButtonOc()
{
	// TODO: Add your control notification handler code here
	int iResults;
	m_strResult = _T("OK/CANCEL 버튼을 눌렀습니다.");
	UpdateData(FALSE);
	iResults = AfxMessageBox(_T("OK/CANCEL 버튼을 눌렀습니다."), (MB_OKCANCEL | MB_ICONSTOP));

	if (iResults == IDOK)
	{
		m_strResult = _T("OK 버튼을 눌렀습니다!");
		UpdateData(FALSE);
	}
	if (iResults == IDCANCEL)
	{
		m_strResult = _T("CANCEL 버튼을 눌렀습니다!");
		UpdateData(FALSE);
	}
}

void CMessageBoxesDlg::OnClickedButtonMfc()
{
	// TODO: Add your control notification handler code here
	m_strResult = _T("MFC 버튼을 눌렀습니다!");
	UpdateData(FALSE);

	MessageBox(_T("Microsoft Foundation Class 입니다."));
}

void CMessageBoxesDlg::OnClickedButtonExit()
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CMessageBoxesDlg::OnClickedButtonAri()
{
	// TODO: Add your control notification handler code here
	int iResults;
	m_strResult = _T("ABORT/RETRY/IGNORE 버튼을 눌렀습니다!");
	UpdateData(FALSE);
	iResults = AfxMessageBox(_T("ABORT/RETRY/IGNORE 버튼을 눌렀습니다."), (MB_ABORTRETRYIGNORE | MB_ICONINFORMATION));
	
	if (iResults == IDABORT)
	{
		m_strResult = _T("ABORT 버튼을 눌렀습니다!");
		UpdateData(FALSE);
	}
	if (iResults == IDRETRY)
	{
		m_strResult = _T("RETRY 버튼을 눌렀습니다!");
		UpdateData(FALSE);
	}
	if (iResults == IDIGNORE)
	{
		m_strResult = _T("IGNORE 버튼을 눌렀습니다!");
		UpdateData(FALSE);
	}
}


###

image

image image image image image image

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.