Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Message Map Array

Posted on 2002-07-23
7
Medium Priority
?
547 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
Industry Leaders: 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 200 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

926 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