: :
: : Digging through MS docs, I found an easier way...
: : declare the handler like this: afx_msg void OnDoSomething( UINT nID ); then you have the ID already in the handler :)
:
:
:
Well, not really. Two reasons(): (1) The OnButtonEvent() does not have a parameter, and (2) If you don't add that function to the message map it will never get called.
:
: If you have a dialog with two buttons, and use the wizard to create the event handler for each, where each button uses the same event, the results are like below, which in the original posters problem, is not what he wants
:
: BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
: //{{AFX_MSG_MAP(CTestDlg)
: ON_WM_SYSCOMMAND()
: ON_WM_PAINT()
: ON_WM_QUERYDRAGICON()
: ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
: ON_BN_CLICKED(IDC_BUTTON2, OnButton1)
: //}}AFX_MSG_MAP
: END_MESSAGE_MAP()
:
:
: // in the .h file
: // Generated message map functions
: //{{AFX_MSG(CTestDlg)
: virtual BOOL OnInitDialog();
: afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
: afx_msg void OnPaint();
: afx_msg HCURSOR OnQueryDragIcon();
: virtual void OnOK();
: afx_msg void OnButton1();
: //}}AFX_MSG
: DECLARE_MESSAGE_MAP()
:
:
My reply was in response to getting the ID of the pressed button when you use the ON_CONTROL_RANGE message map.. I've tried it and it works good. Using your previous example:
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
// this has to be outside the wizard created AFX_MSG_MAP (see msdn docs)
ON_CONTROL_RANGE(BN_CLICKED,100,110,OnAnyButton)
END_MESSAGE_MAP()
BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
CString str;
int top = 10;
int bottom = 30;
for(int i = 0; i < 10; i++)
{
m_ButtonArray[i] = new CButton;
str.Format("Button%d",i+1);
DWORD dwStyle = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON;
m_ButtonArray[i]->Create(str, dwStyle, CRect(10,top,100,bottom), this,100+i);
m_ButtonArray[i]->ShowWindow(TRUE);
top += 30;
bottom += 30;
}
return TRUE; // return TRUE unless you set the focus to a control
}
// event handle for the buttons
void CTestDlg::OnAnyButton( UINT nID )
{
CString s;
s.Format("button %d was pressed", nID - 100);
AfxMessageBox(s);
}
// in the header file
// Generated message map functions
//{{AFX_MSG(CTestDlg)
afx_msg void OnAnyButton( UINT nID );
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
To understand recursive, first you need to understand recursive