难以从CImageList设置菜单项位图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了难以从CImageList设置菜单项位图相关的知识,希望对你有一定的参考价值。

我有一个菜单,在某种情况下提示。我有一个位图图像,其中包含我想要添加到菜单中的图标。 首先,我加载图像列表如下:

CImageList imageList;
imageList.Create(18, 16, ILC_COLOR24|ILC_MASK, 0, 0)
CBitmap bitmap;
bitmap.LoadBitmap(IDR_CL2_TAB_MENU_OPTIONS);
imageList.Add(&bitmap, RGB(192, 192, 192));

第二,我尝试使用以下内容提取位图以添加到菜单中

IMAGEINFO imgInfo;
ImgList.GetImageInfo( nBmpNo, &imgInfo );
pMenu->SetMenuItemBitmaps(iItem, MF_BYPOSITION, CBitmap::FromHandle( imgInfo.hbmImage ), NULL);

但不幸的是,它没有用,所以我尝试了这里写的代码https://www.codeproject.com/Articles/4673/Extracting-Single-Images-from-a-CImageList-object

但它只给我菜单中的黑色图标。 这是我显示菜单的完整功能:

void CMainFrame::ShowTabOptions(CPoint point)
{
  CMenu Menu, *pMenu = NULL;

  if (!Menu.LoadMenu (IDR_POPUP_TAB_OPTIONS))
    return;

  pMenu = Menu.GetSubMenu(0);
  if (NULL == pMenu)
    return;

  CImageList imageList;
  if(!imageList.Create(18, 16, ILC_COLOR24|ILC_MASK, 0, 0))
    return;

  CBitmap bitmap;
  bitmap.LoadBitmap(IDR_CL2_TAB_MENU_OPTIONS);
  imageList.Add(&bitmap, RGB(192, 192, 192));

  CArray<CBitmap*, CBitmap*> bitmapArray;
  CBitmap b1, b2, b3, b4, b5, b6, b7, b8, b9;
  bitmapArray.Add(&b1);
  bitmapArray.Add(&b2);
  bitmapArray.Add(&b3);
  bitmapArray.Add(&b4);
  bitmapArray.Add(&b5);
  bitmapArray.Add(&b6);
  bitmapArray.Add(&b7);
  bitmapArray.Add(&b8);
  bitmapArray.Add(&b9);

  for (int iItem = 0; iItem < 9; iItem++)
  {
    imageList.Copy( 0, iItem, ILCF_SWAP );

    IMAGEINFO imageInfo;
    imageList.GetImageInfo(0,&imageInfo);
    CDC dc; 
    dc.CreateCompatibleDC (GetWindowDC()); 
    CRect rect (imageInfo.rcImage);

    bitmapArray.GetAt(iItem)->CreateCompatibleBitmap (this->GetWindowDC(), rect.Width (), rect.Height ());

    CBitmap* pOldBmp = dc.SelectObject (bitmapArray.GetAt(iItem));
    imageList.DrawIndirect (&dc, 0, CPoint (0, 0), CSize (rect.Width (), rect.Height ()), CPoint (0, 0)/*, ILD_NORMAL, SRCCOPY, RGB(255, 255, 255)*/);
    dc.SelectObject (pOldBmp);

    pMenu->SetMenuItemBitmaps(iItem, MF_BYPOSITION, bitmapArray.GetAt(iItem), NULL);
  }


  pMenu->TrackPopupMenu (TPM_LEFTALIGN | TPM_VCENTERALIGN | TPM_LEFTBUTTON, point.x, point.y, this, NULL);
  Menu.DestroyMenu ();

  for (int iIndex = 0; iIndex < bitmapArray.GetCount(); iIndex ++)
  {
    bitmapArray.GetAt(iIndex)->DeleteObject();
  }
}

谁能告诉我我错过了什么? 提前致谢

答案

所以,这就是我实际做到的方式:

Step 1

我有一个变量定义我的班级:

MenuBitmapsMap m_mapMenuBitmap;

这是在标题中:

using MenuBitmapsMap = map<UINT, CBitmap>;

Step 2

我班上有这个方法:

