?
Solved

create a button control at run time in VC 5

Posted on 1998-02-14
4
Medium Priority
?
262 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
[X]
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
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 800 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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

801 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