Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMFCButton::SetImage - Bitmaps won't show

Tags:

c++

winapi

mfc

I'm trying to load bitmaps for my buttons with the function SetImage from the CMFCButton. I don't get any error or something, just a plain button. I'm doing the same thing with icons and it works, but I need it to load bitmap too. I need to LoadImage from a path and not from the resources.

Here's my code :

iconResource = path + m_type + _T("U") + extension; //i.e : C:\test\earthU.bmp
HANDLE hIcon =  ::LoadImage(nullptr, iconResource, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
//same thing for Hot and Disable bitmap

and the call for the SetImage function :

SetImage((HBITMAP)hIcon, 0, (HBITMAP)hIconHot, 0 , (HBITMAP)hIconDis);
like image 965
Emile Avatar asked Oct 27 '22 10:10

Emile


1 Answers

Use LR_CREATEDIBSECTION|LR_LOADFROMFILE flag when loading bitmap files for CMFCButton

::LoadImage(nullptr, bitmapfile, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_LOADFROMFILE);

Partial explanation:

MFC source code for CMFCButton ("afxbutton.cpp") shows it adds LR_CREATEDIBSECTION for LoadImage. This is not documented and it is not clear why it needs that. It seems LR_CREATEDIBSECTION is required when source bitmap is not 32-bit.

like image 56
Barmak Shemirani Avatar answered Nov 13 '22 06:11

Barmak Shemirani