Windows 7 弹出系统菜单不正常(IContextMenu相关),求大神指教

BlackNoodle 2013-01-23 11:58:41
网上搜到的代码改了一下,系统菜单可以弹出,但是,上面没有任何文字 所有菜单项点击执行正常,但就是木有文字。
原来的代码是罗朝辉大神写的,地址如下:http://www.cppblog.com/kesalin/archive/2008/02/23/43124.html


修改后代码如下:

static IContextMenu2* g_pIContext2 = NULL;
static IContextMenu3* g_pIContext3 = NULL;
void RightMenu(HWND handle,IShellFolder* folder,ITEMIDLIST* pIID)
{
HWND hwnd = handle;
LPCONTEXTMENU pContextMenu = NULL;
LPCONTEXTMENU pCtxMenuTemp = NULL;
g_pIContext2 = NULL;
g_pIContext3 = NULL;
int menuType = 0;
HRESULT hRslt = folder->GetUIObjectOf(
hwnd,
1,
(LPCITEMIDLIST*)&(pIID),
IID_IContextMenu,
0,
(void**)&pCtxMenuTemp);
if (FAILED(hRslt))
{
return;
}
POINT pt;
GetCursorPos(&pt);
if (pCtxMenuTemp->QueryInterface(IID_IContextMenu3,(void**)&pContextMenu) == NOERROR)
{
menuType = 3;

std::cout<<std::endl<<"menu type is 3"<<std::endl;
}
else if (pCtxMenuTemp->QueryInterface(IID_IContextMenu2,(void**)&pContextMenu) == NOERROR)
{
menuType = 2;

std::cout<<std::endl<<"menu type is 2"<<std::endl;
}
if(pContextMenu)
{
std::cout<<"new menu got"<<std::endl;
pCtxMenuTemp->Release();
}
else
{
std::cout<<"it's old menu"<<std::endl;
pContextMenu = pCtxMenuTemp;
menuType = 1;
}
if (menuType == 0)
{
std::cout<<"there's no menu"<<std::endl;
return;
}
HMENU hMenu = CreatePopupMenu();
hRslt = pContextMenu->QueryContextMenu(hMenu,0,1,0x7FFF,CMF_EXPLORE);

if (FAILED(hRslt))
{
return;
}
WNDPROC oldWndProc = NULL;
if (menuType > 1)
{
oldWndProc = (WNDPROC)SetWindowLongPtr(handle,GWL_WNDPROC,(LONG)HookWndProc);
SLINT err = GetLastError();
if(err)
std::cout<<"Something wrong when SetWidowLongPtr: "<<err<<std::endl;
if(menuType == 2)
{
g_pIContext2 = (LPCONTEXTMENU2) pContextMenu;
}
else
{
g_pIContext3 = (LPCONTEXTMENU3) pContextMenu;
}
}
else
{
oldWndProc = NULL;
}
int cmd = TrackPopupMenu(
hMenu,
TPM_LEFTALIGN | TPM_BOTTOMALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
pt.x,
pt.y,
0,
hwnd,
0);
if (oldWndProc)
{
SetWindowLongPtr(handle,GWL_WNDPROC,(LONG)oldWndProc);
}
if (cmd != 0)
{
CMINVOKECOMMANDINFO ci = { 0 };
ci.cbSize = sizeof(CMINVOKECOMMANDINFO);
ci.hwnd = hwnd;
ci.lpVerb = (LPCSTR) MAKEINTRESOURCE(cmd - 1);
ci.nShow = SW_SHOWNORMAL;
pContextMenu->InvokeCommand(&ci);
}
pContextMenu->Release();
g_pIContext2 = NULL;
g_pIContext3 = NULL;
DestroyMenu(hMenu);
}
LRESULT CALLBACK HookWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
DRAWITEMSTRUCT *ds = (DRAWITEMSTRUCT*)lParam;
switch (message)
{
case WM_MENUCHAR:
if (g_pIContext3)
{
LRESULT lResult = 0;
g_pIContext3->HandleMenuMsg2(message, wParam, lParam, &lResult);
return(lResult);
}
break;
case WM_DRAWITEM:
case WM_MEASUREITEM:
if(wParam)
{
break;
}
case WM_INITMENUPOPUP:
if (g_pIContext2)
{
g_pIContext2->HandleMenuMsg(message, wParam, lParam);
}
else
{
if(g_pIContext3)
g_pIContext3->HandleMenuMsg(message, wParam, lParam);
}
return(message == WM_INITMENUPOPUP ? 0 : TRUE);
break;
default:
break;
}
return ::CallWindowProc((WNDPROC) GetProp(hWnd, TEXT("oldWndProc")), hWnd, message, wParam, lParam);
}

...全文
228 3 点赞 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
erfbe1 2013-07-15
oldWndProc = (WNDPROC)SetWindowLongPtr(handle,GWL_WNDPROC,(LONG)HookWndProc); 去掉HookWndProc即可,这应该是老代码,兼容Win9X的,XP和之后的Windows都不需要Hook。
  • 打赏
  • 举报
回复
BlackNoodle 2013-01-25
引用 1 楼 sha_jinhao 的回复:
你在rc编辑器里面加入了么?!
这个跟RC没关系吧 ……
  • 打赏
  • 举报
回复
jimette 2013-01-23
你在rc编辑器里面加入了么?!
  • 打赏
  • 举报
回复
相关推荐
发帖
界面
加入

1.5w+

社区成员

VC/MFC 界面
申请成为版主
帖子事件
创建了帖子
2013-01-23 11:58
社区公告
暂无公告