Create windows right click context menu for SPECIFIC folders

谁说我不能喝 提交于 2019-11-30 08:46:56

问题


How can I create a context menu that appears for files/folders inside a particular folder.
Say there is a directory "D:\RandomCodes"
How do I create a custom context menu item "Open in MyApp" for any file/folder inside this? This menu item should not appear for any other directory. I know if I add the entry in HKCR/Directory/Shell, it'll work, but then it'll appear for all files and folders everywhere. Please guide me through this.


回答1:


Is possible modifing your code for IShellExtInit:

    STDMETHODIMP CShellExt::Initialize(LPCITEMIDLIST pidl,LPDATAOBJECT pDataObj,HKEY hk)
    {
    // Initialize can be called more than once

    // If Initialize has already been called, release the old
    // IDataObject pointer.
    if (m_pDataObj)
    { 
        m_pDataObj->Release(); 
    }

    // If a data object pointer was passed in, save it and
    // extract the file name. 
    if (pDataObj == NULL)
        return E_INVALIDARG;

        m_pDataObj = pDataObj; 
        pDataObj->AddRef(); 

        STGMEDIUM   medium;
        FORMATETC   fe = {CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
        UINT        uCount;

        HRESULT hr = pDataObj->GetData(&fe, &medium);
        if (FAILED(hr))
            return E_INVALIDARG;

        // save the file name
        if (DragQueryFile((HDROP) medium.hGlobal, 0xFFFFFFFF, NULL, 0)==1) 
        {
            DragQueryFile((HDROP) medium.hGlobal, 0, m_szFile, 
                sizeof(m_szFile));

            if (lstrcmpi(m_szFile, "D:\\RandomCodes") == 0) 
            {
                hr = NOERROR;
            }
            else 
                hr = E_INVALIDARG;
        }
        else
            hr = E_INVALIDARG;

        ReleaseStgMedium(&medium);

        return hr;

}



回答2:


example:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Folder\shell\NetBeans]
"AppliesTo"="System.ItemPathDisplay:\"NetBeansProjects\""
@="Open with NetBeans"

[HKEY_CLASSES_ROOT\Folder\shell\NetBeans\command]
@="\"C:\\Program Files\\NetBeans 7.2.1\\bin\\netbeans64.exe\" --open \"%1\""

more info here:

http://msdn.microsoft.com/en-us/library/cc144171%28VS.85%29.aspx




回答3:


I know this is a pretty old question, but to anyone who comes across this in the future, I found the simplest way was to add a String Value to the key called "AppliesTo" and set its value to "under:{path}"

In my example, I want it to only look in the T Drive, so my String value is "AppliesTo":"under:T:".

In C#, this is easily accomplished with the following:

RegistryKey _key = Registry.ClassesRoot.OpenSubKey("Folder\\Shell", true);
RegistryKey newkey = _key.CreateSubKey("My Menu Item");
newkey.SetValue("AppliesTo", "under:T:");

RegistryKey subNewkey = newkey.CreateSubKey("Command");
subNewkey.SetValue("", "C:\\yourApplication.exe");
subNewkey.Close();

newkey.Close();
_key.Close();


来源:https://stackoverflow.com/questions/10686411/create-windows-right-click-context-menu-for-specific-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!