Click here to Skip to main content
15,867,330 members
Articles / Desktop Programming / ATL
Article

ATL COM Based Addin / Plugin Framework With Dynamic Toolbars and Menus

Rate me:
Please Sign up or sign in to vote.
4.86/5 (35 votes)
9 Dec 20043 min read 163.9K   3.2K   96   48
An article on ATL COM Based Addin / Plugin Framework With Dynamic Toolbars and Menus, based on VC++ Addin Architecture.

Image 1

Links

Introduction

Building a product which suits to every customers requirement is every Companies dream. But in practice this may not be the case. This is because in most of the cases the requirement keep on changing drastically so that we have to make changes in the code level , recompile it, test it and ship it as another version. This way of development is not efficient in terms of project cost and developmental effort. This is where the Addin / Plugin architecture wins. In this article I have written a ATL COM Addin / Plugin Framework with dynamic toolbars and menu support . This framework is actually based on the VC++ 6.0 Addin Architecture which Microsoft used in every MS Office based applications (not sure of the exact implementation , just a guess work, because I followed the same path of VC++ Addin wizard generated code ). One difference, instead of adding menus , their description and other details in a new line delimited fashion in VC++ Addin , I used XML format for adding the Plugin details (Don't know why Microsoft didn't use XML?, perhaps at that time XML was not standardized , or is there any other specific reason??) along with Component Category for categorizing the plugins. Click here for seeing the XML format which I used in this project. You can add more properties as leaf nodes if you want (I followed the Idea of putting menu properties as nodes. Alternatively you can put the properties in some other fashion too).

Background

This article assumes that you have a basic understanding of COM and MFC. This article is actually a continuation of the excellent article written by Zac Howland Pluggable Components using Component Categories - Part 1 . So the user is advised to go through this article first before coming to my article.

Using the code

The demo project attached contains 3 project workspaces written in VC++ 6.0. The application ProjectFramework is the one which loads all the addins , their menus, toolbars and other details. This application uses one core class called CAddinManager for various addin manipulations. The main header file of CAddinManager class is given below.

class CAddinManager 
{
private:
    BOOL m_bLoadAllAdins;
    CArray<CAddinInfo,CAddinInfo> m_AddinInfoArray;
    CComPtr<IProjectFramework> m_ProjectFrameworkObject;
    HRESULT LoadAllAdins(BSTR strAddinLoadingInfo,
                         IProjectFramework* pProjectFramework);
 
public:
    BOOL InvokeAddinMenuItem(UINT iCommandID);
    BOOL SetAddinVisible(CString strAddinName, BOOL bVisible);
    const CAddinInfo& GetAddinInformation(UINT iCommandID);
    const CAddinCommadInfo& GetAddinCommadInfo(UINT iCommandID);
    CAddinCommadInfo GetAddinCommadInfo(long iAddinIndex,long lIndex);
    BOOL AddAddinCommandInfo(long iAddinIndex, 
                             CAddinCommadInfo AddinCommadInfo);
    BOOL SetAddinCount(long lCount);
    BOOL UnloadAllAddins();
    CAddinInfo GetAddinInfo(long iAddinIndex);
    BOOL SetAddinInfo(long iAddinIndex,CAddinInfo AddinInfo);
    long GetAddinCount();
    void SetLoadAllAddinStatus(BOOL bLoadAllAddins);
    virtual BOOL GetLoadAllAddinStatus();
    CAddinInfo GetAddinIffo(CLSID clsID);

    BOOL LoadAllAddins();
    BOOL SaveAddinDefaultSettings();
    BOOL LoadAddinDefaultSettings();
    
    CAddinManager();
    virtual ~CAddinManager();

};
This class uses a lot of other related classes like CAddinInfo which contains information's about each addin, IProjectFramework which is the interface exposed by the ProjectFramework application and IProjectFrameworkAddin interface which every pluggin should implement. Please see the source code for more details.The important blocks of codes in which the application built up is given below.

In CProjectFrameworkApp declare a variable of CAddinManager statically.

static CAddinManager m_AddinManager;
static CProjectFrameworkView* m_pView;

in

BOOL CProjectFrameworkApp::InitInstance()
{
    ...
    if (!ProcessShellCommand(cmdInfo))
        return FALSE;
    ((CMainFrame*)m_pMainWnd)->LoadAditionalAccelerators();
}

in CMainFrame

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_ADDIN_ADDINSETTINGS, OnAddinAddinsettings)
ON_WM_CLOSE()
ON_WM_MENUSELECT()
ON_WM_INITMENUPOPUP()
//}}AFX_MSG_MAP

ON_COMMAND_RANGE(PF_ADDIN_CMD_MIN_MSG_ID, PF_ADDIN_CMD_MAX_MSG_ID, <BR>                 OnAddinMenuItems)
ON_UPDATE_COMMAND_UI_RANGE(PF_ADDIN_CMD_MIN_MSG_ID, PF_ADDIN_CMD_MAX_MSG_ID, <BR>                 OnUpdateAddinMenuItems)
ON_UPDATE_COMMAND_UI_RANGE(ID_FILE_NEW, ID_APP_ABOUT, OnUpdateMenuItems)

ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, PF_MIN_MSG, PF_MAX_MSG, OnToolTipText)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, PF_MIN_MSG, PF_MAX_MSG, OnToolTipText)

END_MESSAGE_MAP()

and

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;


    //Load all addins
    if(CProjectFrameworkApp::m_AddinManager.LoadAddinDefaultSettings())
    {
        if(CProjectFrameworkApp::m_AddinManager.GetLoadAllAddinStatus())
        {
            CProjectFrameworkApp::m_AddinManager.LoadAllAddins(); 
        }
    }
    LoadAllAddinCommands();

    if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | 
                                         WS_VISIBLE | CBRS_TOP
                    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY 
                     | CBRS_SIZE_DYNAMIC) 
        || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
    {
        TRACE0("Failed to create toolbar\n");
        return -1; // fail to create
    }

    if (!m_wndStatusBar.Create(this) ||
        !m_wndStatusBar.SetIndicators(indicators,
                                      sizeof(indicators)/sizeof(UINT)))
    {
        TRACE0("Failed to create status bar\n");
        return -1; // fail to create
    }

    // TODO: Delete these three lines if you don't want the toolbar to
    // be dockable

    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
    EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndToolBar);

    //Remove File new and Open Commands
    RemoveCommands(ID_FILE_NEW);
    RemoveCommands(ID_FILE_OPEN);
    return 0;
}

Points of Interest

In this version features implemented are.

  1. Dynamic Menus.
  2. Dynamic Toolbars.
  3. Help string and tool tip support.
  4. Invoking of methods in plugins from addin menu and toolbar.
  5. Automation support explained using a dialog box invoked from one of the plugins (Report Addin -> Sales report -> Todays Sales Report Ctrl + B menu ).
  6. Hiding of Menus and toolbar buttons (eg : File ->New and Open ).
  7. Key Board accelerator support for plugin.
  8. Loading / Unloading of addins.
  9. Connection point support for getting notification events from addins.

History

  • Initial Release : Dec 2, 2004
  • Added Connection Point support : Dec 10, 2004

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


Written By
Chief Technology Officer KTS INFOTECH PVT LTD
India India
->9+ Years of Experience in IT Field.
->Basically a C++ Programmer migrating to .NET
->Have Masters degree in Physics and Computer Scince.
-> Doing his Ph.D(Part Time) in Optical Networking)
->Interests: Software product development,Networking, Robotics,Sports Physics, Learning musical instruments, Cricket.

->Resides in kerala ,the gods own country, with his mother and wife.

Home page


Comments and Discussions

 
Generalfails under VC2008 Pin
emmy20098-Jun-09 1:30
emmy20098-Jun-09 1:30 
Generalabout the framework Pin
pan xie22-Nov-07 21:46
pan xie22-Nov-07 21:46 
GeneralRe: about the framework Pin
thomas_tom9929-Nov-07 21:06
thomas_tom9929-Nov-07 21:06 
Generalabout the framework Pin
pan xie22-Nov-07 21:45
pan xie22-Nov-07 21:45 
GeneralUsing a resource from an ATL COM MFC Component Pin
cmacgowan7-Sep-07 9:43
cmacgowan7-Sep-07 9:43 
GeneralRe: Using a resource from an ATL COM MFC Component Pin
thomas_tom9912-Sep-07 0:42
thomas_tom9912-Sep-07 0:42 
QuestionHandling view related msg in Addin Pin
sharathgowda19-Aug-07 21:44
sharathgowda19-Aug-07 21:44 
AnswerRe: Handling view related msg in Addin Pin
thomas_tom9928-Aug-07 20:15
thomas_tom9928-Aug-07 20:15 
QuestionIs this work fine under vs2005? Pin
zhms25-Apr-07 5:33
zhms25-Apr-07 5:33 
AnswerRe: Is this work fine under vs2005? Pin
thomas_tom9929-Apr-07 23:34
thomas_tom9929-Apr-07 23:34 
GeneralNeed to add the project types in VS.net 2005 Pin
karabagamoorthy16-Nov-06 16:46
karabagamoorthy16-Nov-06 16:46 
GeneralRe: Need to add the project types in VS.net 2005 Pin
thomas_tom9917-Nov-06 15:31
thomas_tom9917-Nov-06 15:31 
Generalsupprt for controls in toolbars Pin
bensabat6-Nov-05 22:09
bensabat6-Nov-05 22:09 
GeneralRe: supprt for controls in toolbars Pin
thomas_tom9910-Nov-05 3:30
thomas_tom9910-Nov-05 3:30 
GeneralA problem with the app toolbar Pin
bensabat23-Oct-05 23:08
bensabat23-Oct-05 23:08 
GeneralRe: A problem with the app toolbar Pin
thomas_tom9926-Oct-05 22:09
thomas_tom9926-Oct-05 22:09 
GeneralRe: A problem with the app toolbar Pin
bensabat27-Oct-05 0:32
bensabat27-Oct-05 0:32 
GeneralRe: A problem with the app toolbar Pin
thomas_tom9927-Oct-05 1:54
thomas_tom9927-Oct-05 1:54 
GeneralRe: A problem with the app toolbar Pin
bensabat27-Oct-05 2:15
bensabat27-Oct-05 2:15 
Generala Problem with MDI application Pin
bensabat11-Oct-05 1:27
bensabat11-Oct-05 1:27 
GeneralRe: a Problem with MDI application Pin
thomas_tom9912-Oct-05 19:10
thomas_tom9912-Oct-05 19:10 
GeneralRe: a Problem with MDI application Pin
bensabat13-Oct-05 20:36
bensabat13-Oct-05 20:36 
GeneralRe: a Problem with MDI application Pin
bensabat17-Oct-05 20:31
bensabat17-Oct-05 20:31 
GeneralRe: a Problem with MDI application Pin
thomas_tom9927-Oct-05 2:22
thomas_tom9927-Oct-05 2:22 
GeneralMemory leak! with fix solution. Pin
HarveyLiu19-Aug-05 0:57
HarveyLiu19-Aug-05 0:57 

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

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