void CCreateReportDlg::AddMenuBitmaps()
{
    CMenu*pMenu = GetMenu();

    if (pMenu == nullptr)
        return;

    m_mapMenuBitmap.clear(); 

    // File menu
    UpdateMenuBitmap(pMenu, ID_FILE_SAVE, IDB_BMP_MENU_SAVE);
    UpdateMenuBitmap(pMenu, ID_FILE_SAVEASFILE, IDB_BMP_MENU_SAVE_AS);
    UpdateMenuBitmap(pMenu, ID_FILE_PRINT, IDB_BMP_MENU_PRINT);
    UpdateMenuBitmap(pMenu, ID_FILE_IMPORT_FROM_OCLM_ASSIGNMENT_HISTORY, IDB_BMP_MENU_IMPORT);
    UpdateMenuBitmap(pMenu, ID_FILE_EXPORT, IDB_BMP_MENU_EXPORT);
    UpdateMenuBitmap(pMenu, ID_FILE_EXPORTSETTINGS, IDB_BMP_MENU_SETTINGS);
    UpdateMenuBitmap(pMenu, ID_FILE_PRINT_PREVIEW, IDB_BMP_MENU_PRINT_PREVIEW);
    UpdateMenuBitmap(pMenu, ID_FILE_AVAILABILITY_REPORT, IDB_BMP_MENU_REPORT);

    // Auto menu
    UpdateMenuBitmap(pMenu, ID_AUTOASSIGN_AUTO_ASSIGN, IDB_BMP_MENU_AUTO_ASSIGN);
    UpdateMenuBitmap(pMenu, ID_AUTOASSIGN_SELECTED_COLUMN, IDB_BMP_MENU_COLUMNS);
    UpdateMenuBitmap(pMenu, ID_AUTOASSIGN_SETTINGS, IDB_BMP_MENU_SETTINGS);
    UpdateMenuBitmap(pMenu, ID_AUTOASSIGN_CLEAR_ASSIGNMENTS, IDB_BMP_MENU_CLEAR);
    UpdateMenuBitmap(pMenu, ID_AUTOASSIGN_SELECTED_COLUMN, IDB_BMP_MENU_COLUMNS);
    UpdateMenuBitmap(pMenu, ID_AUTOASSIGN_EXCLUSIONS, IDB_BMP_MENU_EXCLUSIONS);
    UpdateMenuBitmap(pMenu, ID_AUTOASSIGN_SELECTED_COLUMN, IDB_BMP_MENU_COLUMNS);
    UpdateMenuBitmap(pMenu, ID_AUTOASSIGN_SHOW_CONFLICTS, IDB_BMP_MENU_HIGHLIGHT);

    // Help menu
    UpdateMenuBitmap(pMenu, ID_HELP_HELP, IDB_BMP_MENU_HELP);

}

上面的方法在OnInitDialog中调用。

Step 3

这是UpdatemenuBitmap的定义:

void CCreateReportDlg::UpdateMenuBitmap(CMenu *pMenu, UINT uCommandID, UINT uBMPResource,
    bool bByPosition /*false*/, bool bDisabled /*false*/)
{
    if (pMenu == nullptr)
        return;

    // We are working on the "actual" bitmap object held in the map
    CBitmap& bitmap = m_mapMenuBitmap[uCommandID];
    if (bitmap.GetSafeHandle() != nullptr)
        bitmap.DeleteObject();
    if (bitmap.LoadBitmap(uBMPResource) == NULL)
        return;

    theApp.SetBitmapBackgroundAsMenuColour(bitmap);
    if (bDisabled)
        theApp.SetBitmapAsGrayScale(bitmap);

    if (!pMenu->SetMenuItemBitmaps(uCommandID,
        bByPosition ? MF_BYPOSITION : MF_BYCOMMAND,
        &bitmap,
        &bitmap))
    {
        // #UpdateMenuBitmap Failed to set the menu item bitmaps
    }
}

我没有提供其他两种方法,因为它们与我不认为的问题没有直接关系。如果需要,我可以添加答案。

当你查看SetMenuItemBitmaps的文档时,它指出:

当菜单被销毁时,这些位图不会被破坏;由应用程序来销毁它们。

在您的代码中,您将在方法中创建CImageList。将其添加为成员变量,以便在显示菜单时对象保持有效。然后,在OnDestroy中销毁您的图像列表(资源图像可能不需要)。

