Tabbed ActiveX Control

Environment: Visual C++ .NET

This article is about creating a simple tabbed ActiveX control. The function of the control is very simple; it adds two numbers and displays their results in another tab.

We'll create an MFC ActiveX control with the following functions exposed:

void SetFirstNumber(LONG Num1)
void SetSecondNumber(LONG Num2)
void AddAndShow()

You will find the things very simple if you have worked in MFC. The user interface is simply a CPropertySheet containing two CPropertyPages. So, let us start following the steps below.

  1. First of all, create a new MFC ActiveX control Project. Give it the name "SimpleAdditionAtx". Click the control settings tab and select "STATIC" in the combo under the text "Create control based on" and click Finish. The wizard has created the following classes: CSimpleAdditionAtxApp, CSimpleAdditionAtxCtrl, and CSimpleAdditionAtxPropPage.
  2. Next, switch to the resource view and add two dialog resources. One will be used for input and the other will be used to display the output. Add the required controls in them. For the current situation, we need two edit boxes and one button for the input page, as you can see in the screenshot of our control. You can add as many Pages as you want, depending upon your requirements.
  3. Now, create the classes for the two dialogs by right-clicking on the dialogs one by one and selecting "add class". Set proper names for the classes. I chosed CInputPage, CResultsPage, and Select CPropertyPage as the base class for both dialogs. Then click Finish.
  4. Next, create the following members in the "CSimpleAdditionAtxCtrl" class:
  5. CPropertySheet* m_pSheet;
    CInputPage m_InputPage;
    CResultsPage m_ResultsPage;
  6. Now, select the "CSimpleAdditionAtxCtrl" class and select Properties. Select the "Messages" button on the properties sheet and add a method for the WM_CREATE message. The wizard will add the OnCreate method in the class.
  7. Now, we add the following code in the OnCreate method:
  8. m_pSheet=new
    CPropertySheet("hello");               /*This Creates a new
                                            * property Sheet on the
                                            * heap that will hold
                                            * the the
                                            * PropertyPages*/
    m_pSheet->AddPage(&m_InputPage);       /*Adds the PropertyPage
                                            * to the
                                            * PropertySheet*/
    m_pSheet->Create(this,WS_CHILD,NULL);  /* Creates PropertySheet
                                            * structure in memory
                                            * as a child window
                                            * of our control*/

    The code above first will create an Object of the Property Sheet, add pages to it, and then actually create the CPropertySheet structure in memory. After that, it adjusts the position of the CPropertySheet to the top left corner of our control and finally it shows it.

  9. Because we created the CPropertySheet on the heap, we deallocate the memory also, so we also have to handle the WM_DESTROY message by adding a method corresponding to it, just as we did it in Step 5. Add the following code in the OnDestroy method.
  10. if(m_pSheet!=NULL)
      delete m_pSheet;
  11. Now, add an event handler for the button we created on the input page dialog in Step 2. Also, add the value member variables for the two edit boxes. Here, we add the following code in the handler of button:
  12. LONG result=m_FirstNumber+ m_SecondNumber;
    CPropertySheet* pParentSheet=(CPropertySheet*)GetParent();
    CResultsPage* pPage=(CResultsPage*)pParentSheet->GetPage(1);

    m_FirstNumber and m_SecondNumber are the member variables for the two edit controls.

    Display result is a method you need to add in the CResultPage to display results.....

  13. Now, we are ready to expose methods. For this, switch to class view and expand the "SimpleAdditionAtxLib" node. You will see two more nodes:
  14. _DSimpleAdditionAtx and

    The first one is where we'll add our methods; the second is for adding the events. We'll simply add methods by right-clicking on the first node and selecting "add method." Now, we add the following methods, one by one:

    void SetFirstNumber(LONG Num1)
    void SetSecondNumber(LONG Num2)
    void AddAndShow()
  15. Now, we need to provide implementation for these methods. The wizard will have already created a default implementation in the class "CSimpleAdditionAtxCtrl". Our implementation of these methods is simple:
  16. void CSimpleAdditionAtxCtrl::SetFirstNumber(LONG Num1) {
    CEdit* pEdit1=(CEdit*)m_InputPage.GetDlgItem(IDC_EDIT1);
    char buff[10]; wsprintf(buff,"%d",Num1);
    void CSimpleAdditionAtxCtrl::AddAndShow(void) {
  17. Now, our control is ready to be built and then tested in the ActiveX control test container. For this, go to Tools-> ActiveX control test container.


Download demo project - 49 Kb


  • very helpful example

    Posted by Legacy on 02/17/2004 12:00am

    Originally posted by: SaviP

    very helpful example, thanks man!

  • SetActivePage error

    Posted by Legacy on 02/09/2004 12:00am

    Originally posted by: Ruslan Androsyuk

    You have a little mistake - SetActivePage function takes the index of page, not it's ID.

  • Good Job, Well done !!

    Posted by Legacy on 01/29/2004 12:00am

    Originally posted by: Aisha Ikram

    Good job. Very informative for ActiveX beginners.
    Keep up the good work !

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds