locked
Icon for Context Menu RRS feed

  • Question

  • I added some custom context menus to my project using shell extension. Now i want to show a little icon in front of menu entry like winzip or winrar have.

    I wanna add colored icons to the menu items which get pop up when u right click on a file in explorer..

                 File
                 Edit
    ________________
      <Icon> Click Me
    ________________

    I am using vc++ 6.0 for my project.

    Thanks in advance
    Thursday, March 19, 2009 10:27 AM

Answers

  • Use this code
    http://www.codeproject.com/KB/shell/iconextract.aspx

    HRESULT CIconExtractObj::QueryContextMenu(HMENU hmenu, UINT uMenuIndex, UINT uCmdID, UINT uidLastCmd, UINT uFlags)
    {
    //If the flags include CMF_DEFAULTONLY then we shouldn't do anything

    if(uFlags & CMF_DEFAULTONLY)
    return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0);
    int nMenus = 0;
    //The number of icons in the file

    int nIcons = (int)ExtractIconEx(m_szFile, -1, NULL, NULL, 0);
    if(nIcons > 0)
    {
    InsertMenu(hmenu, uMenuIndex, MF_STRING | MF_BYPOSITION, uCmdID++, _T("Extract &Large Icon"));
    if(NULL != m_hExtractBmpL)
    SetMenuItemBitmaps(hmenu, uMenuIndex, MF_BYPOSITION, m_hExtractBmpL, NULL);
    uMenuIndex++;
    InsertMenu(hmenu, uMenuIndex, MF_STRING | MF_BYPOSITION, uCmdID++, _T("Extract &Small Icon"));
    if(NULL != m_hExtractBmpS)
    SetMenuItemBitmaps(hmenu, uMenuIndex, MF_BYPOSITION, m_hExtractBmpS, NULL);
    uMenuIndex++;
    nMenus = 2;
    }
    return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, nMenus);
    }

    HRESULT CIconExtractObj::InvokeCommand(LPCMINVOKECOMMANDINFO pCmdInfo)
    {
    // If lpVerb really points to a string, ignore this function call and bail out

    if(0 != HIWORD( pCmdInfo->lpVerb))
    return E_INVALIDARG;
    //Extract first icon

    HICON hIconLarge, hIconSmall;
    ExtractIconEx(m_szFile, 0, &hIconLarge, &hIconSmall, 1);
    ICONINFO oIconInfo;
    switch(LOWORD(pCmdInfo->lpVerb))
    {
    case 0:
    GetIconInfo(hIconLarge, &oIconInfo);
    break;

    case 1:
    GetIconInfo(hIconSmall, &oIconInfo);
    break;

    default:
    return E_INVALIDARG;
    }
    BOOL bOpen = ::OpenClipboard(NULL);
    if(TRUE==bOpen)
    {
    ::EmptyClipboard();
    ::SetClipboardData(CF_BITMAP, oIconInfo.hbmColor);
    ::CloseClipboard();
    }
    //Destroy the icons

    DestroyIcon(hIconLarge);
    DestroyIcon(hIconSmall);
    return S_OK;
    }

    • Marked as answer by yadaom Thursday, March 19, 2009 1:13 PM
    Thursday, March 19, 2009 1:12 PM