Introduction
This article is about how we can create controls dynamically without using wizard and how to handle notification for dynamically created control. Here I will try to explain it by the notification BN_CLICKED for a button.
Working
Following three things we need to do for creating the button control dynamically.
1. Need to define a button ID in the “resource.h” file and that should be given a unique integer value.
Code: Cpp
#define IDC_BUTTON1 4652
Code: Cpp
BOOL CMyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
/// “m_wndPushButton” is a member variable of CButton class.
m_wndPushButton.Create (_T ("Push Button"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
CRect(100, 100, 150, 150),
this, IDC_BUTTON1);
return TRUE;
}
Code: Cpp
class CMyDialog: public CDialog
{ // Construction
public:
CMyDialog (CWnd* pParent = NULL); // standard constructor
protected:
virtual void DoDataExchange(CDataExchange* pDX);
DECLARE_MESSAGE_MAP()
};
Code: Cpp
afx_msg void OnButton1Clicked();
Code: Cpp
void CMyDialog :: OnButton1Clicked()
{
/////////////
}
Code: Cpp
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
//{{AFX_MSG_MAP(CMyDialog)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1Clicked)
//}}AFX_MSG_MAPEND_MESSAGE_MAP()

