0

I tried adding a Bitmap into CMFCToolBar, it is successfully inserted into the toolbar as shown in below figure. But it is inserted with Black Background. I tried inserting it with different bitmaps, the problem is same. When I add the same Bitmap to a CButton(see image below), CTreeCtrl & many other places, it is perfectly displayed without any black background.

I think there is an issue with CMFCToolBar, I could not figure out the issue(see image 1). If I add the bitmap using CImageList into CMFCToolBar it works( see image 2). I could not understand why. Please see below for the code.

MFCToolBar and CButton with a Bitmap

Function to convert SVG to Hbitmap using NanoSVG

HBITMAP SvgToHbitmap(const char* filename) {    
NSVGimage* image = nsvgParseFromFile(filename, "px", 96);

int OriginalWidth = (int)image->width;
int OriginalHeight = (int)image->height;

int TargetWidth = 16;
int TargetHeight = 16;

BITMAPINFO bmpinfo = { 0 };
bmpinfo.bmiHeader.biSize = sizeof(bmpinfo.bmiHeader);
bmpinfo.bmiHeader.biWidth = static_cast<LONG>(TargetWidth);
bmpinfo.bmiHeader.biHeight = -static_cast<LONG>(TargetHeight);
bmpinfo.bmiHeader.biPlanes = 1;
bmpinfo.bmiHeader.biBitCount = 32;
bmpinfo.bmiHeader.biCompression = BI_RGB;


void* bits = nullptr;   
HBITMAP hbitmap = CreateDIBSection(NULL, &bmpinfo, DIB_RGB_COLORS, &bits, nullptr, 0);

NSVGrasterizer* rast = nsvgCreateRasterizer();

float a = (2 * (TargetWidth + TargetHeight));
float b = (2 * (OriginalWidth + OriginalHeight));
float ReScaleFactor = a / b;

nsvgRasterize(rast, image, 0, 0, ReScaleFactor, (unsigned char*)bits, TargetWidth, TargetHeight, static_cast<int>(TargetWidth * 4));

nsvgDeleteRasterizer(rast);

nsvgDelete(image);  

return hbitmap; }

Adding a Button & MFCToolBar with a HBitmap to Dialog

BOOL CSvgDlg::OnInitDialog(){
CDialogEx::OnInitDialog();
.....
.....
.....
// Create the button
m_button.Create(_T(" "), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,CRect(50, 50, 100, 100), this, 3);

//create MFCToolBar
if (!m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, 100))
{
   return FALSE;
}    
m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle()
        & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_ANY));

CSize   sizeToolBar = m_wndToolBar.CalcFixedLayout(FALSE, TRUE);
m_wndToolBar.SetWindowPos(NULL, 0, 0, sizeToolBar.cx, sizeToolBar.cy,
        SWP_NOACTIVATE | SWP_NOZORDER);

HBITMAP bitmap = SvgToHbitmap("XXX.svg");

m_button.SetBitmap(bitmap);

int ImageIdx = m_wndToolBar.GetImages()->AddImage(bitmap);

m_wndToolBar.InsertButton(CMFCToolBarButton(ID_APP_MENU, -1, _T("Menu")));
m_wndToolBar.InsertButton(CMFCToolBarButton(ID_APP_ABOUT, ImageIdx, _T("About")));
m_wndToolBar.InsertButton(CMFCToolBarButton(ID_APP_EXIT, -1, _T("Exit")));

}

working with CImageList

m_CImageList.Create(16, 16, ILC_COLORDDB | ILC_MASK, 0, 1);
CBitmap lcbitmap;
lcbitmap.Attach(bitmap);
const COLORREF rgbWhite = 0x00FFFFFF;   
int ImageIdx = m_CImageList.Add(&lcbitmap, rgbWhite);   
m_wndToolBar.GetImages()->CreateFromImageList(m_CImageList);

CImageList working

I could not understand why m_wndToolBar.GetImages()->AddImage(bitmap); is not working and m_wndToolBar.GetImages()->CreateFromImageList(m_CImageList); is working. What could be wrong

latosvarun
  • 93
  • 6
  • Is it because the bitmap might go out of scope? The image list if a member variable of teh dialog so the images persist in memory. Have you tried making the bitmap a member variable? I have not researched to see if the `AddImage` takes ownership of the bitmap data. – Andrew Truckle May 03 '23 at 18:30
  • [`ImageList_Add`](https://learn.microsoft.com/en-us/windows/win32/api/commctrl/nf-commctrl-imagelist_add) makes a copy of the image, so lifetime issues are not a concern. I'm guessing that the call to `GetImages()->AddImage(bitmap)` isn't how you should do this. The [documentation](https://learn.microsoft.com/en-us/cpp/mfc/reference/cmfctoolbar-class#getimages) explains that you should be using `CMFCToolBar::LoadBitmap` instead. Regardless, it appears that the default image list isn't created for images with a per-pixel alpha channel. – IInspectable May 03 '23 at 19:19
  • [32-Bit Antialiased Icons](https://learn.microsoft.com/en-us/windows/win32/controls/image-lists#32-bit-antialiased-icons) explains the details. – IInspectable May 03 '23 at 19:19

0 Answers0