Solved

create a button control at run time in VC 5

Posted on 1998-02-14
4
250 Views
Last Modified: 2006-11-17
Hi, Expert

I've successfully create some button controls at run-time.  However, I don't know how to add the message (ON_BUTTON_CLICK) mapping for these controls at the run-time. Normally, the message mapping is implemented by adding some code at message area like

BEGIN_MESSAGE_MAP(.....)
.....
END_MESSAGE_MAP()

However, since all these buttons are created at run time. It is no way to add in any definition in that area during the run time.

Please, give me some idea!
0
Comment
Question by:wzou
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 200 total points
ID: 1182103
When you create button control in runtime you pass UINT number in last parameter in CButton::Create function. This is ID of the button. Now if you want to add BN_CLICKED message handler to button parent class you need to insert the following macro
ON_BN_CLICKED(ID, OnButtonClicked) between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP where ID is buton ID you pass as last parameter in CButton::Create. Then you must add OnButtonClicked function to button parent class declaration and function definition itself.
0
 

Author Comment

by:wzou
ID: 1182104
Hi, galkin;

Thanks for the reply!
The problem run the program I don't know exactly how many Button Controls I will create. So, I have to assign the ID to the new Button Control at run time.  That's why I can't put the message mapping in the BEGIN_MESSAGE_MAP and END_MESSAGE_MAP at build time.
0
 
LVL 7

Expert Comment

by:galkin
ID: 1182105
In this case define macro ON_CONTROL_EX_RANGE as follows

#define ON_CONTROL_EX_RANGE(wNotifyCode, id, idLast, memberFxn) \
      { WM_COMMAND, (WORD)wNotifyCode, (WORD)id, (WORD)idLast, AfxSig_bw, \
            (AFX_PMSG)(void (AFX_MSG_CALL CCmdTarget::*)(UINT))&memberFxn },

to map range of button IDs to the single handler.
where memberFxn is defined as afx_msg BOOL memberFxn (UINT nID);
where nID is ID of the button sent notification message.
wNotifyCode is BN_CLICKED in your case
0
 

Author Comment

by:wzou
ID: 1182106
Hi galkin,

It seems to be a good idea. I will test it.

Tanks for your help!


Wei Zou
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 how to clear a vector as well as how to detect empty vectors in C++.

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