当我们在文件夹窗口中显示的文件(如c:\windows\explorer.exe)上单击鼠标右键时,将弹出一个关联菜单,包括“打开”、“剪切”、“复制”等以及其他软件加入的一些菜单项。在我自己的程序中,使用了ListView控件,里面存放有文件,比如有c:\windows\explorer.exe,当我右击ListView控件中的c:\windows\explorer.exe,我想弹出与在文件夹窗口中右击c:\windows\explorer.exe时相同的关联菜单,应该如何实现?先谢了!
调试欢乐多
SHGetDesktopFolder(&lpDeskFolder);
if(!lpDeskFolder)
{
MessageBox(LPSTR("获取桌面文件夹失败!"),
LPSTR("ccrun's App"), MB_OK | MB_ICONWARNING);
return;
}
char strFileName[] = "explorer.exe";
char strFilePath[] = "c:\\windows";
int nPathLen = sizeof(strFilePath);
wchar_t wszPath[MAX_PATH];
LPITEMIDLIST ParentPidl;
DWORD dwEaten;
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, strFilePath, nPathLen, wszPath, MAX_PATH);
DWORD dwResult = lpDeskFolder->ParseDisplayName(GetSafeHwnd(),
0, wszPath, &dwEaten, &ParentPidl, 0);
if(dwResult != NOERROR)
{
MessageBox(LPSTR("非法的文件名!"),
LPSTR("ccrun's App"), MB_OK | MB_ICONWARNING);
return;
}
// 得到IShellFolder的信息
LPSHELLFOLDER lpParentFolder;
lpDeskFolder->BindToObject(ParentPidl, 0,
IID_IShellFolder,(void **)&lpParentFolder);
if(!lpParentFolder)
{
MessageBox(LPSTR("非法的文件名!"),
LPSTR("ccrun's App"), MB_OK | MB_ICONWARNING);
lpDeskFolder->Release();
lpParentFolder->Release();
return;
}
LPITEMIDLIST lpItemIDList;
//StringToWideChar(strFileName, wszPath, MAX_PATH);
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, strFileName, sizeof(strFileName), wszPath, MAX_PATH);
lpParentFolder->ParseDisplayName(GetSafeHwnd(),
0, wszPath, &dwEaten, &lpItemIDList, 0);
// 得到文件得快捷菜单
LPCONTEXTMENU lpContextMenu;
lpParentFolder->GetUIObjectOf(GetSafeHwnd(), 1,
(LPCITEMIDLIST *)&lpItemIDList, IID_IContextMenu,
0, (void**)&lpContextMenu);
if(!lpContextMenu)
{
MessageBox("无法取得上下文菜单接口!",
"ccrun's App", MB_OK | MB_ICONWARNING);
lpDeskFolder->Release();
lpParentFolder->Release();
return;
}
// 定义一个CMINVOKECOMMANDINFO结构.
CMINVOKECOMMANDINFO ici;
ZeroMemory(&ici, sizeof(ici));
ici.cbSize = sizeof(CMINVOKECOMMANDINFO);
ici.hwnd = GetSafeHwnd();
// 如果是弹出文件属性的对话框就用这个
/*
ici.lpVerb = "Properties";
lpContextMenu->InvokeCommand(&ici);
*/
// 下面是弹出右键菜单的代码
HMENU hMenu = CreatePopupMenu();
dwResult = CMF_EXPLORE;
lpContextMenu->QueryContextMenu(hMenu, 0, 1, 0x7FFF, dwResult);
// 显示菜单
POINT pt;
GetCursorPos(&pt);
int Cmd = TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON |
TPM_RIGHTBUTTON | TPM_RETURNCMD,
pt.x, pt.y, 0, GetSafeHwnd(), 0);
ici.lpVerb = MAKEINTRESOURCE(Cmd - 1);
ici.lpParameters = "";
ici.lpDirectory = "";
ici.nShow = SW_SHOWNORMAL;
lpContextMenu->InvokeCommand(&ici);
// 弹出右键菜单结束
lpContextMenu->Release();
lpParentFolder->Release();
lpDeskFolder->Release();