Solved

Message Map Array

Posted on 2002-07-23
7
533 Views
Last Modified: 2008-02-01
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

0
Comment
Question by:elstcb
7 Comments
 
LVL 4

Expert Comment

by:mblat
ID: 7172479
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.
0
 
LVL 1

Expert Comment

by:jemax
ID: 7172482
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
0
 
LVL 30

Expert Comment

by:Axter
ID: 7172646

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))
...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:elstcb
ID: 7173823
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
0
 
LVL 1

Accepted Solution

by:
jemax earned 50 total points
ID: 7173856
Hi,

into the message map:
ON_CONTROL_RANGE (BN_CLICKED, IDC_BUTTON1, IDC_BUTTON2, OnBtnClicked)

in your dlg.h:
//}}AFX_MSG
...
afx_msg void OnBtnClicked(UINT nCtrlID);
...
DECLARE_MESSAGE_MAP()

in your dlg.cpp:
void CDlg::OnBtnClicked(UINT nCtrlID)
{
     
}

that should work. You will need to remove all the ON_BN_CLICKED from the message map.

If you do not want to reorder your btn ids (easily done by editing resource.h) you can store some index using SetWindowLong(hSomeBtn, GWL_USERDATA...)

Best regards,
jemax
0
 
LVL 2

Author Comment

by:elstcb
ID: 7173904
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
0
 
LVL 2

Author Comment

by:elstcb
ID: 7173936
aha, got it!

Thanks again to everyone that helped...

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

Cheers,

Steve
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question