0

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.

egrunin
  • 24,650
  • 8
  • 50
  • 93
  • It's unclear, what problem you are trying to solve. It's also not clear, why - although you know the solution - preemptively choose to reject it. – IInspectable Feb 29 '20 at 19:57
  • I want to eliminate the gray margin around the box without having to add significant complexity to the app. I'm not even sure the example I cited is relevant. – egrunin Feb 29 '20 at 20:38
  • What's a *"margin"* to you? It's not something that has a well defined meaning in the windowing API. And what's *"significant complexity"* to you? Or why do you believe, that implementing a custom control would add complexity to your application at all? With all that out of the way, why aren't you asking, why your solution (that appears to be fine for you) fails to display a check box? Isn't that the *real* issue you are trying to solve? – IInspectable Feb 29 '20 at 20:55
  • The question mentions a "gray margin," and includes an illustration of an asymmetrical gray area on three sides of the white indented checkbox. As for complexity, the code I referenced (which might or might not work) uses 35 lines of code to solve a problem that is conceptually trivial; and similar problems are often solved by changing a parameter or two. Indeed, I don't understand why the SDK gives the illustrated result in the first place, and I've been programming Windows since version 2.0 – egrunin Mar 05 '20 at 16:59
  • I don't actually find the 1st answer "cryptic" at all, as all these features are adequately documented. The 2nd answer is indeed an overkill. Anyways, have you solved the issue, or you still need an answer? – Constantine Georgiou Mar 05 '20 at 19:16

1 Answers1

0

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;
}
egrunin
  • 24,650
  • 8
  • 50
  • 93