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

MSFlexGrid Control on an ATL Composite Control

By , 27 Mar 2003
 

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 along with MSFlex.ocx and 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:

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. Import msflxgrd.ocx in 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:
     // Create Control
     void CreateObject()
     {
       CComPtr<IUNKNOWN> pUnkCont;
       ComQIPtr <IPERSISTSTREAMINIT> spPerStm;
       CComPtr<ICLASSFACTORY2> 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. In Resource.h file add #define ID_GRID 201 or 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. In the OnInitDialog call CreateObject() function and InitGrid().
  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 call DispEventUnadvise(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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Uttam Kumar Unik!
Software Developer (Senior) Barclays Wealth
United Kingdom United Kingdom
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalcomposite ActivexmemberNikkiee3 Dec '08 - 10:43 
Hi Uttam ,
 
can you please. le mw know.
Is is really possible to make the ATL COM component embedding the active x (.ocx) inside it.
If yes can you please provide me the steps to do so.
 
I have an assignment . to make the ATL COM component with embedding the .ocx.
Here is the brief of the requirement.
 
I am having on e.ocx provideed by the vendor say as scanner.ocx.
What is need to do is make one another ATL COM or active x using this .ocx file. This componet which I am creating also support one dialog and should be all the controls( like buttons combo, I mean all the user interface,)
 
How to do that.
we need to make ATL COM or use the active x wizard for that.
can you please let me know the steps do so. It's urgent.
 
Thanks,
GeneralRe: composite ActivexmemberUttam Kumar Unik!3 Dec '08 - 22:51 
Hi Nikkiee,
 
Looks like you want to create a wrapper of existing OCX. In ATL you can also use OCX. You need to use ocx in the same way getting used in MFC with very little difference. Use #import. Let me know if you find prob.
 
Regards;
 
Uttam
 
Regards;
 
Uttam Kumar

GeneralRe: composite ActivexmemberNikkiee4 Dec '08 - 1:56 
Uttam,
Can you please let me know the steps need to be followed.
 
In VC++ 6.0 , what should i need to choose,
1. ATL COM Application Wizard
2. MFC Active X Control\Wizard
 
Basically I am C++ developer and very new and a beginner for the ATL COM or active X.
 
in which method i can create my own dialog ( for user interface).
 
I would really greatful, i you can help me in this. This is urgent.
GeneralRe: composite ActivexmemberNikkiee4 Dec '08 - 2:02 
Uttam,
Can you please let me know the steps need to be followed.
 
In VC++ 6.0 , what should i need to choose,
1. ATL COM Application Wizard
2. MFC Active X Control\Wizard
 
Basically I am C++ developer and very new and a beginner for the ATL COM or active X.
 
in which method i can create my own dialog ( for user interface).
 
I would really greatful, i you can help me in this. This is urgent.
 
Actually, I need to write an .ocx\dll using this vendor provided .ocx.
GeneralRe: composite ActivexmemberUttam Kumar Unik!4 Dec '08 - 2:08 
When do you need it? I might create a a sample but only if I get time. Please very clearly let me know what do want?
 
If I'm not wrong all you need is:
 
ATL Component with UI i.e. Dlg and would host another OCX given by a 3rd Party right?
 
Regards;
 
Uttam Kumar

GeneralRe: composite ActivexmemberNikkiee4 Dec '08 - 2:41 
Yes you are very much right, It would be great if you can help me in this.
can you pLease create a sample as soon as you get time. Actually i need it urgently.
 

Thanks,
GeneralRe: composite ActivexmemberUttam Kumar Unik!4 Dec '08 - 2:04 
In ATL you can create UI including dialog. Wizard Steps:
 
File - New - ATL COM APP Wizard
Check Dll or EXE if you want exe.
Tick MFC if you want to use MFC methods
 
Go to Insert - Select ATL Object - Choose Misc - Dialog ( If you want Dlg) Or you can you composit control from control.
 
Then do the coding.
 
Regards;
 
Uttam Kumar

GeneralRe: composite Activex [modified]memberNikkiee4 Dec '08 - 2:25 
Thanks a alot Uttam, you have been very helpful.
Is there any good document, to follow for event handling of this dialog( user interface) which i am gonna be used.
 
and what I need to do to expose all the functions, which my vendor provided .ocx is having.
To out-side of this ATL COM dll.
 

Now I have added the Dialog as you suggested, now I really need to add the ocx using Project->Add to Project->Components and Controls->Components and Control Gallery.
To add the ocx in dialog .
I'm confused.
 

Sorry if i am bugging you to much Smile | :)
 
modified on Thursday, December 4, 2008 10:07 AM

GeneralCrashes in release modememberMember 172712518 Feb '08 - 20:03 
The given sample crashes in release mode
at the line
wnd.AttachControl(...); Frown | :(
QuestionHow to handle ctrl/shift keysmemberkirrik5 Mar '07 - 17:27 
Can you please let me know how to add the events for key down of shift/ctrl buttons. I am able to select the rows which are in next to next. If i want to select the rows like 1, 4, and 6th row then how to do that?
 
-KirriK

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 28 Mar 2003
Article Copyright 2002 by Uttam Kumar Unik!
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid