Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Windows menu
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:
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 6-Mar-13 16:52pm
Comments
Michael Haephrati at 7-Mar-13 14:58pm
   
Where do you define IContextMenu2 and IContextMenu3?
Darkcharm at 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)

  Print Answers RSS
0 OriginalGriff 499
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 6 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100