| Tenouk C & C++ | MFC Home | Active Template Library 5 | ATL and ActiveX Controls 2 | Download | Site Index |


 

 

 

 

 

 

 

ATL and ActiveX Controls 1

 

 

 

 

 

 

 

Program examples compiled using Visual C++ 6.0 compiler on Windows XP Pro machine with Service Pack 2. Topics and sub topics for this tutorial are listed below. Don’t forget to read Tenouk’s small disclaimer. The supplementary note for this tutorial is control class.

  1. ActiveX Controls

  2. Using ATL to Write a Control

  3. The Myatldicesvr Program From Scratch

 

If you've finished reading about COM and ATL and still wonder how COM fits into your day-to-day programming activities, you're not alone. Figuring out how to use COM in real life isn't always obvious at first glance. After all, a whole lot of extra code must be typed in just to get a COM object up and running. However, there's a very real application of COM right under your nose, ActiveX Controls. ActiveX controls are small gadgets (usually UI-oriented) written around the Component Object Model.

In Module 28, you examined COM classes created by using ATL. In this module, you'll learn how to write a certain kind of COM class, an ActiveX control. You had a chance to work with ActiveX Controls from the client side in Module 18. Now it's time to write your own. There are several steps involved in creating an ActiveX control using ATL, including:

  1. Deciding what to draw.

  2. Developing incoming interfaces for the control.

  3. Developing outgoing interfaces (events) for the control.

  4. Implementing a persistence mechanism for the control.

  5. Providing a user interface for manipulating the control's properties.

 

This module covers all these steps. Soon, you'll be able to use ATL to create ActiveX controls that you (or other developers) can use within other programs. The next two modules will present ActiveX control program examples compiled using Visual C++ .Net.

 

ActiveX Controls

 

