Solved

Message Map Array

Posted on 2002-07-23
7
529 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now