CMFCButton as a radio button

David Webber 136 Reputation points
2020-09-10T14:25:17.083+00:00

I have just discovered that the 'Feature Pack' class CMFCButton allows me to have an image and text on push-like radio button. (Normally this would require it to be owner-draw, and radio buttons don't have the owner-draw possibility, so I'm quite excited about this.)

I have an hBitmap which (for the moment) looks like this: 23886-image.png

I then set it in my CMFCButton derived class constructor with

	EnableWindowsTheming( TRUE );  
  
	BOOL       bAutoDestroy		= TRUE;  
	HBITMAP hBitmapHot		= NULL;  
	BOOL       bMap3dColors	= TRUE;  
	HBITMAP hBitmapDisabled	= NULL;  
	  
	SetImage( hBitmap, bAutoDestroy, hBitmapHot, bMap3dColors, hBitmapDisabled );  
  

and in use on the dialogue box it looks like: 23834-image.png

The button background has become dark grey. It doesn't seem to matter what colour I draw the button background or whether bMap3dColors is TRUE or FALSE; the background always comes out the same grey colour! (I actually want the area to be the button background colour, but having failed at that, I made the bitmap background yellow to see what's happening. And discovered that my confusion seems to be at a deeper level.)

Can anyone tell me what I'm doing wrong?

Dave

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,753 questions
{count} vote

2 answers

Sort by: Most helpful
  1. David Webber 136 Reputation points
    2020-09-10T21:30:50.23+00:00

    Thanks @David Lowndes

    I'm not actually loading a bitmap as such. To allow for high DPI I'm loading an EMF resource, drawing on a bitmap (of appropriate dpi-dependent size) in memory, and then giving it to my push-like radio button with CButton::SetBitmap(...) or, in particular CMFCButton::SetImage(...)

    I've made some progress by doing this in OnInitDialog() rather than the button constructor. The grey has gone but it's still not mapping my input COLOR_BTNFACE=RGB(192,192,192) onto the button colour - empirically RGB(215,215,215). It's coming out as RGB(240,240,240). In fact CButton,and CMFCButton are both doing this, though I think only CMFCButton is documented as doing a mapping.

    So I'm still puzzled.

    Dave


  2. David Webber 136 Reputation points
    2020-09-12T16:06:49.813+00:00

    I think I've cracked it!

    CMFCButton::SetImage( hBitmap, ... bMap3dColors, ...)

    If hBitmap is 32 bits-per-pixel then this function behaves very differently from other values. The source code is complicated, but makes a mention of (COLORREF)-1;

    Now as far as I know COLORREF is only supposed to use the lowest three nybbles, and the high nybble should always be zero. But (COLORREF)-1 is of course 0xFFFFFFFF.

    So I tried ::FillRect( ) with this COLORREF to draw my background on the bitmap, before drawing my image from the EMF resource. And it works. CMFCImage::SetImage( hBitmap,...) treats it as a transparent background! Irrespective, I think, of bMap3dColors.

    24236-image.png

    Thanks to you on this thread for your comments which definitely helped point me in the right direction!

    Dave


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.