MS功能区:从代码中绘制按钮图标

MS Ribbon: draw button icon from code

本文关键字:绘制 按钮 图标 代码 功能区 MS      更新时间:2023-10-16

我尝试创建MS Ribbon按钮图标表单代码。我创建了32 bpp的CImage。

CImage img;
img.Create(size, size, 32, CImage::createAlphaChannel);

然后我用这个图像像hdc位图:

HDC hdc = CImageDC(img);
BitBlt(hdc, 0, 0, cx, cy, hdcMem, sx, sy, SRCCOPY);

最后,我使用UIRibbonImageFromBitmapFactory并将结果设置为属性:

IUIImage* pImg;
CComPtr<IUIImageFromBitmap> pifb;
pifb.CoCreateInstance(CLSID_UIRibbonImageFromBitmapFactory);
pifb->CreateImage(img, UI_OWNERSHIP_TRANSFER, &pImg);
UIInitPropertyFromImage(key, pImg, ppropvarNewValue);

结果所有功能都成功完成,但按钮图标为空!!!

我使用了这些要求http://msdn.microsoft.com/en-us/library/windows/desktop/dd316921(v=vs.85).aspx按钮图标是否需要其他要求?

您必须首先转换为HBITMAP,然后使用框架的工厂来生成IUIImage:

// Load the bitmap from the resource file.
CImage img;
hr = img.Load(pszResource);
if (FAILED(hr)) return hr;
HBITMAP hbm = (HBITMAP) img.Detach();
if (hbm)
{
    // Use the factory implemented by the framework to produce an IUIImage.
    hr = m_pifbFactory->CreateImage(hbm, UI_OWNERSHIP_TRANSFER, ppimg);
    if (FAILED(hr))
    {
        DeleteObject(hbm);
    }
}

然后您可以将图像添加到按钮中,如中所示

// Set the image as the new property value for the button.
hr = UIInitPropertyFromImage(UI_PKEY_LargeImage, pImg, ppropvarNewValue);

祝你好运!