IT/C++, MFC

모달과 모달리스(Modal and Modaless)

가성비몬 2022. 7. 6. 11:05

 

모달과 모달리스(Modal and Modaless)

 

To create a modal dialog box, call either of the two public constructors declared in CDialog. Next, call the dialog object's DoModal member function to display the dialog box and manage interaction with it until the user chooses OK or Cancel. This management by DoModal is what makes the dialog box modal. For modal dialog
boxes, DoModal loads the dialog resource.

모달 대화 상자를 만들려면 CDialog에 선언된 두 개의 공용 생성자 중 하나를 호출하십시오. 그런 다음 대화 상자 개체의 DoModal 멤버 함수를 호출하여 사용자가 확인 또는 취소를 선택할 때까지 대화 상자를 표시하고 대화 상자와의 상호 작용을 관리합니다. DoModal에 의한 이러한 관리는 대화 상자를 모달로 만드는 것입니다. 모달 대화 상자의 경우 상자에서 DoModal은 대화 상자 리소스를 로드합니다.

https://docs.microsoft.com/en-us/cpp/mfc/creating-modal-dialog-boxes?view=msvc-170
For a modeless dialog box, you must provide your own public constructor in your dialog class. To create a modeless dialog box, call your public constructor and then call the dialog object's Create member function to load the dialog resource. You can call Create either during or after the constructor call. If the dialog resource has the property WS_VISIBLE, the dialog box appears immediately. If not, you must call its ShowWindow member function.

모덜리스 대화 상자의 경우 대화 클래스에 고유한 공용 생성자를 제공해야 합니다. 모덜리스 대화 상자를 만들려면 공용 생성자를 호출한 다음 대화 개체의 Create 멤버 함수를 호출하여 대화 상자 리소스를 로드합니다. 생성자 호출 중이나 호출 후에 Create를 호출할 수 있습니다. 대화 상자 리소스에 WS_VISIBLE 속성이 있으면 대화 상자가 즉시 나타납니다. 그렇지 않은 경우 해당 ShowWindow 멤버 함수를 호출해야 합니다.


https://docs.microsoft.com/en-us/cpp/mfc/creating-modeless-dialog-boxes?view=msvc-170

간단히 설명하면 모달(Modal)은 뒤로 갈 수 없는, 모달리스(Modaless)는 뒤로 갈 수 있는 있는 것이다.

 

 

모달과 모달리스(Modal and Modaless)에 대한 예시


1. 새 Dlg 리소스 생성
2. 새 Dlg에 Class 생성
3. 새 Dlg에 함수 추가
4. 부모 Dlg에서 적절히 호출

 

1. File - New - Project 클릭해서 새로운 프로젝트 생성

 

1. Create a new project - MFC app 클릭

 

2. Configure your new project에서 기본 설정으로 Create 클릭하여 프로젝트 구성

 

3. MFC Application - Application type - Dialog Based - Finish 클릭해서 프로젝트 생성 완료

 

4. Resource View - MFCApplication10 - MFCApplication10.rc - Dialog - IDD_MFCAPPLICATION숫자_DIALOG 더블 클릭 후 오른쪽 Toolbox 클릭해서 Dialog창에 Button을 생성

 

5. Resource View - Add Resoruce 클릭

 

5. Dialog - New 클릭

 

6. Properties - ID 이름 변경

 

6. IDD_MFCAPPLICATION_DIALOG 누르고 오른쪽 마우스 클릭 후 Add MFC Class 클릭

 

7. IDD_MFCAPPLICATION_DIALOG - Button 더블클릭

 

void CMFCApplication10Dlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	CNewDlg child; // 자손 클래스 CNewDlg 생성

	child.DoModal(); // 창을 띄운다.

}

 

7. DoModal 함수를 이용한 창 생성
8. Class View에서 해당 Dlg에 커서 갖다 놓고 오른쪽 마우스 클릭해서 Add - Add function

 

8. Function name 작성, Return type : void, Parameters : CString msg

 

void CMFCApplication10Dlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	CNewDlg child;
	
	child.SetEditBox(_T("하이 Hello")); // 미리 자료를 만들어놓고
	
	child.DoModal(); // 창을 띄운다.

	AfxMessageBox(_T("창이 닫혔습니다."));
	
}

 

9. Build - Local Windows Debugger 후 Button 클릭

 

9.  Dialog창 닫으면 "창이 닫혔습니다." 메세지 출력