Solved

create a button control at run time in VC 5

Posted on 1998-02-14
4
235 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 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

17 Experts available now in Live!

Get 1:1 Help Now