๋ฐ˜์‘ํ˜•

์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ ๋ฉ”์ธ ๋‹ค์ด์–ผ๋กœ๊ทธ์— ์ƒˆ๋กœ์šด ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋ถ™์ด๋Š” ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ๋“œ๋ ธ์—ˆ์Šต๋‹ˆ๋‹ค.
[MFC] Dialog์— Form(Dialog) ๋ถ™์ด๊ธฐ(์˜ˆ์ œ ํฌํ•จ)

 

[MFC] Dialog์— Form(Dialog) ๋ถ™์ด๊ธฐ(์˜ˆ์ œ ํฌํ•จ)

Dialog์— Dialog๋ฅผ ๋ถ™์ธ๋‹ค? ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ์— ์ƒˆ๋กœ์šด ๋‹ค์ด์–ผ๋กœ๊ทธ(CFormView ๊ธฐ๋ฐ˜)๋ฅผ ๋ถ™์ด๋Š” ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒŒ ๋ฌด์Šจ ์˜๋ฏธ์ธ์ง€ ๊ฐ์ด ์•ˆ ์˜ค์‹œ์ฃ ? ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ์•„๋ž˜ ์ด๋ฏธ์ง€๋ฅผ..

luckygg.tistory.com

Form ์Šค์œ„์นญ? Tab Control๊ณผ ๋‹ค๋ฅธ ์ ์€?

์ด๋ฒˆ์—๋Š” ์ด ๋ฐฉ๋ฒ•์„ ์‘์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ Form์„ ์Šค์œ„์นญํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ด ๊ธฐ๋Šฅ์€ MFC์˜ Tab Control๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ์ธ๋ฐ์š”. ์•„์‹œ๋‹ค์‹œํ”ผ MFC UI ์ปจํŠธ๋กค์˜ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•์—๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฒˆ์— ์†Œ๊ฐœํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์–ด๋ ต์ง€๋„ ์•Š๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š”๋Œ€๋กœ ๊พธ๋ฐ€ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ์žฅ์ ์ž…๋‹ˆ๋‹ค.

๋จผ์ € ๋™์ž‘์„ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

Form Switching ์˜ˆ์‹œ

์ด ์˜ˆ์ œ๋Š” ๋ผ๋””์˜ค ๋ฒ„ํŠผ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๊ผญ ๋ผ๋””์˜ค ๋ฒ„ํŠผ์ผ ํ•„์š”๋Š” ์—†์ง€๋งŒ, ์ง๊ด€์ ์ธ ์‹œ๊ฐ ํšจ๊ณผ๋ฅผ ์œ„ํ•ด ๋ผ๋””์˜ค ๋ฒ„ํŠผ์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ ์ฝ”๋“œ Concept

์ด์ „ ์˜ˆ์ œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Main Dialog์— ์Šค์œ„์นญ ๋˜๋Š” Form์ด ๋ณด์—ฌ์งˆ Picture Control์„ ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Form ๊ฐœ์ˆ˜์™€ ๋™์ผํ•œ ๊ฐœ์ˆ˜์˜ Radio Button์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. Radio ๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜๋ฉด ํ•ด๋‹น Form์ด ๋ณด์—ฌ์ง€๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์˜ˆ์ œ ํ”„๋กœ๊ทธ๋žจ ์ปจ์…‰

์Šค์œ„์นญ ๋˜๋Š” Form์˜ ํฌ๊ธฐ๊ฐ€ ์ œ๊ฐ๊ฐ์ธ๋ฐ์š”. Form์ด Picture control๋ณด๋‹ค ํฌ๋ฉด ์Šคํฌ๋กค๋ฐ”๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , ์ž‘์œผ๋ฉด ์ƒ์„ฑ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ ํ•˜๊ธฐ

์˜ˆ์ œ ์ฝ”๋“œ๋Š” ๋ณธ๋ฌธ ํ•˜๋‹จ์— ์ฒจ๋ถ€๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ค‘์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์„ค๋ช…ํ•˜๊ณ , ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ฒจ๋ถ€๋œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”!

๋ฆฌ์†Œ์Šค ํŽธ์ง‘๊ธฐ์—์„œ ์—ฌ๋Ÿฌ Dialog๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๋”๋ถˆ์–ด, Dialog์— ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ๋ฐ˜๋“œ์‹œ CFormView๋ฅผ ๊ธฐ๋ณธ ํด๋ž˜์Šค๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ Main Dialog์˜ UI ๋ฐฐ์น˜ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.

Main Dialog

์Šค์œ„์นญ ๋  Form์€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

์Šค์œ„์นญ ๋  Form

๊ฐ Form ํด๋ž˜์Šค์˜ ํ—ค๋” ํŒŒ์ผ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ์—์„œ๋Š” ๊ฐ Form์˜ ํด๋ž˜์Šค ๋ช…์„ CMyForm1~3์œผ๋กœ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ๊ฐ€์ƒ ํ•จ์ˆ˜๋กœ Create์™€ OnInitialUpdate๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

#pragma once


// CMyForm1 ๋Œ€ํ™” ์ƒ์ž

class CMyForm1 : public CFormView
{
	DECLARE_DYNAMIC(CMyForm1)

public:
	CMyForm1();
	CMyForm1(UINT nIDTemplate);
	virtual ~CMyForm1();

// ๋Œ€ํ™” ์ƒ์ž ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค.
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_FORM_FORM1 };
#endif

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV ์ง€์›์ž…๋‹ˆ๋‹ค.

	DECLARE_MESSAGE_MAP()
public:
	virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
	virtual void OnInitialUpdate();
};

