Drawing from CImageList onto CButton
-
I have a CButton-derived class that is owner-drawn and has data members for a pointer to a CImageList and an index to select which image in the list to display. The bitmap resource that is used to create the ImageList is 8-bit and uses green as the background color to be masked. When I draw the images onto the button, the transparency mask works great, but the rest looks bad, like it might be a 4-bit image? Yet it looks fine in the resource editor. I'm missing something crucial. Thanks in advance for any advice. Tym! Here's what I'm doing more or less: The image list is created from a resource: an 8-bit bitmap strip with a green background for the masked transparent color:
CImageList _defaultImages; _defaultImages.Create(IDB_BITMAP_RESOURCE_ID, _imageWidth, 32, 0x0000ff00);
The Button is created dynamically:#define BTN_STYLE BS_PUSHBUTTON|WS_VISIBLE|WS_TABSTOP|WS_CHILDWINDOW|BS_NOTIFY|BS_OWNERDRAW ... CMyButton* _myButton = new CMyButton(); _myButton->Create("",BTN_STYLE,_buttonRect,this,3999); _myButton->SetImageList(&_defaultImages); _myButton->SetImageIndex(DEFAULT_IMAGE);
and I am drawing the image like so:void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); UINT state = lpDrawItemStruct->itemState; ... // draw the bitmap: if (_imageList != NULL && _imageIndex != -1) { CPoint _location(2,2); // Offset the image if button is selected: if (state & ODS_SELECTED) _location.Offset(1,1); this->_imageList->Draw(pDC, _imageIndex, _location, ILD_TRANSPARENT); } ... }
-
I have a CButton-derived class that is owner-drawn and has data members for a pointer to a CImageList and an index to select which image in the list to display. The bitmap resource that is used to create the ImageList is 8-bit and uses green as the background color to be masked. When I draw the images onto the button, the transparency mask works great, but the rest looks bad, like it might be a 4-bit image? Yet it looks fine in the resource editor. I'm missing something crucial. Thanks in advance for any advice. Tym! Here's what I'm doing more or less: The image list is created from a resource: an 8-bit bitmap strip with a green background for the masked transparent color:
CImageList _defaultImages; _defaultImages.Create(IDB_BITMAP_RESOURCE_ID, _imageWidth, 32, 0x0000ff00);
The Button is created dynamically:#define BTN_STYLE BS_PUSHBUTTON|WS_VISIBLE|WS_TABSTOP|WS_CHILDWINDOW|BS_NOTIFY|BS_OWNERDRAW ... CMyButton* _myButton = new CMyButton(); _myButton->Create("",BTN_STYLE,_buttonRect,this,3999); _myButton->SetImageList(&_defaultImages); _myButton->SetImageIndex(DEFAULT_IMAGE);
and I am drawing the image like so:void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); UINT state = lpDrawItemStruct->itemState; ... // draw the bitmap: if (_imageList != NULL && _imageIndex != -1) { CPoint _location(2,2); // Offset the image if button is selected: if (state & ODS_SELECTED) _location.Offset(1,1); this->_imageList->Draw(pDC, _imageIndex, _location, ILD_TRANSPARENT); } ... }
I found it. When creating the imagelist directly from the resource, the ImageList defaults to 4-bit apparently. So I has to do it the long way:
CBitmap _imageBitmap _imageBitmap.LoadBitmap(IDB_BITMAP_RESOURCE_ID); CImageList _defaultImages; _defaultImages.Create(_imageWidth, _imageHeight, ILC_COLOR32|ILC_MASK, 1, 32); _defaultImages.Add(&_imageBitmap,0x0000ff00);
This will create a 32-bit imagelist with a green mask from the resource.