ATL encapsulation msflexgrid

Source: Internet
Author: User
Introduction

It is bit tricky to use msflexgrid control in ATL projects (I struggled lot with msdn and net) because:

  1. Msflexgrid control is a visual basic control
  2. Msflexgrid control requires runtime control license.

To check the problem you can do the following:

  1. Create ATL Server DLL
  2. Add ATL Composite Control
  3. Right click and insert msflexgrid Control
  4. Add event by Right clicking and expose some events.
  5. Build DLL.
  6. Deploy in a test machine alongMsflex. ocxAnd other support libraries. please note that it will work fine on the development machine (machine which has Visual Studio installed) because when you install Visual Studio it provides design time and runtime license for the current system by default.

See details on:

  • Http://support.microsoft.com/default.aspx? SCID = KB; en-US; q151771
  • Http://support.microsoft.com/default.aspx? SCID = KB; en-US; q151804 & Ln = en-US & SD = gn & Fr = 0 & qry = q151804 & rnk = 1 & src = dhcs_mpp_gn_srch & SPR = CHS
Result

It won't work on Test Machine (the machine which only has OS installed ). if you are using Visual Basic as client it will display page not found kind of HTML page in place of control.

How to Use MSFlexGrid Then?
  1. Create ATL Server DLL.
  2. Add ATL Composite Control.
  3. ImportMsflxgrd. ocxIn control header file.
    #import "C:/WINNT/System32/msflxgrd.ocx" raw_interfaces_only,     raw_native_types, no_namespace, named_guids
  4. Add#pragma warning(disable:4146) Before #import "...msflxgrid.ocx..."Otherwise it will give warning.
  5. Now the problem is: at the time of creating control you need to get the runtime license for msflexgrid control. I had made a function to do so: collapse
     // Create Control void CreateObject() {   CComPtr
        pUnkCont;   ComQIPtr 
        spPerStm;   CComPtr
        pCF;   // Add CLSID of MSFlexGrid for Runtime license   CComBSTR bstrLicKey = "72E67120-5959-11cf-91F6-C2863C385E30";   HRESULT hr = CoGetClassObject(CLSID_MSFlexGrid,    CLSCTX_ALL,    NULL,        IID_IClassFactory2,    reinterpret_cast<void**>(&pCF) );   // Call CreateInstanceLic to create instance of control    // with runtime license   if( !FAILED( hr ) )    hr =pCF->CreateInstanceLic(NULL,NULL,IID_IUnknown,bstrLicKey,      reinterpret_cast<void**>(&pUnk));      spPerStm=pUnk;   spPerStm->InitNew();   wnd.Attach(m_hWnd);   wnd.AttachControl(pUnk, &pUnkCont);   m_VarGrid = pUnk;  // Start event connection  DispEventAdvise(pUnk);  }
  6. Add the following member variables. I 've added it the following member variables as public.
        CAxWindow wnd;    IUnknownPtr pUnk;    IMSFlexGridPtr m_Grid;        
  7. Next step is to catch the events of flxgrid control which can be done by adding the following line in class declaration:
    public IDispEventImpl < ID_VAR_GRID,CAuthorVar,&DIID_DMSFlexGridEvents,    &LIBID_MSFlexGridLib,1,0 >
  8. Add sink entry (I'm feeling lazy and just using click event if you want you can use no of events as per ur requirement)
    BEGIN_SINK_MAP(CFlxCtrl)    //Make sure the Event Handlers have __stdcall calling convention    SINK_ENTRY_EX(ID_GRID,DIID_DMSFlexGridEvents, DISPID_CLICK,       OnClick_grid)END_SINK_MAP()   
  9. InResource. hFile add#define ID_GRID 201Or any ID which you like.
  10. Add event handler:
    VOID __stdcall Click_grid(){    AfxMessageBox("Clicked");}
  11. You can also add a method to initialize gird I. e ..
        void InitGrid()    {        CString Cols[5]={"Name","Phone No","MailID"};        m_VarGrid->put_FixedRows(1);        m_VarGrid->put_FixedCols(0);        m_VarGrid->put_Rows(2);        m_VarGrid->put_Cols(3);        for(int i = 0;i!=3;i++)        {            m_VarGrid->put_TextMatrix(0,i,(_bstr_t)Cols[i]);                m_VarGrid->put_ColWidth(i,1260);        }    } 
  12. InOnInitDialog CallCreateObject() Function andInitGrid().
  13. Add a method to interface IE.
    STDMETHODIMP CAuthorVar::Stop(){AFX_MANAGE_STATE(AfxGetStaticModuleState())    if(pUnk)    {        HRESULT hr= DispEventUnadvise(pUnk);//    }    m_Grid.Release();    return S_OK;}
  14. Test the control in ActiveX control test container and don't forget to call stop method before closing.

I also had noticed another problem which is:

Every time you need to callDispEventUnadvise(pUnk)/DispEventAdvise(pUnk)Method and it won't process any messages from container window or other controls placed there. the easiest solution I found is just wrap up your first control into another composite control and it will make your life easy.

If you are still feeling lazy or has already created control and don't want to rewrite then you can cheat the test machine by adding the following in registry:

Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT/Licenses/72E67120-5959-11cf-91F6-C2863C385E30]@="ibcbbbebqbdbciebmcobmbhifcmciibblgmf"

And be happy but I won't recommend it. So, have a fun with msflexgrid and ATL Composite Control.

Uttam Kumar UNIK!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.