These pages show how to create and use an ActiveX component in a .NET(C# or managed C++) program.
create a new solution
create a directory, I have used the following directory:
In the Visual Studio environment, click New from the File menu and then click New Project. Enter the following details :
create a new project
In Solution Explorer, right-click the mjbogl solution node. On the shortcut
menu, click Add and then click New Project.
The New Project dialog box appears.
select visual C++ project
select ATL Project.
ATL Project Wizard opens.
As you are creating a control, and a control must be an in-process server,
leave the Server type as a DLL.
Adding the client object to the project
Click Class View tab, right-click the mjbogl project.
On the shortcut menu, click Add and then click Add Class.
The Add Class dialog box appears.
From the ATL folder, double-click ATL Control.
In the Short name field, type mjboglCtl. The remaining fields are automatically completed.
in Supported column:
in Not Supported column:
IPersistStorage // generates IDataObjectImpl<CmjbolgCtl>
(we also need to generate public CComObjectRoot,
Insertable checked // makes the control insertable
Stock Properties Tag:
To add the mouseDown, mouseUp and mousedragged methods
In Class View, expand mjbogland mjboglLib to display _ImjboglCtlEvents.
Right-click _ImjboglCtlEvents. On the shortcut menu, point to Add and click Add Method.
Select a Return Type of void.
Type mouseDown in the Method name box.
Under Parameter attributes, check the in box.
Select a Parameter type of LONG.
Type x as the Parameter name.
repeat for y parameter
add mouseUp and mouseDrag in the same way
Build the control
On the Build menu, click Build mjbogl.
Implementing the Connection Point Interfaces
Next you will implement a connection point interface and a connection point container interface for your control. In COM, events are implemented through the mechanism of connection points. To receive events from a COM object, a container establishes an advisory connection to the connection point that the COM object implements. Since a COM object can have multiple connection points, the COM object also implements a connection point container interface. Through this interface, the container can determine which connection points are supported.
The interface that implements a connection point is called IConnectionPoint, and the interface that implements a connection point container is called IConnectionPointContainer.
To help implement IConnectionPoint, you will use the Implement Connection Point Wizard. This wizard generates the IConnectionPoint interface by reading your type library and implementing a function for each event that can be fired.
To use the Implement Connection Point Wizard
In Class View, right-click your control's implementation class, in this case
On the shortcut menu, point to Add and click Add Connection Point.
Select _ImjboglCtlEvents from the Source Interfaces list and double-click it to add it to the Implement connection points column. Click Finish. A proxy class for the connection point will be generated, in this case, CProxy_ImjboglCtlEvents.
If you look at the generated _ImjboglCtlEvents_CP.h file in Solution View, you will see that it has a class called CProxy_ImjboglCtlEvents that derives from IConnectionPointImpl. _ImjboglCtlEvents_CP.h also defines the two methods Fire_ClickIn and Fire_ClickOut, which take the two coordinate parameters. These are the methods you call when you want to fire an event from your control.
The wizard also added CProxy_PolyEvents and IConnectionPointContainerImpl to your control's multiple inheritance list. The wizard also exposed IConnectionPointContainer for you by adding appropriate entries to the COM map.
You are finished implementing the code to support events. Now, add some code to fire the events at the appropriate moment. Remember, you are going to fire a ClickIn or ClickOut event when the user clicks the left mouse button in the control. To find out when the user clicks the button, first add a handler for the WM_LBUTTONDOWN message.
To add a handler for the WM_LBUTTONDOWN message
In Class View, right-click the mjboglCtl class and click Properties on the
In the Properties window, click the Messages icon and then click WM_LBUTTONDOWN from the list on the left.
From the drop-down list that appears, click <Add> OnLButtonDown. The OnLButtonDown handler declaration will be added to mjboglCtl.h, and the handler implementation will be added to mjboglCtl.cpp.
LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL&
LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnLButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnLButtonUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnEraseBkgnd(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);