Main Dialog์˜ ํ—ค๋” ํŒŒ์ผ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. Main Dialog์— Form์„ ๋™์  ์ƒ์„ฑํ•˜๋Š” AllocForms()์™€ Radio Button์ด ์„ ํƒ๋  ๋•Œ, ์„ ํƒ๋œ Form์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ShowForm(int idx)ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

// ExamFormSwitchingDlg.h: ํ—ค๋” ํŒŒ์ผ
//

#pragma once
#include "CMyForm1.h"
#include "CMyForm2.h"
#include "CMyForm3.h"

// CExamFormSwitchingDlg ๋Œ€ํ™” ์ƒ์ž
class CExamFormSwitchingDlg : public CDialogEx
{
// ์ƒ์„ฑ์ž…๋‹ˆ๋‹ค.
public:
	CExamFormSwitchingDlg(CWnd* pParent = nullptr);	// ํ‘œ์ค€ ์ƒ์„ฑ์ž์ž…๋‹ˆ๋‹ค.

// ๋Œ€ํ™” ์ƒ์ž ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค.
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ExamFormSwitching_DIALOG };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV ์ง€์›์ž…๋‹ˆ๋‹ค.

	CMyForm1 *m_pForm1;
	CMyForm2 *m_pForm2;
	CMyForm3 *m_pForm3;
	
	void AllocForms();
	void ShowForm(int idx);
// ๊ตฌํ˜„์ž…๋‹ˆ๋‹ค.
protected:
	HICON m_hIcon;

	// ์ƒ์„ฑ๋œ ๋ฉ”์‹œ์ง€ ๋งต ํ•จ์ˆ˜
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnDestroy();
	afx_msg void OnBnClickedRadioForm1();
	afx_msg void OnBnClickedRadioForm2();
	afx_msg void OnBnClickedRadioForm3();
};

Form์ด ์ƒ์„ฑ๋˜๋Š” AllocForms()ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋งŽ์ฃ ? ์ด๋ ‡๊ฒŒ ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๋Š” ํ•จ์ˆ˜ํ™”ํ•˜๊ฑฐ๋‚˜ ์ƒ์† ๊ด€๊ณ„์˜ ๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๊ฐ„์†Œํ™”ํ•˜๋ฉด ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

void CExamFormSwitchingDlg::AllocForms()
{
	CCreateContext context;
	ZeroMemory(&context, sizeof(context));

	CRect rectOfPanelArea;

	GetDlgItem(IDC_STATIC_RECT)->GetWindowRect(&rectOfPanelArea);
	ScreenToClient(&rectOfPanelArea);
	
	m_pForm1 = new CMyForm1();
	m_pForm1->Create(NULL, NULL, WS_CHILD | WS_VSCROLL | WS_HSCROLL, rectOfPanelArea, this, IDD_FORM_FORM1, &context);
	m_pForm1->OnInitialUpdate();
	m_pForm1->ShowWindow(SW_SHOW);

	m_pForm2 = new CMyForm2();
	m_pForm2->Create(NULL, NULL, WS_CHILD | WS_VSCROLL | WS_HSCROLL, rectOfPanelArea, this, IDD_FORM_FORM2, &context);
	m_pForm2->OnInitialUpdate();
	m_pForm2->ShowWindow(SW_HIDE);

	m_pForm3 = new CMyForm3();
	m_pForm3->Create(NULL, NULL, WS_CHILD | WS_VSCROLL | WS_HSCROLL, rectOfPanelArea, this, IDD_FORM_FORM3, &context);
	m_pForm3->OnInitialUpdate();
	m_pForm3->ShowWindow(SW_HIDE);

	GetDlgItem(IDC_STATIC_RECT)->DestroyWindow();
}

๋งˆ์ง€๋ง‰์œผ๋กœ ShowForm(int idx)๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ์ธ๋ฑ์Šค๋ฅผ ์ธ์ž๋กœ ๋„˜๊ธฐ๋ฉด, ํ•ด๋‹น๋˜๋Š” Form์„ ๋ณด์—ฌ์ฃผ๊ณ  ๋‚˜๋จธ์ง€๋Š” ์ˆจ๊ธฐ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

void CExamFormSwitchingDlg::ShowForm(int idx)
{
	switch (idx)
	{
	case 0:
		m_pForm1->ShowWindow(SW_SHOW);
		m_pForm2->ShowWindow(SW_HIDE);
		m_pForm3->ShowWindow(SW_HIDE);
		break;
	case 1:
		m_pForm1->ShowWindow(SW_HIDE);
		m_pForm2->ShowWindow(SW_SHOW);
		m_pForm3->ShowWindow(SW_HIDE);
		break;
	case 2:
		m_pForm1->ShowWindow(SW_HIDE);
		m_pForm2->ShowWindow(SW_HIDE);
		m_pForm3->ShowWindow(SW_SHOW);
		break;
	}
}

์ •๋ง ๊ฐ„๋‹จํ•˜์ฃ ? ์—ฌ๊ธฐ์„œ Radio Button์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•˜์—ฌ ์˜ˆ์˜๊ฒŒ ๊พธ๋ฉฐ์ฃผ๋ฉด ๋”์šฑ ๋ฉ‹์ง„ UI๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. MFC์—์„œ UI๋Š” ์ฝ”๋”ฉํ•˜๊ธฐ ๋‚˜๋ฆ„์ธ ์ ์„ ๊ผญ ๊ธฐ์–ตํ•˜์„ธ์š”!

ExamFormSwitching.zip
0.14MB

๋ฐ˜์‘ํ˜•