title: Dynamically Created Button Events in MFC Doesn't Fire After Clicking the Button
body: <p>So i was asked to make a bunch of buttons in MFC to update the app with new functions. </p>
  
  <p>What have i done is </p>
  
  <p>a. Reserving the ID in resource.h</p>
  
  <pre><code>#define IDC_MAINFRM_BTN_1               40501
  #define IDC_MAINFRM_BTN_2               40502
  #define IDC_MAINFRM_BTN_3               40503
  #define IDC_MAINFRM_BTN_4               40504
  #define IDC_MAINFRM_BTN_5               40505
  </code></pre>
  
  <p>I also have made sure that there's no duplicate ID in the resource.h</p>
  
  <p>b. To create the button i have inserted these codes in the     CMainFrame::OnCreate() function</p>
  
  <pre><code>//Custom function start
  cButtonA = new CButton();
  cButtonB = new CButton();
  cButtonC = new CButton();
  cButtonD = new CButton();
  cButtonE = new CButton();
  cButtonA-&gt;Create(_T("My button 1"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(btnLeft1, btnTop1, btnRight1, btnBottom1), this, IDC_MAINFRM_BTN_1);
  cButtonB-&gt;Create(_T("My button 2"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(btnLeft1, btnTop2, btnRight1, btnBottom2), this, IDC_MAINFRM_BTN_2);
  cButtonC-&gt;Create(_T("My button 3"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(btnLeft1, btnTop3, btnRight1, btnBottom3), this, IDC_MAINFRM_BTN_3);
  cButtonD-&gt;Create(_T("My button 4"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(btnLeft2, btnTop1, btnRight2, btnBottom1), this, IDC_MAINFRM_BTN_4);
  cButtonE-&gt;Create(_T("My button 5"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(btnLeft2, btnTop2, btnRight2, btnBottom2), this, IDC_MAINFRM_BTN_5);
  </code></pre>
  
  <p>Header File:</p>
  
  <pre><code>CButton*                cButtonA; //First Button
  CButton*                cButtonB; //Second Button
  CButton*                cButtonC; //Third Button
  CButton*                cButtonD; //Fourth Button
  CButton*                cButtonE; //Fifth Button
  </code></pre>
  
  <p>c. For the message map i have tried both of these and neither of them works or catch the functions as for the ShowTestMessage function it only just contain a placeholder AfxMessageBox code. Tried putting breakpoints on those too and it still doesn't work.</p>
  
  <pre><code>BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
  //{{AFX_MSG_MAP(CMainFrame)
  //Method 1
  ON_COMMAND_RANGE(IDC_MAINFRM_BTN_1, IDC_MAINFRM_BTN_13, &amp;CMainFrame::ShowTestMessage)
  
  //Method 2
  ON_BN_CLICKED(IDC_MAINFRM_BTN_1, &amp;CMainFrame::ShowTestMessage)
  </code></pre>
  
  <p>d. I also have made sure that the functions in MainFrm.h who handle the events has afx_msg prefix. </p>
  
  <pre><code>    protected:
      //{{AFX_MSG(CMainFrame
      afx_msg void ShowTestMessage();
      afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP(
  </code></pre>
  
  <p>Other things that i have tried intercepting the message by adding some line of codes in PreTranslateMessage</p>
  
  <pre><code>if (pMsg-&gt;message == BN_CLICKED){ //This doesn't work
      AfxMessageBox("Ohlala", MB_OK);
  }
  
  if (pMsg-&gt;message == WM_LBUTTONDOWN){ //This one work
      AfxMessageBox("Ohlala", MB_OK);
  }
  </code></pre>
  
  <p>Then i tried changing the event handle to </p>
  
  <pre><code>void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)
  </code></pre>
  
  <p>Yet this one also doesn't fire (Since the breakpoint isn't triggered)</p>
  
  <p>So did i miss something? I have to admit i'm not really good at MFC. I tried searching for all the probable mistake that i probably made yet i still can't find out why the event doesn't fire. @__@</p>
  
  <p>EDIT1: If it helps i'm developing this in Visual Studio 2013 and this project (maybe) use MDI judging by the class declaration that i found in the header.</p>
  
  <pre><code>class CMainFrame : public CMDIFrameWnd
  </code></pre>