Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here I need to call out the system menu by passing the function "RightMenu" an ITEMIDLIST* .
It's all OK when I test on windows XP.But it's not OK on windows 7 and windows 8 .

On Windows 7 and Windows 8,when the menu was showed ,there's no text in the menu text and however ,the position of the menu is not align with the mouse..

any one can help ?


Yes ,here's the codes:
C++
static IContextMenu2*  context_menu_2 = NULL;
static IContextMenu3*  context_menu_3 = NULL;

void RightMenu(HWND handle,IShellFolder* folder,ITEMIDLIST* pIID)
{
    HWND hwnd      = handle;
    LPCONTEXTMENU pContextMenu = NULL;
    LPCONTEXTMENU pCtxMenuTemp = NULL;
    context_menu_2 = NULL;
    context_menu_3 = NULL;
    SLINT 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;
    }
    else if (pCtxMenuTemp->QueryInterface(IID_IContextMenu2,(void**)&pContextMenu) == NOERROR)
    {
        menuType = 2;
    }
    if(pContextMenu)
    {
        pCtxMenuTemp->Release();
    }
    else
    {
        pContextMenu = pCtxMenuTemp;
        menuType = 1;
    }
    if (menuType == 0)
    {
        return;
    }
    HMENU hMenu = CreatePopupMenu();
    hRslt       = pContextMenu->QueryContextMenu(hMenu,0,1,0x7FFF,CMF_NORMAL | CMF_EXPLORE);
    if (FAILED(hRslt))
    {
        return;
    }
    WNDPROC oldWndProc = NULL;
    if (menuType > 1)
    {
        oldWndProc = (WNDPROC)SetWindowLongPtr(handle,GWL_WNDPROC,(LONG)HookWndProc);
        if (menuType == 2)
        {
            context_menu_2 = (LPCONTEXTMENU2) pContextMenu;
        }
        else
        {
            context_menu_3 = (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();
    context_menu_2 = NULL;
    context_menu_3 = NULL;
    DestroyMenu(hMenu);
}
LRESULT CALLBACK HookWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_MENUCHAR:
        if (context_menu_3)
        {
            LRESULT lResult = 0;
            context_menu_3->HandleMenuMsg2(message, wParam, lParam, &lResult);
            return(lResult);
        }
        break;
    case WM_DRAWITEM:
    case WM_MEASUREITEM:
        if(wParam)
        {
            break;
        }
    case WM_INITMENUPOPUP:
        if (context_menu_2)
        {
            context_menu_2->HandleMenuMsg(message, wParam, lParam);
        }
        else
        {
            context_menu_3->HandleMenuMsg(message, wParam, lParam);
        }
        return(message == WM_INITMENUPOPUP ? 0 : TRUE);
        break;
    default:
        break;
    }
    return ::CallWindowProc((WNDPROC) GetProp(hWnd, TEXT("oldWndProc")), hWnd, message, wParam, lParam);
}
Posted
Comments
Michael Haephrati 7-Mar-13 14:58pm    
Where do you define IContextMenu2 and IContextMenu3?
Darkcharm 11-Mar-13 5:31am    
They are global!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900