An ActiveX is class, is a library, is a COM object further more is a concept itself.
ActiveX controls are pieces of code compiled and linked that could live in a dll or by itself in an ocx file.
The ActiveX must be loaded by other applications like a web browser or you can drag and drop it in your application. To test an ActiveX you can use ActiveX control test container.
Creating the ActiveX:
Let’s start building an ActiveX and will discus more at the right time.
First click New in the File Menu and choose MFC ActiveX ControlWizard type. Name it EvolActiveX. Click OK.
Select Help Files to be generated. You don’t want runtime license.
In the 2nd step you will uncheck <
By clicking finish you will be brought to your new workspace. Here we have EvolActiveX classes:
CEvolActiveXApp
is the application class.
|
Here will be created the instances.
CEvolActiveXCtrl
is the control class. Here you will do the most of your implementations. The drawing, the mouse events and even the implementation of the methods will be all implemented here.
CEvolActiveXPropPage
is a property page generated by the wizard.
_DEvolActiveXEvents
– the event interface for EvolActiveX control; every time the state of your ActiveX has changed you can fire an event to the parent. This is the interface were you will add as much events as you wish. Let’s create an event. Right click this interface and then click “Add Event”.
Choose Click event, click custom implementation. We will use the FireClick as the internal name of the event. Let’s fire this event when you double click the left button. Add using ClassWizard WM_LBUTTONDBLCLK. This will add to the code the function :
void CEvolActiveXCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
{
FireClick();
COleControl::OnLButtonDblClk(nFlags, point);
}
You will have to add here the FireClick. When you double click the left button of the mouse, will be called the FireClick() that will fire the event:
// this is added by the wizzard
{FireEvent(DISPID_CLICK,EVENT_PARAM(VTS_NONE));}
_DEvolActiveX
– the dispatch interface for EvolActiveX control; here you will add the methods that this ActiveX will implement. The methods are functions, they had a name, arguments, and a return value. The implementation can be stock or custom. We will create a custom method named SetColor.
This method will return void and will have a argument “short” type named sColor.
Define in the control class header (EvolActiveXCtl.h)
short m_color;
Now let’s override CEvolActiveXCtrl::OnDraw.
void CEvolActiveXCtrl::OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
switch(m_color)
{
case 0:
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
break;
case 1:
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(LTGRAY_BRUSH)));
break;
case 2:
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(GRAY_BRUSH)));
break;
case 3:
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(DKGRAY_BRUSH)));
break;
case 4:
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(BLACK_BRUSH)));
break;
case 5:
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH)));
break;
default:
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
break;
}
pdc->Ellipse(rcBounds);
}
The implementation of the SetColor method is done in CEvolActiveXCtrl::SetColor. Here you will receive a short sColor that you will use to redraw the ellipse in the OnDraw function.
void CEvolActiveXCtrl::SetColor(short sColor)
{
if(sColor >=0 && sColor <=5)
m_color = sColor;
else
m_color = 0;
Invalidate();
}
One last thing. Before compiling this project let’s take a walk to Resource View – String Table. Here we need to modify the IDS_EVOLACTIVEX. The new name is the original title for this ActiveX. When searching the Windows registry you will found this ActiveX by two this: the UUID and this name.
Change the name to EvolActiveXExample1 Control.
Now!!! Build the project.
|
And run-it. You will be ask for an exacutable to play the ActiveX. Let’s use the ActiveX Control Test Container
In the ActiveX Control Test Container click “insert new control” from Edit menu and choose from the list our ActiveX (named “EvolActiveXExample1 Control”). You will be seeing a window like this:
From Control menu, “Invoke methods” will bring you to this window.
By changing the Parameter Value to 2 then clicking Set Value and Invoke, you will notice that the background color of the ActiveX has changed.
When you double click the left button you will be seeing in the bottom of the screen:
EvolActiveXExample1 Control: Click
Notes for advanced readers!!!
Using regedit search “EvolActiveXExample1 Control” in the registry. Find for example the uuid. Now compare the uuid with the ones in EvolActiveX.odl.
For examples I have found this 395B…. This uuid is unique, so yours will be different. One information kept in registry is the path to the ocx. To learn more about creating an application that use custom ActiveX controls read this article: https://codersource.net/2010/01/30/custom-activex-control-in-mfc/
By: Dragos BREZOI.