简体   繁体   中英

Shell extension for explorer context menu, icon breaks alignment in classic Windows design

I'got following problem,

when adding an entry for the context menu of the windows explorer and the windows 7 design is set to classic, the icon breaks the alignment of the menu.

This picture show the menu before adding the entry (Please regard the icon of Microsoft Security Essentials):

正常行为

After adding the menu entry it looks like this:

错误行为

You see that there is a space between the icon of Microsoft Security Essentials and the menu caption. The used bitmap is a standard bmp 16 x 16.

Have anyone an idea why this happens? Once again, this only happens with the Win 7 classic design, with other designs it works as expected.

Thanks in advance for your help

EDIT:

This is my initial code for adding the item:

iconHandle = LoadImageW(NULL, iconPath.c_str(), IMAGE_BITMAP, 0, 0,   LR_DEFAULTSIZE | LR_LOADTRANSPARENT | LR_LOADFROMFILE);   

MENUITEMINFOW contextEntryAppSuite = { sizeof(contextEntryAppSuite) };
contextMenuItem.fMask =  MIIM_STRING | MIIM_STATE | MIIM_BITMAP | MIIM_FTYPE | MIIM_ID;
contextMenuItem.dwTypeData = caption;       
contextMenuItem.wID = 0;  
contextMenuItem.fType = MFT_STRING;
contextMenuItem.fState = MFS_ENABLED;
contextMenuItem.hbmpItem = static_cast<HBITMAP>(iconHandle);
if(!InsertMenuItemW(hMenu, indexMenu, TRUE, &contextMenuItem))
{       
    return HRESULT_FROM_WIN32(GetLastError());
}

After your help i changed:

contextMenuItem.hbmpItem = static_cast<HBITMAP>(iconHandle);

to:

contextMenuItem.hbmpItem = IconToBitmap(pathToIcon);

// the function from your posted link

HBITMAP IconToBitmap(std::string sIcon) 
{ 
    RECT rect;
    rect.right = ::GetSystemMetrics(SM_CXMENUCHECK);
    rect.bottom = ::GetSystemMetrics(SM_CYMENUCHECK);

    rect.left = rect.top  = 0;

    HICON hIcon = (HICON)LoadImageA(NULL, sIcon.c_str(), IMAGE_ICON,    rect.right, rect.bottom, LR_DEFAULTCOLOR | LR_LOADFROMFILE);
    if (!hIcon)
        return NULL;

    HWND desktop = ::GetDesktopWindow();
    if (desktop == NULL)
    {
        DestroyIcon(hIcon);
        return NULL;
    }

    HDC screen_dev = ::GetDC(desktop);
    if (screen_dev == NULL)
    {
        DestroyIcon(hIcon);
       return NULL;
    }

    // Create a compatible DC
    HDC dst_hdc = ::CreateCompatibleDC(screen_dev);
    if (dst_hdc == NULL)
    {
        DestroyIcon(hIcon);
        ::ReleaseDC(desktop, screen_dev);
        return NULL;
    }

    // Create a new bitmap of icon size
    HBITMAP bmp = ::CreateCompatibleBitmap(screen_dev, rect.right, rect.bottom);
    if (bmp == NULL)
    {
        DestroyIcon(hIcon);
        ::DeleteDC(dst_hdc);
        ::ReleaseDC(desktop, screen_dev);
        return NULL;
    }

    // Select it into the compatible DC
    HBITMAP old_dst_bmp = (HBITMAP)::SelectObject(dst_hdc, bmp);
    if (old_dst_bmp == NULL)
    {
        DestroyIcon(hIcon);
        return NULL;
    }

    // Fill the background of the compatible DC with the given colour
    ::SetBkColor(dst_hdc, RGB(255, 255, 255));
    ::ExtTextOut(dst_hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);

    // Draw the icon into the compatible DC
    ::DrawIconEx(dst_hdc, 0, 0, hIcon, rect.right, rect.bottom, 0, NULL, DI_NORMAL);

    // Restore settings
    ::SelectObject(dst_hdc, old_dst_bmp);
    ::DeleteDC(dst_hdc);
    ::ReleaseDC(desktop, screen_dev);
    DestroyIcon(hIcon);
    return bmp; 

}

Your original code was correct... Don't bother with that IconToBitmap function since your LoadImageW returns HBITMAP when IMAGE_BITMAP is specified.

Menus with bitmaps on XP or using the Classic theme on later versions of Windows seem to reserve space for checkmarks and you sometimes need to call SetMenuInfo with MNS_CHECKORBMP... Try the following code:

iconHandle = LoadImageW(NULL, iconPath.c_str(), IMAGE_BITMAP, 0, 0,   LR_DEFAULTSIZE | LR_LOADTRANSPARENT | LR_LOADFROMFILE);   

MENUITEMINFOW contextEntryAppSuite = { sizeof(contextEntryAppSuite) };
contextMenuItem.fMask =  MIIM_STRING | MIIM_STATE | MIIM_BITMAP | MIIM_FTYPE | MIIM_ID;
contextMenuItem.dwTypeData = caption;       
contextMenuItem.wID = 0;  
contextMenuItem.fType = MFT_STRING;
contextMenuItem.fState = MFS_ENABLED;
contextMenuItem.hbmpItem = static_cast<HBITMAP>(iconHandle);

MENUINFO menuInfo;
menuInfo.cbSize = sizeof(MENUINFO);
menuInfo.fMask = MIM_STYLE;
menuInfo.dwStyle = MNS_CHECKORBMP;
SetMenuInfo(hMenu, &menuInfo);

if(!InsertMenuItemW(hMenu, indexMenu, TRUE, &contextMenuItem))
{       
    return HRESULT_FROM_WIN32(GetLastError());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM