Click here to Skip to main content
15,896,278 members
Articles / Programming Languages / C++

The Complete Idiot's Guide to Writing Shell Extensions - Part VII

Rate me:
Please Sign up or sign in to vote.
5.00/5 (31 votes)
30 May 2006 518.4K   5.1K   128  
A tutorial on using owner-drawn menus in a context menu shell extensions, and on making a context menu extension that responds to a right-click in a directory background.
// BmpCtxMenuExt.h : Declaration of the CBmpCtxMenuExt

#ifndef __BMPCTXMENUEXT_H_
#define __BMPCTXMENUEXT_H_

/////////////////////////////////////////////////////////////////////////////
// CBmpCtxMenuExt

class ATL_NO_VTABLE CBmpCtxMenuExt :
    public CComObjectRootEx<CComSingleThreadModel>,
    public CComCoClass<CBmpCtxMenuExt, &CLSID_BmpCtxMenuExt>,
    public IShellExtInit,
    public IContextMenu3
{
public:
    CBmpCtxMenuExt() { }

    BEGIN_COM_MAP(CBmpCtxMenuExt)
        COM_INTERFACE_ENTRY(IShellExtInit)
        COM_INTERFACE_ENTRY(IContextMenu)
        COM_INTERFACE_ENTRY(IContextMenu2)
        COM_INTERFACE_ENTRY(IContextMenu3)
    END_COM_MAP()

    DECLARE_REGISTRY_RESOURCEID(IDR_BMPCTXMENUEXT)

public:
    // IShellExtInit
    STDMETHODIMP Initialize(LPCITEMIDLIST, LPDATAOBJECT, HKEY);

    // IContextMenu
    STDMETHODIMP QueryContextMenu(HMENU, UINT, UINT, UINT, UINT);
    STDMETHODIMP InvokeCommand(LPCMINVOKECOMMANDINFO);
    STDMETHODIMP GetCommandString(UINT, UINT, UINT*, LPSTR, UINT);

    // IContextMenu2
    STDMETHODIMP HandleMenuMsg(UINT, WPARAM, LPARAM);

    // IContextMenu3
    STDMETHODIMP HandleMenuMsg2(UINT, WPARAM, LPARAM, LRESULT*);

protected:
    TCHAR   m_szFile[MAX_PATH];
    CBitmap m_bmp;
    UINT    m_uOurItemID;

    LONG m_lItemWidth, m_lItemHeight;
    LONG m_lBmpWidth, m_lBmpHeight;

    static const LONG m_lMaxThumbnailSize;
    static const LONG m_l3DBorderWidth;
    static const LONG m_lMenuItemSpacing;
    static const LONG m_lTotalBorderSpace;

    // Helper functions for handling the menu-related messages.
    HRESULT MenuMessageHandler(UINT, WPARAM, LPARAM, LRESULT*);

    HRESULT OnMeasureItem(MEASUREITEMSTRUCT*, LRESULT*);
    HRESULT OnDrawItem(DRAWITEMSTRUCT*, LRESULT*);
};

#endif //__BMPCTXMENUEXT_H_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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
Software Developer (Senior) VMware
United States United States
Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

Mike was a VC MVP from 2005 to 2009.

Comments and Discussions