Search code examples
mfccustom-controlscbutton

How can I get CMFCButton to look like a standard checkbox?


A standard MFC CButton checkbox on a color background looks like this:

color fringing on checkbox enter image description here

I want to get rid of the gray margin, but can't get it to go away. No matter how I set the size of the control it still appears.

Changing it to an CMFCButton gives this:

unchecked checked

Which is good, it gets rid of the margin, except now there's no checkmark. I need that checkmark.

Is there any way to get the clean appearance with the checkmark? I had the thought of passing the standard set of images to CMFCControl::SetImage(), but I don't see how to get them. I know I can draw everything myself, but I'm trying to avoid reinventing the wheel.

I know there are many similar questions here on SO, but none of the answers I found seem to apply. The closest I found was this: Once and for all: how do I get a fully transparent checkbox, button, radio button, etc. in Windows API, and not with a black background?; but the first answer there is very cryptic, and the second is a big chunk of code that seems like overkill.


Solution

  • I found something simple that worked:

    After adding ON_WM_CTLCOLOR() to the dialog's MESSAGE_MAP, I added this to the dialog class:

    HBRUSH MyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
    
        if (myCheckbox.m_hWnd == pWnd->m_hWnd) // myCheckbox is the problem control
        {
            pDC->SetBkMode(TRANSPARENT);
            hbr = reinterpret_cast<HBRUSH>(::GetStockObject(NULL_BRUSH));
        }
    
        return hbr;
    }