Link to home
Start Free TrialLog in
Avatar of elstcb
elstcb

asked on

Message Map Array

Hi all,

I've got the following code in the visual C++ message map:
...
BEGIN_MESSAGE_MAP(TestDlg, CDialog)
     //{{AFX_MSG_MAP(TestDlg)
     ON_BN_CLICKED          (IDC_BUTTON_BROWSE_1,                    OnButtonBrowse1)
     ON_BN_CLICKED          (IDC_BUTTON_BROWSE_2,                    OnButtonBrowse2)
     ON_BN_CLICKED          (IDC_BUTTON_BROWSE_3,                     OnButtonBrowse3)
     ON_BN_CLICKED          (IDC_BUTTON_BROWSE_4,                    OnButtonBrowse4)
     ON_BN_CLICKED          (IDC_BUTTON_BROWSE_5,                    OnButtonBrowse5)
     ON_BN_CLICKED          (IDC_BUTTON_BROWSE_6,                    OnButtonBrowse6)
...

however each function call contains identical code (apart from one integer value) which seems a bit pointless. What I really want to do is something like:

...
BEGIN_MESSAGE_MAP(TestDlg, CDialog)
     //{{AFX_MSG_MAP(TestDlg)
     ON_BN_CLICKED          (IDC_BUTTON_BROWSE_1,                    OnButtonBrowse(1))
     ON_BN_CLICKED          (IDC_BUTTON_BROWSE_2,                    OnButtonBrowse(2))
     ON_BN_CLICKED          (IDC_BUTTON_BROWSE_3,                     OnButtonBrowse(3))
     ON_BN_CLICKED          (IDC_BUTTON_BROWSE_4,                    OnButtonBrowse(4))
     ON_BN_CLICKED          (IDC_BUTTON_BROWSE_5,                    OnButtonBrowse(5))
     ON_BN_CLICKED          (IDC_BUTTON_BROWSE_6,                    OnButtonBrowse(6))
...

so that one function is called passing this integer. However when I tried this I got a static declaration error.

Any ideas?

Thanks in advance,

Steve

Avatar of mblat
mblat

Well you get an error , cause of the way ON_BN_CLICKED macro is defined.

What you can do is this:

1. Make sure that #define are sequensial in resource.h

i.e.

#define IDC_BUTTON1 1000
#define IDC_BUTTON2 1001
....
#define IDC_BUTTON6 1006

override DefWindowProc

LRESULT CKeyboardDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    if(message == WM_COMMAND)
    {
        if(BN_CLICKED == HIWORD(wParam))
        {
            WORD nId = LOWORD(wParam);
            if(nId >= IDC_BUTTON1 && nId <= IDC_BUTTON6)
                OnButtonPressed(IDC_BUTTON6 - nId);
        }
    }
     
    return CDialog::DefWindowProc(message, wParam, lParam);
}

You can also accompish the same by ovewriting OnCommand or using ON_COMMAND_RANGE ( I think)

Diadvantage here is very visible - if somehow you defines get out of wack you are done, so I don't like doing it ( but sometimes I do)

Hope it helps.
Hi,

mblat is right, but there is more simple way if you use MFC
add to the message map:
ON_CONTROL_RANGE (BN_CLICKED, IDC_BUTTON1, IDC_BUTTON2, OnBtnClicked)
and the handler:
CDlg::OnBtnClicked(UINT nCtrlID)

HTH,
jemax
Avatar of Axter

You could try the following:

#define OnButtonBrowse(x) OnButtonBrowse1+x-1
BEGIN_MESSAGE_MAP(TestDlg, CDialog)
    //{{AFX_MSG_MAP(TestDlg)
    ON_BN_CLICKED          (IDC_BUTTON_BROWSE_1,                    OnButtonBrowse(1))
    ON_BN_CLICKED          (IDC_BUTTON_BROWSE_2,                    OnButtonBrowse(2))
    ON_BN_CLICKED          (IDC_BUTTON_BROWSE_3,                     OnButtonBrowse(3))
    ON_BN_CLICKED          (IDC_BUTTON_BROWSE_4,                    OnButtonBrowse(4))
    ON_BN_CLICKED          (IDC_BUTTON_BROWSE_5,                    OnButtonBrowse(5))
    ON_BN_CLICKED          (IDC_BUTTON_BROWSE_6,                    OnButtonBrowse(6))
...
Avatar of elstcb

ASKER

Thanks for all your replies, I had a go at all of them but ran in to a few issues.

mblat,
My #define's aren't in order...! I'd quite like to re-order them and in some cases renumber them anyway so that the code is a bit neater to read. How do I go about renumbering them? I realise I could just do:
if (nld == IDC_BUTTON1 || nld == IDC_BUTTON2 || nld...){
...
}
but figure this is pretty messy.


jemax,
I got an overload member function: void (unsigned int) error. Will try again in a sec to see if I missed something.


Axter,
I just got lots of compile errors terminating with a fatal error... :-(


Thanks,

Steve
ASKER CERTIFIED SOLUTION
Avatar of jemax
jemax

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elstcb

ASKER

jemax,

Cool yeah got that going now. How do I reorder/reassign the id's? Is it just a case of moving the controls around a leaving the numbers put or can I change the numbers somehow (so that I can set up neat ranges for different controls - e.g. 2000 - 2006 for my 7 buttons)?

Thanks,

Steve
Avatar of elstcb

ASKER

aha, got it!

Thanks again to everyone that helped...

mblat I've posted some points for you here:
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20327261

Cheers,

Steve