Design ActiveX control with VC ++

zhaozj2021-02-08  254

ActiveX is a set of Microsoft's use 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

Compile this project to 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. Add toolbars and sub-windows for controls

VC templates are often useful tools, we design a class ctoolbar to add a toolbar for 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 for the toolbar on the prompt information protected: hwnd m_hwndtoolbar; // Toolbar window handle};

Next, you have to make a corresponding modification for 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 location of each object in the control window

The toolbar and sub-window have been generated in the control, and an important job is to adjust 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, TB_AUTOSIZE, 0L, 0L)

Then, the client area of ​​the control window is calculated as the creation (parameter LPRECTPOS has been given), calling the getWindowRect to get 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 objects within the control

Because there is a child window, the functional operation of the user actually is 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);

Thus, when the mouse moves and stays on a button of the toolbar, the control object calls the ONTOOLBARNEEDTEXT function in the toolbar template class to obtain the corresponding prompt information.

4, control window and internal object revoke

Finally, what is needed, if we create a new toolbar and sub-window in the control, the control window and its internal object should also be revoked. 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.

转载请注明原文地址:https://www.9cbs.com/read-2028.html

New Post(0)