Solved

create a button control at run time in VC 5

Posted on 1998-02-14
4
221 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi galkin,

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

Tanks for your help!


Wei Zou
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

772 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

11 Experts available now in Live!

Get 1:1 Help Now