Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / MFC

Windows Explorer wildcard selection shell extension

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
28 Sep 200213 min read 194.8K   3.2K   47  
A shell extension to allow you to select files based on a wildcard search
#ifndef CTXMENU_H
#define CTXMENU_H

#include <comdef.h>
#include <shlobj.h>

#include <string>
#include <list>

using namespace std;

/**
 * Our context menu Shell Extension.
 *
 * Based on code by Michael Dunn.
 */
class ATL_NO_VTABLE CtxMenu : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CtxMenu, &CLSID_CtxMenu>,
	public IShellExtInit,
	public IContextMenu
{
public:

	/** Constructor. */
	CtxMenu(void);

	/** Destructor. */
	virtual ~CtxMenu(void);

DECLARE_REGISTRY_RESOURCEID(IDR_CTXMENU)
DECLARE_NOT_AGGREGATABLE(CtxMenu)
DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CtxMenu)
	COM_INTERFACE_ENTRY(IShellExtInit)
	COM_INTERFACE_ENTRY(IContextMenu)
END_COM_MAP()

public:

	// IShellExtInit
	STDMETHOD(Initialize)(LPCITEMIDLIST, LPDATAOBJECT, HKEY);

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

	/**
	 * Finds the files and subfolders from the current folder 
	 * that match the specified wildcard pattern, and tells 
	 * Explorer's ListView to select them. 
	 */
	void SelectFiles(const char* pattern);

	/** 
	 * Selects the files and subfolders that were selected at 
	 * the time Wildcard Select was invoked.
	 */
	void RestoreOriginalSelection(void);

protected:

	/** Type for a list of file names. */
	typedef list<string> file_list_t;

	/** Iterator for a list of file names. */
	typedef file_list_t::iterator file_iter_t;

	/**
	 * Invoked when the user clicks the right mouse button 
	 * on the background of an Explorer window. In this case, 
	 * Wildcard Select will try to match all files.
	 */
	HRESULT ClickedOnBackground(LPCITEMIDLIST pidl);

	/**
	 * Invoked when the user clicks the right mouse button 
	 * on a file or subfolder name. In this case, we will 
	 * only try to match the files and subfolders that were 
	 * selected at the time Wildcard Select was invoked.
	 */
	HRESULT ClickedOnFileOrFolder(LPDATAOBJECT dataObj);

	/** Finds the ListView control inside an Explorer window. */
	bool FindListView(HWND explorerWnd);

	/** 
	 * Recursively searches "wnd" and all its child windows
	 * for a window with the specified window class name.
	 * Returns NULL if no such window could be found.
	 */
	HWND FindWindowWithClass(HWND wnd, const char* className);

	/**
	 * Selects the item with the specified filename in 
	 * Explorer's ListView.
	 */
	void SelectListViewItem(const char* filename);

	/**
	 * Returns the index of the specified filename in
	 * Explorer's ListView, or -1 if no such item exists.
	 */
	int GetListViewIndex(const char* filename);

	/** De-selects everthing in Explorer's ListView. */
	void ClearSelection(void);

	/** The folder the user is working in. */
	char folder[MAX_PATH + 1];

	/**
	 * Whether to look at all files and subfolders, or only at 
	 * the ones that were selected at the time Wildcard Select
	 * was invoked. In the latter case, "allFiles" is false and 
	 * "selected" keeps a list of these files.
	 */
	bool allFiles;

	/**
	 * The files and subfolders that were selected at the time 
	 * Wildcard Select was invoked. We restrict our search to 
	 * only these files. This collection is empty if "allFiles" 
	 * is true.
	 */
	file_list_t selected;

	/** Explorer's ListView control. */
	HWND listView;

	/** The icon we display in the context menu. */
	HBITMAP bitmap;
};

#endif // CTXMENU_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
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions