Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / ATL

The Complete Idiot's Guide to Writing Namespace Extensions - Part I

Rate me:
Please Sign up or sign in to vote.
4.96/5 (56 votes)
9 Dec 200127 min read 1.6M   9.3K   256  
A detailed tutorial on writing your own Explorer namespace extensions.
// ShellViewImpl.h : Declaration of the CShellViewImpl

#ifndef __SHELLVIEWIMPL_H_
#define __SHELLVIEWIMPL_H_

#include "resource.h"       // main symbols

#include "ShellFolderImpl.h"
#include "PidlMgr.h"

/////////////////////////////////////////////////////////////////////////////
// CShellViewImpl

class ATL_NO_VTABLE CShellViewImpl : 
    public CComObjectRootEx<CComSingleThreadModel>,
    public CComCoClass<CShellViewImpl, &CLSID_ShellViewImpl>,
    public IShellView,
    public IOleCommandTarget,
    public CWindowImpl<CShellViewImpl>
{
public:
    CShellViewImpl() : m_uUIState(SVUIA_DEACTIVATE), m_nSortedColumn(0), 
                       m_bForwardSort(true), m_hwndParent(NULL),
                       m_hMenu(NULL), m_psfContainingFolder(NULL), 
                       m_wndList(this, 1)
    {
    }

    ~CShellViewImpl()
    {
        if ( NULL != m_psfContainingFolder )
            m_psfContainingFolder->Release();
    }

DECLARE_NO_REGISTRY()
DECLARE_PROTECT_FINAL_CONSTRUCT()

DECLARE_WND_CLASS(NULL)

BEGIN_COM_MAP(CShellViewImpl)
    COM_INTERFACE_ENTRY(IShellView)
    COM_INTERFACE_ENTRY(IOleWindow)
    COM_INTERFACE_ENTRY(IOleCommandTarget)
END_COM_MAP()

BEGIN_MSG_MAP(CShellViewImpl)
    MESSAGE_HANDLER(WM_CREATE, OnCreate)
    MESSAGE_HANDLER(WM_SIZE, OnSize)
    MESSAGE_HANDLER(WM_SETFOCUS, OnSetFocus)
    MESSAGE_HANDLER(WM_CONTEXTMENU, OnContextMenu)

    NOTIFY_CODE_HANDLER(LVN_DELETEITEM, OnListDeleteitem)
    NOTIFY_CODE_HANDLER(HDN_ITEMCLICK, OnHeaderItemclick)
    NOTIFY_CODE_HANDLER(NM_SETFOCUS, OnListSetfocus)

    COMMAND_ID_HANDLER(IDC_SYS_PROPERTIES, OnSystemProperties)
    COMMAND_ID_HANDLER(IDC_EXPLORE_DRIVE, OnExploreDrive)
    COMMAND_ID_HANDLER(IDC_ABOUT_SIMPLENS, OnAbout)
    MESSAGE_HANDLER(WM_INITMENUPOPUP, OnInitMenuPopup)
    MESSAGE_HANDLER(WM_MENUSELECT, OnMenuSelect)
END_MSG_MAP()

public:
    // IOleWindow
    STDMETHOD(GetWindow)(HWND* phwnd);

    STDMETHOD(ContextSensitiveHelp)(BOOL)
        { return E_NOTIMPL; }
    
    // IShellView methods
    STDMETHOD(CreateViewWindow)(LPSHELLVIEW, LPCFOLDERSETTINGS, LPSHELLBROWSER, LPRECT, HWND*);
    STDMETHOD(DestroyViewWindow)();
    STDMETHOD(GetCurrentInfo)(LPFOLDERSETTINGS);
    STDMETHOD(Refresh)();
    STDMETHOD(UIActivate)(UINT);

    STDMETHOD(AddPropertySheetPages)(DWORD, LPFNADDPROPSHEETPAGE, LPARAM)
        { return E_NOTIMPL; }
    STDMETHOD(EnableModeless)(BOOL)
        { return E_NOTIMPL; }
    STDMETHOD(GetItemObject)(UINT, REFIID, void**)
        { return E_NOTIMPL; }
    STDMETHOD(SaveViewState)()
        { return E_NOTIMPL; }
    STDMETHOD(SelectItem)(LPCITEMIDLIST, UINT)
        { return E_NOTIMPL; }
    STDMETHOD(TranslateAccelerator)(LPMSG)
        { return E_NOTIMPL; }
    
    // IOleCommandTarget methods
    STDMETHOD(QueryStatus)(const GUID*, ULONG, OLECMD prgCmds[], OLECMDTEXT*);
    STDMETHOD(Exec)(const GUID*, DWORD, DWORD, VARIANTARG*, VARIANTARG*);

    // Message handlers
    LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnSetFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnContextMenu(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnInitMenuPopup(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnMenuSelect(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

    // List control message handlers
    LRESULT OnListDeleteitem(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
    LRESULT OnHeaderItemclick(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
    LRESULT OnListSetfocus(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);

    // Command handlers
    LRESULT OnSystemProperties(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnExploreDrive(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
    LRESULT OnAbout(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
        
    // Other stuff
    HRESULT _init ( CShellFolderImpl* pContainingFolder )
    {
        m_psfContainingFolder = pContainingFolder;

        if ( NULL != m_psfContainingFolder )
            m_psfContainingFolder->AddRef();

        return S_OK;
    }

    static UINT sm_uListID;

private:
    CPidlMgr     m_PidlMgr;
    UINT         m_uUIState;
    int          m_nSortedColumn;
    bool         m_bForwardSort;
    FOLDERSETTINGS m_FolderSettings;

    HWND         m_hwndParent;
    HMENU        m_hMenu;
    CContainedWindowT<ATLControls::CListViewCtrl> m_wndList;

    CShellFolderImpl*      m_psfContainingFolder;
    CComPtr<IShellBrowser> m_spShellBrowser;

    void FillList();
    static int CALLBACK CompareItems ( LPARAM l1, LPARAM l2, LPARAM lData );
    void HandleActivate(UINT uState);
    void HandleDeactivate();
};

#endif //__SHELLVIEWIMPL_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