mjbWorld program - Create ATL component


Creating the Projects

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:

c:\mjbworld\net\cpp\opengl

In the Visual Studio environment, click New from the File menu and then click New Project. Enter the following details :

Name: mjbogl

Location: C:\mjbworld\net\cpp\opengl

click OK

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.

Name: mjbogl

Location: C:\mjbworld\net\cpp\opengl\mjbogl

click OK

ATL Project Wizard opens.
Click Application Settings to see the options available.

As you are creating a control, and a control must be an in-process server, leave the Server type as a DLL.
We are not be using attributes, so make sure the Attributed check box is not checked.
None of the additonal options are selected.

Click Finish

Adding the Client Object

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.

Name Tag:

In the Short name field, type mjboglCtl. The remaining fields are automatically completed.

Options Tag:

Standard control
Minimal control: not checked
Aggregation: Yes
Threading model: Apartment
Interface: Dual
Support: Connection Points - checked // creates ImjboglCtlEvents interface to support for outgoing interface

Interfaces Tag:

in Supported column:

IDataObject
IProvideClassInfo2

in Not Supported column:

IPersistStorage // generates IDataObjectImpl<CmjbolgCtl>
IObjectWithSite
IPropertNotifySink
IQuickActivate // generates IQuickActivateImpl<mjboglCtl>
IServiceProvider
ISpecicyPropertyPages // generates ISpecifyPropertyPagesImpl<mjboglCtl>
IsupportErrorInfo

(we also need to generate public CComObjectRoot,
public CComCoClass<COpenGLObj,&CLSID_COpenGLObj>,
public IDispatchImpl<IOpenGLObj, &IID_IOpenGLObj, &LIBID_OPENGLLib>,
public IObjectSafetyImpl<COpenGLObj, INTERFACESAFE_FOR_UNTRUSTED_CALLER>,
public IConnectionPointContainerImpl<COpenGLObj>,)

Appearance Tag:

Insertable checked // makes the control insertable

Stock Properties Tag:

Text
HWND

 

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 mjboglCtl.
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 shortcut menu.
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& /*bHandled*/);
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*/);


Adding incoming methods

Click Class View tab, right-click the ImjboglCtl interface

click on add method

STDMETHOD(SceneGraphBegin)(void);
STDMETHOD(SceneGraphEnd)(void);
STDMETHOD(mglBegin)(USHORT i);
STDMETHOD(mglEnd)(void);
STDMETHOD(mglColor3f)(FLOAT r, FLOAT g, FLOAT b);
STDMETHOD(mglNormal3f)(FLOAT x, FLOAT y, FLOAT z);
STDMETHOD(mglTexCoord2f)(FLOAT u, FLOAT v);
STDMETHOD(mglVertex3d)(DOUBLE x, DOUBLE y, DOUBLE z);
STDMETHOD(mglPushMatrix)(void);
STDMETHOD(mglPopMatrix)(void);
STDMETHOD(mglTranslated)(DOUBLE x, DOUBLE y, DOUBLE z);
STDMETHOD(mglRotated)(DOUBLE a, DOUBLE x, DOUBLE y, DOUBLE z);
STDMETHOD(mglScaled)(DOUBLE x, DOUBLE y, DOUBLE z);
STDMETHOD(mglEnable)(USHORT i);
STDMETHOD(mglDisable)(USHORT i);
STDMETHOD(mglEnableClientState)(USHORT i);
STDMETHOD(mglDisableClientState)(USHORT i);
STDMETHOD(mglColorMaterial)(USHORT i1,USHORT i2);
STDMETHOD(mglMaterialf)(USHORT i1, USHORT i2, FLOAT f1, FLOAT f2, FLOAT f3, FLOAT f4);
STDMETHOD(mglLightf)(USHORT i1, USHORT i2, FLOAT f1, FLOAT f2, FLOAT f3, FLOAT f4);
STDMETHOD(SceneGraphInit)(FLOAT a, FLOAT n, FLOAT f);


Source Code produced

The classes produced are here:


metadata block
see also:
Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover This explains how to program using the managed extensions to C++.

Commercial Software Shop

Where I can, I have put links to Amazon for commercial software, not directly related to this site, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.

cover Visual C++ .NET for Microsoft Platforms only.

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2017 Martin John Baker - All rights reserved - privacy policy.