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;
}