简体   繁体   English

我怎样才能让 CMFCButton 看起来像一个标准的复选框?

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

A standard MFC CButton checkbox on a color background looks like this:彩色背景上的标准 MFC CButton复选框如下所示:

复选框上的色边 在此处输入图片说明

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:将其更改为CMFCButton会得到:

未经检查 检查

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.我想过将标准图像集传递给CMFCControl::SetImage() ,但我不知道如何获取它们。 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?我发现的最接近的是: 一劳永逸:如何在 Windows API 中获得完全透明的复选框、按钮、单选按钮等,而不是黑色背景? ; ; but the first answer there is very cryptic, and the second is a big chunk of code that seems like overkill.但是第一个答案非常神秘,第二个答案是一大块代码,看起来有点矫枉过正。

I found something simple that worked:我发现一些简单有效的方法:

After adding ON_WM_CTLCOLOR() to the dialog's MESSAGE_MAP , I added this to the dialog class:ON_WM_CTLCOLOR()添加到对话框的MESSAGE_MAP ,我将其添加到对话框类中:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM