Use CB skills to solve the dynamic binding event in VC
First, declare that I am not very familiar with VC, I have a colleague who discovers the dynamically created button when using VC, such as click, he created several buttons, but to handle their click It is very troublesome when the event is, it is necessary to build several corresponding functions.
I use CB, the event binding in the CB is very easy, as long as you create a MyClick (Tobject * Sender) function, then Button-> OnClick = MyClick: It is ok, even if it is The button can be processed with this function to use this function to force the Sender to force the Sender to force to TButton types.
Use the above practice to the VC, I think it is ok.
First inherit the CBUTTON class (CMYBUTTON), declare the typedef void (Int ID) in the header file; // This parameter is set according to different situations.
Define a public member variable in the class
PUBLIC:
CMYCLICK M_MYCLICK;
INT m_id; // This is the only sign used to record the button
Then handle the original OnClick events of this class, write this in the function
void onclicked ()
{
IF (m_myclick)
m_myclick (m_id);
}
This is OK.
Create a function in your program (cannot be a member function of the class)
Void __cdecl myclick (int ID)
{
// Do what you want to do
AfxMessageBox ("You click on the button");
}
// Dynamically create, dynamically bind
For (int i = 0; i { CMYBUTTON TBUTTON; ..... TButton.m_id = i; TButton.m_myclick = myclick; ... } // below is CMYBUTTON class TypeDef void (* cmyclick) (INT ID); Class CMYButton: Public CButton { // construction PUBLIC: CMYBUTTON (); // attributes PUBLIC: CMYCLICK M_MYCLICK; INT m_id; // This is the only sign used to record the button // Operations PUBLIC: // Overrides // ClassWizard Generated Virtual Function Overrides // {{AFX_VIRTUAL (CMYBUTTON) //}} AFX_VIRTUAL // Implementation PUBLIC: Virtual ~ cmybutton (); // generated message map functions protected: // {{AFX_MSG (CMYBUTTON) AFX_MSG void onclicked (); //}} AFX_MSG }