How to add a message handler function to a control at run-time?
-
hello, I use MFC and new I need to create controls (buttons) on-the-fly and add message handlers to them for the BN_CLICKED event at the time they are created. I see how the messagemap functionality can add handlers for the controls that I already have on my form, but what about dynamically created. I do not wish to create an excessive amount of controls and just hide the ones that are inappropriate at the time.. (cause: where di I set the limit as to what is a safe amount of controls then..) thankful for any help I can get.
-
hello, I use MFC and new I need to create controls (buttons) on-the-fly and add message handlers to them for the BN_CLICKED event at the time they are created. I see how the messagemap functionality can add handlers for the controls that I already have on my form, but what about dynamically created. I do not wish to create an excessive amount of controls and just hide the ones that are inappropriate at the time.. (cause: where di I set the limit as to what is a safe amount of controls then..) thankful for any help I can get.
Hi, You can intercept the button down message (WM_LBUTTONDOWN) or any button in the PreTranslateMessage(MSG* pMsg):
BOOL CYourDialog::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_LBUTTONDOWN)
{
CWnd *wnd = CWnd::FromHandle(pMsg->hwnd);
int ID = wnd->GetDlgCtrlID();
if(ID == IDC_OWNBUTTON1)
{
MessageBox("IDC_OWNBUTTON1");
return TRUE;
}
// ....
if(ID == IDC_OWNBUTTON_N)
{
MessageBox("IDC_OWNBUTTON_N");
return TRUE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}The IDC_OWNBUTTON1 ... IDC_OWNBUTTON_N is an custom control id's Ivan Cachicatari www.latindevelopers.com