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.
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);
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