ExploitationThe Application Wizard of MFC allows us to easily create a project. First, select the MFC ActiveX Control in the project type, and then select how many controls are created in the project. In the following dialog box, enter relevant information for each control. You can use the Edit Name button to specify a Name for the control, and use the Advanced button to specify various additional features for the control, in addition, you can specify a control as a subclass that inherits an existing window class. In this example, we create three controls:
- Common window controlCSam_windowCtrl
- No window controlCSam_nowindowCtrl
- EDIT window control CSam_editwindowCtrl
After the project is created, you can compile and test the control, but the control cannot do anything at this time. We need to add methods and properties for the control. In short, the method is equivalent to the function call in the class, and the attribute is equivalent to the member variable in the class.
In the normal window control, we will display a student information, including name, date of birth, and class name. To be able to set the data, we will add a method for the control. SelectClass Wizard, select the Automation page in the dialog box, press the Add Method button to Add a message, and enter relevant information in the pop-up dialog box. The newly added method is named SetMertial. Three parameters are also specified: LPCTSTR pszName, LPCTSTR pszBirth, and LPCTSTR pszClass. the return value is set to void. After saving, the Class Wizard will generate a function, the function name is the method name you specified. In addition, we also add an attribute for users to set the color of the displayed text. In the "add property" dialog box, Set the property name to TextColor and the received parameter to OLE_COLOR, and Set the implementation method to Get/Set Methods. After saving, Class Wizard will generate the GetXXX/SetXXX functions for you. XXX represents the property name you specified. When the control container changes the property, these two functions are used.
Then weCSam_windowCtrl add three member variables to save these student materials and one for saving colors. Of course, do not forget to initialize them in the constructor. Then modify and add functions related to methods and properties. We can see that after the data is modified, Invalidate () is called to re-paint the window.
// Add member variables
Class CSam_windowCtrl: public COleControl
{
DECLARE_DYNCREATE (CSam_windowCtrl)
// Constructor
Public:
CSam_windowCtrl ();
CString m_szName, m_szBirth, m_szClas