In a single document view (SDI) structure, a view is generally used to display data. However, you sometimes want to display buttons or other controls in the view to meet your needs. Vc6.0 does not directly provide a wizard for adding buttons to the view (maybe yes, I did not find it ). Next, I will provide a function to manually add buttons and make them respond to events.
Step 1: Add a button
First, define a button identifier in ** view. h.
# Define id_button 100
Next, add a member variable to the C ** View class.
Cbutton m_mybutton; // This statement is written in the class definition of ** view. h and must be declared as public.
Then create a button instance
M_mybutton.create (_ T ("example button"), // The caption attribute of the button, and the display of the button
Ws_child | ws_visible | bs_pushbutton | bs_icon, // The button is also a form, which also has a form style. These constants enable buttons to achieve certain effects through or operations. For the meanings of these constants, refer to msdn.
Crect (20,320, 50,340), // This parameter specifies the button position and size in the view.
This, // This Pointer Points to the parent form of the button
Id_button); // This is the constant just defined for the button. The control ID of the button
Then call the m_mybutton.showwindow (sw_hide) function to display the button according to the style of the parameter. At this time, the parameter indicates the hidden button. to display the button, you can use sw_show as the parameter. In this way, when you want to use the button, call the m_mybutton.showwindow (sw_show) function to display the button.
Note: The buttons are initialized in the C ** view: oncreate () function. This function is an initialization function called when the form is created. Objects such as buttons are suitable for initialization at this time.
Int C ** view: oncreate (maid)
{
If (cview: oncreate (lpcreatestruct) =-1)
Return-1;
M_mybutton.create (_ T ("example button"), ws_child | ws_visible | bs_pushbutton | bs_icon,
Crect (20,320, 50,340), this, id_button );
M_mybutton.seticon (afxgetapp ()-> loadicon (icon_save); // load an icon to the button
M_mybutton.showwindow (sw_hide );
Return 0;
}
Step 2: Message Functions
At this time, the button cannot respond to any message. Next we will manually add the button message.
1. Declared function prototype
Declare a protected function in the C ** View class declaration.
// {Afx_msg (csoftview)
Afx_msg void onmybutton ();
//} Afx_msg
The common practice is that of the vc6.0 wizard. The message processing function will be declared at // {afx_msg (csoftview )~~~ //} Afx_msg flag. Of course, in vs. NET 2003, we will not see this flag. Afx_msg indicates that the subsequent function is a message processing function.
2. Function implementation
The following function is implemented. Obviously, this function is also a member function of the C ** View class, so it is implemented in ** view. cpp
Void C ** view: onmybutton ()
{
MessageBox ("You clicked me ");
}
3. If you run the program at this time, the button still does not respond to any events. Because the message generated by the event is sent to the view, we need to pass the message from the view to the button, which is easy to implement. In ** view. cpp, find the following mark:
Begin_message_map (C ** view, cview)
End_message_map ()
Write the following code into the tag: on_bn_clicked (id_button, onmybutton)
In this Code, let the object referred to by id_button respond to the click (on_bn_clicked) event. The event processing function is onmybutton. This is the message ing mechanism of MFC. For this mechanism, we will not talk about it here. It will be available soon.
At this point, a button that can respond to the event is created. If you want to respond to other events, you can create them in this way. You only need to change step 1 in this process and change on_bn_clicked to the Message type you want to respond.