Even today, there's some confusion as to what really constitutes an ActiveX control. In 1994, Microsoft tacked some new interfaces onto its Object Linking and Embedding protocol, packaged them within DLLs, and called them OLE Controls. Originally, OLE Controls implemented nearly the entire OLE Document embedding protocol. In addition, OLE Controls supported the following:

  1. Dynamic invocation (Automation).

  2. Property pages (so the user could modify the control's properties).

  3. Outbound callback interfaces (event sets).

  4. Connections (a standard way to for clients and controls to hook up the event callbacks).

 

When the Internet became a predominant factor in Microsoft's marketing plans, Microsoft announced its intention to plant ActiveX Controls on Web pages. At that point, the size of these components became an issue. Microsoft took its OLE Control specification, changed the name from OLE Controls to ActiveX Controls, and stated that all the features listed above were optional. This means that under the new ActiveX Control definition, a control's only requirement is that it be based on COM and that it implements IUnknown. Of course, for a control to be useful it really needs to implement most of the features listed above. So in the end, ActiveX Controls and OLE Controls refer to more or less the same animal.

 

Developers have been able to use MFC to create ActiveX controls since mid-1994. However, one of the downsides of using MFC to create ActiveX controls is that the controls become bound to MFC. Sometimes you want your controls to be smaller or to work even if the end user doesn't have the MFC DLLs on his or her system. In addition, using MFC to create ActiveX controls forces you into making certain design decisions. For example, if you decide to use MFC to write an ActiveX control, you more or less lock yourself out of using dual interfaces (unless you feel like writing a lot of extra code). Using MFC to create ActiveX controls also means the control and its property pages need to use IDispatch to communicate among themselves.

To avoid the problems described so far, developers can now use ATL to create ActiveX controls. ATL now includes the facilities to create full-fledged ActiveX controls, complete with every feature an ActiveX control should have. These features include incoming interfaces, persistent properties, property pages, and connection points. If you've ever written an ActiveX control using MFC, you'll see how much more flexible using ATL can be.

 

Using ATL to Write a Control

 

Although creating an ActiveX control using ATL is actually a pretty straightforward process, using ATL ends up being a bit more burdensome than using MFC. That's because ATL doesn't include all of MFC's amenities. For example, ATL doesn't include device context wrappers. When you draw on a device context, you need to use the raw device context handle. In addition, ClassWizard doesn't understand ATL-based source code, so when you want your control to handle messages, you end up using the "TypingWizard". That is, you end up typing the message maps in by hand. Despite these issues, creating an ActiveX control using ATL is a whole lot easier than creating one from scratch. Also, using ATL gives you a certain amount of flexibility you don't get when you use MFC. For example, while adding dual interfaces to your control is a tedious process with MFC, you get them for free when you use ATL. The ATL COM Object Wizard also makes adding more COM classes (even non-control classes) to your project very easy, while adding new controls to an MFC-based DLL is a bit more difficult.

For this module's example, we'll represent a small pair of dice as an ATL-based ActiveX control. The dice control will illustrate the most important facets of ActiveX Controls, including control rendering, incoming interfaces, properties, property pages and events.

 

The Myatldicesvr Program From Scratch

 

Before we dig into the story, let build an ATL program from scratch. As usual, launch AppWizard by clicking File New menu of the Visual C++. Then, just follow the shown steps.

 

Figure 1: Myatldicesvr - Visual C++ new ATL COM AppWizard project dialog.

 

Figure 1: Myatldicesvr - Visual C++ new ATL COM AppWizard project dialog.

 

Figure 2: ATL COM AppWizard step 1 of 1.

 

Figure 2: ATL COM AppWizard step 1 of 1.

 

Figure 3: Myatldicesvr project summary.

 

Figure 3: Myatldicesvr project summary.

 

Add new ATL object.

 

Figure 4: Adding new ATL object.

 

Figure 4: Adding new ATL object.

 

Select Controls in Category list and Full Control in Objects list. Then click the Next button.

 

Figure 5: ATL object wizard, adding Full Control object.

 

Figure 5: ATL object wizard, adding Full Control object.

 

Type in myatldiceob as object name, others will be automatically provided. You can change as needed.

 

Figure 6: Myatldicesvr’s object name.

 

Figure 6: Myatldicesvr’s object name.

 

Tick the Support Connection Points for Attributes.

 

Figure 7: Adding the Support Connection Point attribute to ATL object.

 

Figure 7: Adding the Support Connection Point attribute to ATL object.

 

Just accept the default for Miscellaneous.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-----------------------------------------------------------------------------------

Figure 8: Accepting default miscellaneous options.

 

Figure 8: Accepting default miscellaneous options.

 

We select the Background Color for the Stock Properties.

 

Figure 9: Adding background of the Stock Properties.

 

Figure 9: Adding background of the Stock Properties.

 

Start creating bitmaps for white, blue and red colors. Use the Copy and Paste menu under the Edit to speed up your work :-).

 

Figure 10: Adding white dice bitmaps.

 

Figure 10: Adding white dice bitmaps.

 

Figure 11: Adding blue dice bitmaps.

 

Figure 11: Adding blue dice bitmaps.

 

Figure 12: Adding red dice bitmaps.

 

Figure 12: Adding red dice bitmaps.

 

 

Now we can start the coding part. Put codes in myatldiceob.h. Firstly add the MAX_DIEFACES constant.

 

#define MAX_DIEFACES 6

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 1.

 

Using ClassView, add LoadBitmaps() function to Cmyatldiceob class as shown below.

 

BOOL LoadBitmaps();

 

Figure 13: Adding functions and variables to Cmyatldiceob class.

 

Figure 13: Adding functions and variables to Cmyatldiceob class.

 

Figure 14: Adding LoadBitmap() function.

 

Figure 14: Adding LoadBitmap() function.

 

Add the HBITMAP m_dieBitmaps[MAX_DIEFACES] array variable.

 

Figure 15: Adding an array variable.

 

Figure 15: Adding an array variable.

 

Using ClassView, add the following member functions.

 

void ShowFirstDieFace(ATL_DRAWINFO& di);

void ShowSecondDieFace(ATL_DRAWINFO& di);

 

Figure 16: Adding ShowFirstDieFace() member function.

 

