Design ActiveX control with VC
(Zhou Yongsheng July 05, 2001) 14:03) ActiveX is a set of Microsoft proposed in a set of COM (ComponentObjectModel, component object model) to make software components interact in network environments. It doesn't matter with specific programming languages. As a technique for Internet application development, ActiveX is widely used in various aspects of the web server as well as clients. This article focuses on Visual C 5.0 as a tool, using the AppWizard provided by the system to quickly construct a simple ActiveX control. Usually VC 5.0 provides two ways to develop ActiveX control: MFC and ATL (ActiveXTemplateLibrary). With MFC, you can pay attention to the details of the interface, focusing on the control itself, but the generated control is relatively large. Moreover, if the client wants to run this ActiveX control, you must have the corresponding version of the MFC class library DLL. Otherwise, these huge libraries must be downloaded; use ATL, due to non-MFC standard class libraries, generated control relative Smaller, but developers must understand the details of COM, OLE technology. Here, we mainly illustrate the method of developing ActiveX control using MFC because this method is relatively easy. In the Microsoft Developer Studio environment, you can quickly generate a standard ActiveX control as follows. First, start the system's AppWizard, create a new project with the New menu item under the File menu. Note: We choose the MFC ActiveX ControlWizard item in the list box. You can also use the ATL COM AppWizard item to generate an ActiveX control, which is the final code amount of the control of the control, but since the ActiveX control object added in this method is started from the most basic COM object, it cannot be used in the MFC class library. The power provided by ColeControl. The MFC ActiveX ControlWizard item generates a control object class class with ColeControl as a base class, which inherits features of the OLE control implemented in all ColeControl classes, including window object features and methods, properties, and events. Then, according to the prompt information given by the system, the creation work is gradually completed. Users can customize the features of the control according to the actual application. We assume that the project is called Test. After the creation works, open the Workspace file test.dsw, you can see the system generated Test control framework, the main class and implementation function descriptions are shown below:
Delivery class base class Implementation Function CTestAppColecontrolModule Control Instance Initialization and Undo CTestCtrlColecreenTrol Control Window Creation, Update, and Message Handling CTestPageColePropertyPage Control Property Page Settings and Compiling this item with actual properties values, generate Test.ocx. In the current developer Studio environment, this control has been registered automatically. Tools provided by the system ActiveX Control Test Container can be tested immediately. In the list of available OLE controls, Test Control is the name of the control we generated. If you want to use it in other environments, you must first register with Test.ocx files, and the specific practice is to type commands in the command line mode: "Regsvr32 ... / test.ocx". Since then, we can use it in any environment that supports ActiveX controls. Note: .oCX file should be updated and registered at any time. Of course, there is no substantial use of this generated control. If you test it in any package container, you can only see a rectangular box and an inner ellipse, except for the scaling and location movement. However, the controls we want to develop are like a usual editing environment: users can perform text input and editing in the window, and have tools to support each operation. 1. Adding a toolbar and sub-window VC template for the control, often use a class ctoolbar, add a toolbar to the control class T, display each button and prompt information. Our control class CTestCtrl is derived from this class, so that you can add a designed toolbar for controls. The following is a definition of implementing the toolbar template (in the Toolbar.h file):
templateclass CToolbar {public: CToolbar (); void CreateToolbar (HWND hParent); // create tools void AddButtons (); // Add button individually void DestroyToolbar (); // undo toolbar LRESULT OnToolbarNeedText (WPARAM, LPNMHDR pnmh, BOOL & ); // Tips on the toolbar PROTECTED: HWND M_HWNDTOOLBAR; // Toolbar window handle}; then, to make corresponding modifications to TestCTL.H and TestCTL.cpp. In TestCTL.h, you must first contain the Toolbar.h file and define the control class as:
class CTestCtrl: public COleControl, public CToolbar and then adding the message map macro: BEGIN_TOOLBAR_MAP (CTestCtrl) TOOLBAR_BUTTON (ID_New) TOOLBAR_SEPARATOR () TOOLBAR_BUTTON (ID_CUT) ...... TOOLBAR_SEPARATOR () ...... END_TOOLBAR_MAP () In response to each button message on the customized toolbar. The above macros are defined in the Toolbar.h file. Thus, in the member function of the CTestCtrl class, you can flexibly use the CToolbar classes. Also, we designed an editable sub-window for the control. This is very convenient, and the object directly adds to the CEDIT class directly as a member variable M_TestedIt in the control class definition. To really generate our own control window, you need to overload the Colecontrol :: CreateControlWindow function. When the control window is created, the function is called, and the base class is called in the function, and then call the toolbar and sub-window in the CreateToolbar and CreateTestedit to generate the control. Naturally, we no longer need the schematic function of the system default call, remove the CTestCtrl :: OndraW to the part of the rectangular box and the ellipse. CreateToolbar is implemented in the toolbar template class; CreateTestedit is customized in the control class, the function calculates the window size of the client area and the toolbar of the control window, gets the position of the sub-window, so that the entire area other than the toolbar is filled. Then call the m_testedit.create function, set the parameters to make the sub-window have the desired style of the user. 2. Adjust the position toolbar of each object in the control window and sub-window After generating the control, there is an important job that adjusts the position and size of the toolbar and sub-window when the control position moves or size changes. The ColeControl class provides us with such a function:
ColeControl :: OnSetObjectRects (LPCRect LPRECTPOS, LPCRECT LPRECTCLIP); where parameters give the control new location. Reload this function, first automatically adjust the toolbar according to the function provided by the system, call:
:: SendMessage (m_hwndtoolbar, tbar, 0l, 0L) Then, the client area of the control window is calculated as the creation (the parameter LPRECTPOS has been given), and the getWindowRect gets the window size of the toolbar. To make the sub-window full area other than the toolbar outside the control, the corresponding rectangular position should be calculated and the m_testedit.setwindowPos is called to implement the position adjustment of the sub-window. For the sake of beautiful, you can also leave a little surplus on the four sides of the child window. Finally, call the OnsetObjectRects function of the base class.
3. Implement the control of the object within the control because there is a child window, the functional operation of the user is actually controlled by the sub-window. We deliver the control of the focus to the child window, enabling the user to enter and edit operations, call M_Testedit.Setfocus in ctestctrl :: OnsetFocus. Similarly, when the control object captures the button pressed on the toolbar, call the corresponding function of m_testedit to execute the user's command. The display of the prompt information on the toolbar is also controlled by the control object. Add the following code in the ctestctrl :: overnotify function:
NMHDR * PNMHDR = (nmHDR *) LPARAM; BOOL TMP; if (pnmhdr-> code == TTN_NeedText) Return ONTOOLBARNEEDTEXT (WPARAM, PNMHDR, TMP); This, when the mouse moves and stays on a button of the toolbar, The control object will call the ONTOOLBARNEDTEXT function in the toolbar template class to obtain the corresponding prompt information. 4, the cancellation of the control window and the internal object will finally need to explain that if we created a new toolbar and sub-window in the control, the control window should also be revoked in time. Withdraw the control window, you call DESTROYTOOLBAR and M_TESTEDIT.DESTROYWINDOW to clear the toolbar and sub-windows in the CTESTedit :: overdestroy function. Follow the above four working steps to create an ActiveX control with a toolbar and editing sub-window. The sub-window can be defined by the user yourself, like a commonly or simple or complex function like usual applications. The presence of the toolbar is more convenient to perform various operations. Furthermore, it is possible to add other various objects to other various objects in the control.