Click here to Skip to main content
Click here to Skip to main content

Tabbed ActiveX control

, 25 Jan 2004
Rate this:
Please Sign up or sign in to vote.
A simple MFC ActiveX control with tabs.

Sample Image - tabbedatx.gif

Introduction

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 for displaying the output. Add 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 chose CInputPage and CResulsPage. And select CPropertyPage as the base class for both dialogs, then click Finish.
  4. Next, create the following members in CSimpleAdditionAtxCtrl class.
    • CPropertySheet* m_pSheet;
    • CInputPage m_InputPage;
    • CResultsPage m_ResultsPage;
  5. Now, select the class CSimpleAdditionAtxCtrl and select properties.. select the "Messages" button in the properties and add a method for WM_CREATE message. The wizard will add the OnCreate method in the class.
  6. Now, we add the following code in the OnCreate method:
    /*This Creates a new property Sheet on the 
    heap which will hold the the PropertyPages*/  
    m_pSheet=new CPropertySheet("hello");
    /*Adds the PropertyPage to the PropertySheet*/
    m_pSheet->AddPage(&m_InputPage);
    m_pSheet->AddPage(&m_ResultsPage);
    /* Creates PropertySheet structure in memory
     as a child window of our control*/
    m_pSheet->Create(this,WS_CHILD,NULL);
    m_pSheet->SetWindowPos(NULL,0,0,500,500,NULL); 
    m_pSheet->ShowWindow(TRUE);

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

  7. Since we have created the CPropertySheet on heap, we've to deallocate the memory also. So, we also have to handle the WM_DESTROY message by adding a method corresponding to it, just like we did it in Step 5. Add the following code in the OnDestroy method:
    if(m_pSheet!=NULL) 
      delete m_pSheet;
  8. Now, add event handler for the button we created on the input page dialog in step 2. And also add value member variables for the two edit boxes. And we add the following code in the handler of the button:
    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. DisplayResult is a method you need to add in the CResultPage to display results.....

  9. Now, we are ready to expose methods. For this, switch to class view and expand the node SimpleAdditionAtxLib. Now, you will see two more nodes:
    • _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()
  10. Now, we need to provide implementation for these methods. The wizard will have already created default implementation in the class CSimpleAdditionAtxCtrl. Our implementation of these methods is simple.
    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(); 
    }
  11. Now, our control is ready for build. Then test in the ActiveX control test container. For this, go to Tools-> ActiveX control test container.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Muhammad Ahmed
Software Developer (Senior) ITRS Group plc, London, UK
Pakistan Pakistan
I have done masters in networks and e-business centred computing from The University of Reading, UK, Carlos III de Madrid, Spain and Aristotle University of Thessaloniki. Presently working in a London based software company as a senior software engineer. I like programming in C++, Java and C#, besides software development I spend my spare time in photography and music.
 
find me on facebook here
http://www.facebook.com/?ref=logo#!/profile.php?id=764610012
Follow on   Twitter

Comments and Discussions

 
GeneralWorking only with ActiveX Control Test Container and not with your own clients PinmemberHabeeballah Hasnoddin6-Feb-10 6:04 
GeneralCreating an ActiveX control that has a menu PinmemberVemduc11-Sep-06 8:41 
Generalexcellent Pinmemberliuxine10-Apr-06 20:12 
GeneralAhmed...please help me with my problem Pinmemberashvarma198228-Mar-05 0:55 
GeneralNeed help PinmemberIrishaS24-Mar-05 23:14 
GeneralRe: Need help PinmemberSubash.k11-Jun-08 5:56 
GeneralGood work Pinmemberpc_dev28-Dec-04 18:24 
GeneralPossible with Visual C++! PinmemberAngshuman29-Jan-04 18:06 
GeneralRe: Possible with Visual C++! PinmemberMuhammad Ahmed29-Jan-04 19:05 
GeneralWell Done! PinmemberAisha Ikram28-Jan-04 20:31 
GeneralRe: Well Done! PinmemberMuhammad Ahmed28-Jan-04 20:43 
GeneralRe: Well Done! PinmemberAisha Ikram28-Jan-04 23:55 
GeneralGood but... PinmemberSnyp26-Jan-04 15:04 
GeneralRe: Good but... PinmemberMuhammad Ahmed26-Jan-04 22:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 26 Jan 2004
Article Copyright 2004 by Muhammad Ahmed
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid