Shell extensions: Customizing context Menus

Source: Internet
Author: User
Tags implement

If you need to get the full path of a selected file, little brother often mechanically copied the path in the shell window, then copied the file name and spelled it after the path. Sometimes the file path is copied for use by the program, which means that all the "\" in the path must be replaced by "\". Very boring operation. Luckily, I'm a programmer, and I can customize some programs to help myself. Here to share.

The function of the program is clear: Add a menu item "Get file path and save to clipboard" in the Shell context menu. Click to save the full path of one or more of the selected files to the Clipboard. The newline "\ r \ n" Interval between multiple file paths. If you need to get the path is the program format ("\" instead of "\"), you can click the menu item in the state where the CTRL key is pressed.

Implementation: Custom Shell menu items, need to implement IContextMenu interface, but also need to implement Ishellextinit interface to complete the initialization of the work.

First, define some macros you need to use:

//菜单ID
#define  ID_COPY_PATH  0

//用于剪贴板格式
#ifdef _UNICODE
#define CF_TEXT_FORMAT        CF_UNICODETEXT
#else
#define CF_TEXT_FORMAT        CF_TEXT
#endif

Defines an array to hold the selected list of files (folders), as defined below:

CAtlArray<CString>   m_arrFilePath;

The Ishellextinit interface is a initialize method that is used here to display the initialization work before the context menu. In the implementation, I save the currently selected list of files (folders) to the M_arrfilepath array, as follows:

HRESULT stdmethodcalltype Initialize (lpcitemidlist pidlfolder, IDataObject *pdtobj, hkey hkeyprogid)
{
M_arrfilepath.removeall ();

File list
if (pdtobj!= NULL)
{
Stgmedium medium = {0};
Formatetc Fe = {cf_hdrop, NULL, Dvaspect_content,-1, Tymed_hglobal};
if (SUCCEEDED (Pdtobj->getdata (&fe, &medium))
{
Hdrop Hdrop = (hdrop):: GlobalLock (Medium.hglobal);
UINT Ucount =::D ragqueryfile (Hdrop, (UINT)-1, NULL, 0);
for (UINT uindex = 0; Uindex < ucount; uindex++)
{
TCHAR Szfilename[max_path] = {0};
::D ragqueryfile (Hdrop, Uindex, szFileName, sizeof (szFileName)/sizeof (TCHAR))-1);
                
szFileName as file (folder) name
M_arrfilepath.add (szFileName);
}
:: GlobalUnlock (Medium.hglobal);
:: ReleaseStgMedium (&medium);
}
}
return S_OK;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.