Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Message Map Array

Posted on 2002-07-23
7
532 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 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