λ°˜μ‘ν˜•

μ§€λ‚œ ν¬μŠ€νŒ…μ—μ„œλŠ” Button Control 을 정적 μƒμ„±ν•˜λŠ” 방법을 μ„€λͺ…ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

μ΄λ²ˆμ—λŠ” λ™μ μœΌλ‘œ μƒμ„±ν•˜κ³  Button Event λ₯Ό μ–΄λ–»κ²Œ μ—°κ²°μ‹œν‚€λŠ”μ§€ μ„€λͺ…ν•˜κ² μŠ΅λ‹ˆλ‹€.

링크 : Button Control 정적 생성


μ•žμœΌλ‘œ μ„€λͺ…λ“œλ¦΄ λ‚΄μš©μ€, λ‹€μ„― 개의 λ²„νŠΌμ„ μƒμ„±ν•˜λŠ” 예제λ₯Ό λ³΄μ—¬λ“œλ¦΄ κ²ƒμž…λ‹ˆλ‹€.


1. Main Dialog Header 에 λ²„νŠΌ κ°œμˆ˜μ™€ 각 λ²„νŠΌμ˜ ID λ₯Ό μ •μ˜ν•©λ‹ˆλ‹€.

#define MAX_BTN 5
#define BTN_ID_1 10001
#define BTN_ID_2 10002
#define BTN_ID_3 10003
#define BTN_ID_4 10004
#define BTN_ID_5 10005


2. CButton 개체λ₯Ό μ„ μ–Έν•©λ‹ˆλ‹€. 볡수의 λ²„νŠΌμ„ κ°œλ³„μ μœΌλ‘œ 동적할당을 ν•˜κΈ°μœ„ν•΄ 2쀑 ν¬μΈν„°λ‘œ μ„ μ–Έν–ˆμŠ΅λ‹ˆλ‹€.

public :
    CButton** m_pBtn;


3. Class Wizard λ₯Ό μ‹€ν–‰ν•˜μ—¬ 'OnDestroy()' ν•¨μˆ˜λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€. 

μœˆλ„μš° μ’…λ£Œ μ‹œ λ™μ ν• λ‹Ήλœ λ²„νŠΌλ“€μ„ ν•΄μ œμ‹œν‚€κΈ° μœ„ν•΄ μΆ”κ°€ν•˜λŠ” μž‘μ—…μž…λ‹ˆλ‹€.

λ˜ν•œ, λ²„νŠΌμ΄ 클릭됐을 λ•Œ λ°œμƒν•  이벀트 ν•¨μˆ˜ 'OnButtonEvent(UINT ID)'λ₯Ό μ •μ˜ν•©λ‹ˆλ‹€.

public :
    afx_msg void OnDestroy();
    afx_msg void OnButtonEvent(UINT ID);


4. Main Dialog 의 μƒμ„±μžμ— CButton 개체λ₯Ό NULL Pointer 둜 μ΄ˆκΈ°ν™” ν•©λ‹ˆλ‹€.

CMyButtonDlg::CMyButtonDlg(CWnd* pParent /*NULL*/)
    : CDialogEx(IDD_MYBUTTON_DIALOG, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_pBtn = NULL;
}


5. 'OnInitDialog()'에 동적 생성 μ½”λ“œλ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.

BOOL CMyButtonDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	m_pBtn = new CButton*[MAX_BTN];

	CString caption = _T("");
	for (int i = 0; i < MAX_BTN; i++)
	{
		m_pBtn[i] = new CButton();
		caption.Format(_T("%d Button"), i);
		m_pBtn[i]->Create(caption, WS_CHILD | WS_VISIBLE |
			BS_PUSHBUTTON, CRect(10,10+(50*i),100,50+(50*i)), this, BTN_ID_1+i);
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}


6. 'OnDestroy()'μ—λŠ” 동적 μƒμ„±λœ μ»¨νŠΈλ‘€μ„ ν•΄μ œν•˜λŠ” μ½”λ“œλ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.

2쀑 ν¬μΈν„°λ‘œ μƒμ„±ν•˜μ˜€μœΌλ―€λ‘œ, 2μ€‘μœΌλ‘œ ν•΄μ œν•΄μ•Ό λ©”λͺ¨λ¦¬ λˆ„μˆ˜κ°€ λ°œμƒν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

void CMyButtonDlg::OnDestroy()
{
	CDialogEx::OnDestroy();

	if (m_pBtn != NULL)
	{
		for (int i = 0; i < MAX_BTN; i++)
		{
			delete m_pBtn[i];
			m_pBtn[i] = NULL;
		}
		delete[] m_pBtn;
	}
}


1~6 번 κΉŒμ§€κ°€ λ™μ μœΌλ‘œ λ²„νŠΌμ„ μƒμ„±ν•˜κ³  ν•΄μ œν•˜λŠ” λΆ€λΆ„μ΄μ—ˆμŠ΅λ‹ˆλ‹€. 이제 λ²„νŠΌμ„ λ§Œλ“€μ—ˆμœΌλ‹ˆ 클릭 μ΄λ²€νŠΈλ„ λ™μž‘ν•˜λ„λ‘ λ§Œλ“€μ–΄μ•Όκ² μ§€μš”?


7. Message Map 에 동적 μƒμ„±λœ λ²„νŠΌλ“€κ³Ό 이벀트 ν•¨μˆ˜λ₯Ό λ“±λ‘ν•©λ‹ˆλ‹€.

BEGIN_MESSAGE_MAP(CMyButtonDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_COMMAND_RANGE(BTN_ID_1, BTN_ID_5, OnButtonEvent)
END_MESSAGE_MAP()


8. 'OnButtonEvent(UINT ID)' μ—λŠ” λ‹€μŒκ³Ό 같이 μ½”λ“œλ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.

λ²„νŠΌ 클릭 μ‹œ λͺ‡ 번째 λ²„νŠΌμ΄ λˆŒλ ΈλŠ”μ§€ λ©”μ‹œμ§€ 창이 λ°œμƒν•˜λ„λ‘ μ½”λ“œλ₯Ό μž‘μ„±ν–ˆμŠ΅λ‹ˆλ‹€.

void CMyButtonDlg::OnButtonEvent(UINT ID)
{
	CString msg = _T("");
	msg.Format(_T("%d Button Click!"), ID - BTN_ID_1);

	AfxMessageBox(msg);
}


9. μ‹€ν–‰ν•˜λ©΄ λ‹€μŒκ³Ό 같이 λ™μž‘ν•©λ‹ˆλ‹€.


μžμ„Έν•œ μ½”λ“œλŠ” μ²¨λΆ€λœ 예제λ₯Ό 확인해 μ£Όμ„Έμš”.

Dynamic_MyButton.zip


λ°˜μ‘ν˜•