BN_CLICKED
-
I created a new class based on CButton -- CMyButton. I add a message notification handler in CMyButton.cpp for =BN_CLICKED to change an protected member variable of CMyButton.
void CMyButton::OnClicked()
{
m_nState += 1;
}Using resource editor, I put an instance of CMyButton to my dialog template. I want my dialog to handle the BN_CLICKED also, do some other stuff.
void CMyDlg::OnMyBtn()
{
MessageBox("Button pressed.");
}The problem is: CMyDlg::OnMyBtn is never called, even though I add the style of "Notify" to the button instance in the resource editor. What am I missing here? TIA!
-
I created a new class based on CButton -- CMyButton. I add a message notification handler in CMyButton.cpp for =BN_CLICKED to change an protected member variable of CMyButton.
void CMyButton::OnClicked()
{
m_nState += 1;
}Using resource editor, I put an instance of CMyButton to my dialog template. I want my dialog to handle the BN_CLICKED also, do some other stuff.
void CMyDlg::OnMyBtn()
{
MessageBox("Button pressed.");
}The problem is: CMyDlg::OnMyBtn is never called, even though I add the style of "Notify" to the button instance in the resource editor. What am I missing here? TIA!
You need an entry in the message map for
CMyDlg
that 'connects' your button to the handler.BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_BN_CLICKED(IDC_Control_ID, OnMyBtn)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()IDC_Control_ID is the resource ID for the button that you assigned when you created it. To get a feel for this, use the wizard to create a simple dialog application. In the resource editor, drop a button on the dialog. In VC6, right-click on the button you just added and select Events. In VS.NET, right-click and select Add Event Handler. This lets you add code to the dialog class to handle events from the button. Take a look at the code generated by the wizard. It adds code in the following places:
- A declaration is added to
MyDlg.h
for the button handler. This will be a line of the formafx_msg void OnMyButton();
- An entry is added to the message map like the example above.
- A definition of the handler is added to
MyDlg.cpp
, similar to the following:
void MyDlg::OnMyButton()
{
// handle the button
}
Software Zen:
delete this;
- A declaration is added to
-
You need an entry in the message map for
CMyDlg
that 'connects' your button to the handler.BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_BN_CLICKED(IDC_Control_ID, OnMyBtn)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()IDC_Control_ID is the resource ID for the button that you assigned when you created it. To get a feel for this, use the wizard to create a simple dialog application. In the resource editor, drop a button on the dialog. In VC6, right-click on the button you just added and select Events. In VS.NET, right-click and select Add Event Handler. This lets you add code to the dialog class to handle events from the button. Take a look at the code generated by the wizard. It adds code in the following places:
- A declaration is added to
MyDlg.h
for the button handler. This will be a line of the formafx_msg void OnMyButton();
- An entry is added to the message map like the example above.
- A definition of the handler is added to
MyDlg.cpp
, similar to the following:
void MyDlg::OnMyButton()
{
// handle the button
}
Software Zen:
delete this;
yes, I already had it. It seemed to me that the BN_CLICKED is processed by the button itself and the message never get to the dialog. I add the following to CMyButton::OnClicked:
GetParent()->PostMessage(WM\_COMMAND, MAKEWPARAM(this->GetDlgCtrlID(), BN\_CLICKED), LPARAM(this->m\_hWnd));
but it didn't help. :confused::( Any hint?
- A declaration is added to