Update

以下是我在app类中的额外方法:

void CMeetingScheduleAssistantApp::SetBitmapBackgroundAsMenuColour(HBITMAP hbmp)
{
    UpdateBitmapBackground(hbmp, true);
}


void CMeetingScheduleAssistantApp::SetBitmapAsGrayScale(HBITMAP hbmp)
{
    UpdateBitmapBackground(hbmp, false);
}

void CMeetingScheduleAssistantApp::UpdateBitmapBackground(HBITMAP hbmp, bool enabled, COLORREF crBackground /* GetSysColor(COLOR_MENU) */)
{
    if (!hbmp)
        return;
    HDC memdc = CreateCompatibleDC(nullptr);

    BITMAP bm;
    ::GetObject(hbmp, sizeof(bm), &bm);
    int w = bm.bmWidth;
    int h = bm.bmHeight;
    BITMAPINFO bi = { sizeof(BITMAPINFOHEADER), w, h, 1, 32, BI_RGB };

    std::vector<uint32_t> pixels(w * h);
    GetDIBits(memdc, hbmp, 0, h, &pixels[0], &bi, DIB_RGB_COLORS);

    //assume that the color at (0,0) is the background color
    uint32_t old_color = pixels[0];

    //this is the new background color
    uint32_t bk = crBackground;

    //swap RGB with BGR
    uint32_t new_color = RGB(GetBValue(bk), GetGValue(bk), GetRValue(bk));

    //define lambda functions to swap between BGR and RGB
    auto bgr_r = [](uint32_t color) { return GetBValue(color); };
    auto bgr_g = [](uint32_t color) { return GetGValue(color); };
    auto bgr_b = [](uint32_t color) { return GetRValue(color); };

    BYTE new_red = bgr_r(new_color);
    BYTE new_grn = bgr_g(new_color);
    BYTE new_blu = bgr_b(new_color);

    //change background and modify disabled bitmap
    for (auto &p : pixels)
    {
        if (p == old_color)
        {
            p = new_color;
        }
        else if (!enabled)
        {
            //blend color with background, similar to 50% alpha
            BYTE red = (bgr_r(p) + new_red) / 2;
            BYTE grn = (bgr_g(p) + new_grn) / 2;
            BYTE blu = (bgr_b(p) + new_blu) / 2;
            p = RGB(blu, grn, red); //<= BGR/RGB swap
        }
    }

    //fix corner edges
    for (int row = h - 2; row >= 1; row--)
    {
        for (int col = 1; col < w - 1; col++)
        {
            int i = row * w + col;
            if (pixels[i] != new_color)
            {
                //check the color of neighboring pixels:
                //if that pixel has background color,
                //then that pixel is the background 

                bool l = pixels[i - 1] == new_color; //left pixel is background
                bool r = pixels[i + 1] == new_color; //right  ...
                bool t = pixels[i - w] == new_color; //top    ...
                bool b = pixels[i + w] == new_color; //bottom ...

                                                     //we are on a corner pixel if:
                                                     //both left-pixel and top-pixel are background or
                                                     //both left-pixel and bottom-pixel are background or
                                                     //both right-pixel and bottom-pixel are background or
                                                     //both right-pixel and bottom-pixel are background
                if (l && t || l && b || r && t || r && b)
                {
                    //blend corner pixel with background
                    BYTE red = (bgr_r(pixels[i]) + new_red) / 2;
                    BYTE grn = (bgr_g(pixels[i]) + new_grn) / 2;
                    BYTE blu = (bgr_b(pixels[i]) + new_blu) / 2;
                    pixels[i] = RGB(blu, grn, red);//<= BGR/RGB swap
                }
            }
        }
    }

    SetDIBits(memdc, hbmp, 0, h, &pixels[0], &bi, DIB_RGB_COLORS);
    DeleteDC(memdc);
}

以上是关于难以从CImageList设置菜单项位图的主要内容,如果未能解决你的问题,请参考以下文章

MFC-CImagelist图像列表控件

从单个按钮从多个片段中提取数据

具有透明背景的win32菜单项位图

如何正确使用 SetMenuItemBitmaps 替换菜单项上的默认位图?

热门从另一个片段导航到主页片段

如何在 Android 中显示和隐藏菜单项?