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


Comments

  • very helpful example

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

    Originally posted by: SaviP

    very helpful example, thanks man!

    Reply
  • 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.

    Reply
  • 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 !

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

Top White Papers and Webcasts

  • Intelligent N+X Redundancy, Placement Affinities, & Future Proofing in the Virtualized Data Center Virtualization brought about the ability to simplify business continuity management in IT. Workload portability and data replication capabilities mean that physical infrastructure failures no longer need impact application services, and they can rapidly be recovered even in the event of complete site failure. However, Enterprises and Service Providers face new challenges ensuring they have enough compute …

  • Cloud computing isn't new, but it's still a hot topic in technology circles. Moving to the cloud has done great things for many businesses. When it comes to handling a basic business necessity, cloud computing has brought many improvements to overall business continuity. Using the cloud for DR makes a lot of sense for many, but it's not exactly the most simple concept. Read this eBook to find answers to all the key questions IT professionals have about cloud-based disaster recovery, and helpful info around the …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date