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->AddPage(&m_ResultsPage);
    m_pSheet->Create(this,WS_CHILD,NULL);  /* Creates PropertySheet
                                            * structure in memory
                                            * as a child window
                                            * of our control*/
    m_pSheet->SetWindowPos(NULL,0,0,500,500,NULL);
    m_pSheet->ShowWindow(TRUE);
    

    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();
    pParentSheet->SetActivePage(RESULTS_PAGE);
    CResultsPage* pPage=(CResultsPage*)pParentSheet->GetPage(1);
    pPage->DisplayResult(result);
    

    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
    _DSimpleAdditionAtxEvents
    

    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) {
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    m_pSheet->SetActivePage(INPUT_PAGE);
    CEdit* pEdit1=(CEdit*)m_InputPage.GetDlgItem(IDC_EDIT1);
    char buff[10]; wsprintf(buff,"%d",Num1);
    pEdit1->SetWindowText(buff);
    }
    
    void CSimpleAdditionAtxCtrl::AddAndShow(void) {
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    m_InputPage.OnBnClickedButton1();
    }
    

  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.

Downloads

Download demo project – 49 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read