Using a Custom ActiveX Control in MFC
CoderSource.net
Using a Custom ActiveX Control in MFC - Article by shours
Level: BeginnerType: Article
Rating: 5Page: 3 of 3

Date: 1/14/2006 12:00:00 AM

Environment: Windows, Visual Studio

Next add a member function to EvolDoc: void Load().

The implementation is:

          CRect rect;
          HRESULT hResult;
          IEnumGUIDPtr pEnum;
          CLSID clsid;
          LPOLESTR pszName;
          CString strName;
         
          POSITION pos=0;
          pos=m_lControls.GetHeadPosition();
            while(pos)
            {
                       
                        clsid=m_lControls.GetNext(pos);
                        hResult = OleRegGetUserType( clsid, USERCLASSTYPE_FULL, &pszName );
                        if( SUCCEEDED( hResult ) )
                        {
                                    strName = pszName;
                                    CoTaskMemFree( pszName );
                                    pszName = NULL;
                        }
                        else
                        {
                                    pszName = NULL;
                        }

                        if(strName=="EvolActiveXExample1 Control")
                        {
                                    pItem = new CEvolCntrItem(this);

                                    rect.left = 150; rect.right = 200; rect.top = 150; rect.bottom = 200;
                                    pItem->m_rect = rect;
                                   
                                    pItem->CreateNewItem(clsid);

                                    if(pItem->m_lpObject != NULL)
                                                pItem->InvalidateItem();
                                    break;
                        }
            }

Some of the structures called in the above are not define in the namespace of this project or in the StdAfx.h, so that’s way you will have to mannualy include them. In the StdAfx.h include the following headers:

            #include <comdef.h>
            #include <comcat.h>
            #include <afxtempl.h>

Next we will add the ActiveX position in the document. This must be a member variable of the class CEvolCntrItem:

public:
            CRet m_rect; // position within the document

NOTE: Before running the application make sure you already registered the ActiveX. I have developed for this application a quite simple container that draw a ellipse and fill the exterior of the ellipse with some color. If you will build the application at this point, there will appear a window which contains the ActiveX.

Although you cannot move or resize the ActiveX at this moment. To resize or move we will need to change some features in OnDraw function in the class CEvolView. Comment the implicit code and add the new code pDoc->pItem[0]->DoVerb(OLEIVERB_SHOW, this); so your new OnDraw look like:

            void CEvolView::OnDraw(CDC* pDC)
            {
                        CEvolDoc* pDoc = GetDocument();
                        ASSERT_VALID(pDoc);
                        // TODO: add draw code for native data here
                        // TODO: also draw all OLE items in the document
                       
                        // Draw the selection at an arbitrary position. This code should be
                        // removed once your real drawing code is implemented. This position
                        // corresponds exactly to the rectangle returned by CEvolCntrItem,
                        // to give the effect of in-place editing.
                       
                        // TODO: remove this code when final draw code is complete.
                       
                        /* if (m_pSelection == NULL)
                        {
                                    POSITION pos = pDoc->GetStartPosition();
                                    m_pSelection = (CEvolCntrItem*)pDoc->GetNextClientItem(pos);
                        }
                        if (m_pSelection != NULL)
                                    m_pSelection->Draw(pDC, CRect(10, 10, 210, 210));*/
                        pDoc->pItem->DoVerb(OLEIVERB_SHOW, this);
            }

We have managed the first part. We got to the interesting part: calling a method on the ActiveX interface. The ActiveX that I created have a method called SetColor: void SetColor(short sColor); It is the first method on the interface so that’s why will be called as InvokeMethod0.

Let’s concentrate now on the functionality of the server. You will need to construct now the function that call the 1st method on the ActiveX interface. At the level of CEvolDoc a new function will appear:

              void InvokeMethod0(short sColor);
The function will be implemented like this:

             void CEvolDoc::InvokeMethod0(short sColor)
            {
                        DISPPARAMS dpParams;
                        HRESULT hResult;
                        COleVariant v, m_varResult;
                        EXCEPINFO m_excepInfo;
                        UINT iArgErr;
                       
                        dpParams.rgdispidNamedArgs = NULL;
                        dpParams.cNamedArgs = 0;
                        dpParams.cArgs = 1;
                        dpParams.rgdispidNamedArgs = NULL;
                        dpParams.cNamedArgs = 0;
                       

                        v.vt=VT_I2;
                        v.lVal=(short) sColor;//m_pvarParams;
                        m_varResult.Clear();
                        dpParams.rgvarg=v;
                        CEvolCntrItem* pItemHit =pItem;
                        IDispatchPtr m_pDispatch;
                        hResult = pItemHit->m_lpObject->QueryInterface( IID_IDispatch, (void**)&m_pDispatch );
                        if(FAILED(hResult))
                        {
                                    return;
                        }
                        hResult=m_pDispatch->Invoke(1,IID_NULL,GetUserDefaultLCID(),INVOKE_FUNC,&dpParams,&m_varResult,&m_excepInfo,&iArgErr);            
}

To really set a color to the ellipse I’ve made up a little dialog box with a combo box.

You choose the color then you hit a button and the next part of code will be executed:

            void CEvolSetColorDlg::OnButtonSetColor()
            {
              CMainFrame * parent = (CMainFrame*)this->GetParent();
              CDocument* pDoc = parent->GetActiveDocument();
              short pos = m_combo_set_color_ctrl.GetCurSel();
              ((CEvolDoc*)pDoc)->InvokeMethod0(pos);
             }

By: Dragos BREZOI.


Attachments

Project Files Demo Files

1 2 3

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us