Creating Custom ActiveX Controls in Visual C++
CoderSource.net
Creating Custom ActiveX Controls in Visual C++ - Article by shours
Level: BeginnerType: Article
Rating: 4Page: 2 of 3

Date: 1/21/2006 12:00:00 AM

Environment: Windows, Visual Studio

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.


Attachments

Project Files Evol ActiveX Demo

1 2 3

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us