Windows资源管理器上下文菜单 [英] windows explorer context menu

查看:136
本文介绍了Windows资源管理器上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序显示文件夹和文件的列表

我希望可以选择在文件夹或文件上显示与用户在Windows资源管理器中看到的相同的上下文菜单-左键单击和右键单击拖放.

只是在寻找应该从哪里开始的一些提示-不建议使用.NET

谢谢

My program displays a list of folders and files

I would like to have the option of showing the same context menus on a folder or file as the user sees in Windows Explorer - for left click & right click drag/drop.

Just looking for some hints on where I should start - prefer no .NET

Thanks

推荐答案

您可以使用Windows Shell创建与文件夹和文件的Windows相同的上下文菜单.

以下代码准备的上下文菜单与为我的文档"文件夹创建的Windows相同.
请从您的对话框类中调用CMyTestDlg::ShowContextMenu().

You can use Windows shell to create context menus same as that of windows for folders and files.

Following code prepares context menu same as that of Windows created for My Documents folder.
Please call CMyTestDlg::ShowContextMenu() from your dialog class.

HRESULT GetSHContextMenu(LPSHELLFOLDER psfFolder, LPCITEMIDLIST localPidl,
                         void** ppCM, int* pcmType)
{
    *ppCM = NULL;
    LPCONTEXTMENU pICv1 = NULL; // plain version
    // try to obtain the lowest possible IContextMenu
    HRESULT hr = psfFolder->GetUIObjectOf(NULL, 1, &localPidl, 
        IID_IContextMenu, NULL, (void**)&pICv1);
    if(pICv1) { // try to obtain a higher level pointer, first 3 then 2
        hr = pICv1->QueryInterface(IID_IContextMenu3, ppCM);
        if(NOERROR == hr) *pcmType = 3;
        else {
            hr = pICv1->QueryInterface(IID_IContextMenu2, ppCM);
            if(NOERROR == hr) *pcmType = 2;
        }

        if(*ppCM) pICv1->Release(); // free initial "v1.0" interface
        else { // no higher version supported
            *pcmType = 1;
            *ppCM = pICv1;
            hr = NOERROR; // never mind the query failures, this''ll do
        }
    }

    return hr;
}

void CMyTestDlg::ShowContextMenu()
{
    #define MIN_SHELL_ID 1
    #define MAX_SHELL_ID 30000

    CMenu menu;
    menu.CreatePopupMenu();
    int cmType; // we don''t need this here
    LPCONTEXTMENU pCM;

    IShellFolder *psfParent = NULL;
    LPITEMIDLIST pidlSystem = NULL;
    LPCITEMIDLIST pidlRelative = NULL;
    STRRET strDispName;
    TCHAR szDisplayName[MAX_PATH];
    HRESULT hr;

    hr = SHGetFolderLocation(NULL, CSIDL_MYDOCUMENTS, NULL, NULL, &pidlSystem);
    hr = SHBindToParent(pidlSystem, IID_IShellFolder, (void **) &psfParent, &pidlRelative);

    // assume that psfFolder and pidl are valid
    HRESULT hr1 = GetSHContextMenu(psfParent, pidlRelative, (void**)&pCM, &cmType);

    // fill the menu with the standard shell items
    hr = pCM->QueryContextMenu(menu, 0, MIN_SHELL_ID, MAX_SHELL_ID, CMF_EXPLORE);
    // show the menu and retrieve the selected command ID
    int cmdID = menu.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN, 0, 0, this);
}




这是MSDN中的示例,与您的应用程序相同:
http://www.microsoft.com/msj/0497/wicked/wicked0497.aspx

MSDN链接以获取有关文件夹内容的信息
http://msdn.microsoft.com/zh-CN /library/windows/desktop/bb776885(v=vs.85).aspx

Shell上下文菜单支持:
http://netez.com/2xExplorer/shellFAQ/bas_context.html#getmenu




Here is an example in MSDN, same as that of your application:
http://www.microsoft.com/msj/0497/wicked/wicked0497.aspx

MSDN link to get Information About the Contents of a Folder
http://msdn.microsoft.com/en-us/library/windows/desktop/bb776885(v=vs.85).aspx

Shell context menu support:
http://netez.com/2xExplorer/shellFAQ/bas_context.html#getmenu


这篇关于Windows资源管理器上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