Figure 16: Adding ShowFirstDieFace() member function.

 

Figure 17: Adding ShowSecondDieFace() function to Cmyatldiceob class.

 

Figure 17: Adding ShowSecondDieFace() function to Cmyatldiceob class.

 

Using ClassView, add the following member variables.

short m_nDiceColor;

short m_nTimesToRoll;

short m_nTimesRolled;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 18: Adding member variables to Cmyatldiceob class.

 

Figure 18: Adding member variables to Cmyatldiceob class.

 

Add the following member variables.

 

unsigned short m_nFirstDieValue;

unsigned short m_nSecondDieValue;

 

Figure 19: Adding member variable, m_nFirstDieValue to Cmyatldiceob class.

 

Figure 19: Adding member variable, m_nFirstDieValue to Cmyatldiceob class.

 

Figure 20: Adding member variable, m_nSecondDieValue to Cmyatldiceob class.

 

Figure 20: Adding member variable, m_nSecondDieValue to Cmyatldiceob class.

 

The following are the previously added member functions and variables. Take note that, the codes have been relocated just after the END_MSG_MAP().

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 2.

 

Add the Cmyatldiceob() codes. These codes are variables initialization.

 

Cmyatldiceob()

{

    m_bWindowOnly = TRUE;

    LoadBitmaps();

    srand((unsigned)time(NULL));

    m_nFirstDieValue = (rand() % (MAX_DIEFACES)) + 1;

    m_nSecondDieValue = (rand() % (MAX_DIEFACES)) + 1;

    m_nTimesToRoll = 15;

    m_nTimesRolled = 0;

    m_nDiceColor = 0;

}

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 3.

 

Add the #include directive for time related function.

 

#include "time.h"

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 4.

 

Edit the OnDraw(). Take note that, here, you will find that the location of the OnDraw() and a few other functions declared and defined in myatldiceob.h (default if using ClassView) while in the story part of this Tutorial, the functions declared in myatldiceob.h and defined in myatldiceob.cpp.

 

HRESULT OnDraw(ATL_DRAWINFO& di)

{

      RECT& rc = *(RECT*)di.prcBounds;

      ShowFirstDieFace(di);

      ShowSecondDieFace(di);

      return S_OK;

}

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 5.

 

Add WM_TIMER (OnTimer()) Windows message handler using ClassView to Cmyatldiceob class.

 

Figure 21: Adding WM_TIMER Windows message handler to Cmyatldiceob class.

 

Figure 21: Adding WM_TIMER Windows message handler to Cmyatldiceob class.

 

Figure 22: Adding WM_TIMECHANGE Windows message handler.

 

Figure 22: Adding WM_TIMECHANGE Windows message handler.

 

Add the OnTimer() code.

 

LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)

{

      // TODO : Add Code for message handler. Call DefWindowProc if necessary.

      if(m_nTimesRolled > 15)

      {

            m_nTimesRolled = 0;

            KillTimer(1);

      } else {

            m_nFirstDieValue = (rand() % (MAX_DIEFACES)) + 1;

            m_nSecondDieValue = (rand() % (MAX_DIEFACES)) + 1;

            FireViewChange();

            m_nTimesRolled++;

      }

      bHandled = TRUE;

      return 0;

}

 

MFC C++ code snippet - ATL and ActiveX Controls

 

Listing 6.

 

 

 

 

 

 

Further reading and digging:

  1. DCOM at MSDN.

  2. COM+ at MSDN.

  3. COM at MSDN.

  4. Win32 process, thread and synchronization story can be found starting from Module R.

  5. MSDN MFC 7.0 class library online documentation.

  6. MSDN MFC 9.0 class library online documentation - latest version.

  7. MSDN Library

  8. Windows data type.

  9. Win32 programming Tutorial.

  10. The best of C/C++, MFC, Windows and other related books.

  11. Unicode and Multibyte character set: Story and program examples.

 

 

 

 


 

| Tenouk C & C++ | MFC Home | Active Template Library 5 | ATL and ActiveX Controls 2 | Download | Site Index |