如何更改MFC单选按钮的背景颜色 [英] How to change the background colour of an MFC radio button

查看:131
本文介绍了如何更改MFC单选按钮的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些单选按钮(CButton),它们已放置在CWnd中,然后在CDialog上显示.

单选按钮的背景色是黑色,而不是通常的Windows对话框背景色.如何使CWnd中的单选按钮的背景颜色与CDialog的背景颜色相同?

这是我创建CWnd和单选按钮的代码:

I have some radio buttons (CButtons) which I have placed in a CWnd which is then diplayed on a CDialog.

The background colour of the radio button is black, not the usual windows dialog background colour. How can I make the background colour of the radio button in the CWnd the same as the background colour of the CDialog?

This is my code to create the CWnd and the radio buttons:

CWnd *ctlGroupBox = new CWnd;
CButton *rdo1 = new CButton;
CButton *rdo2 = new CButton;

ctlGroupBox->Create(_T("BUTTON"),
		_T("Pizza Size"),
		WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
		CRect(50, 10, 200, 160),
		this,
		15);

rdo1->Create(_T("Large1"),
	        WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
	        CRect(40, 80, 120, 140), ctlGroupBox, 0x16);
rdo2->Create(_T("Large2"),
	        WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
                CRect(40, 130, 120, 140), ctlGroupBox, 0x16);

推荐答案

是否要使用CRadioButton代替CButton?


派生一个CButton中的类,并在ON_WM_ERASEBKGND()中进行代码
例如:
derive a class from CButton and do code in ON_WM_ERASEBKGND()
example:
class CMyButton : public CButton
{
    DECLARE_DYNAMIC(CMyButton)
public:
    CMyButton();
    virtual ~CMyButton();
protected:
    DECLARE_MESSAGE_MAP()
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    afx_msg void OnPaint();
};

// CMyButton
IMPLEMENT_DYNAMIC(CMyButton, CButton)
CMyButton::CMyButton()
{
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
    ON_WM_ERASEBKGND()
    ON_WM_PAINT()
END_MESSAGE_MAP()
// CMyButton message handlers
BOOL CMyButton::OnEraseBkgnd( CDC* pDC )
{
    CRect Rect;
    GetClientRect( Rect );
    pDC->FillSolidRect( Rect, RGB( 255, 0, 0));
    return TRUE;
}
void CMyButton::OnPaint()
{
    CPaintDC dc(this); // device context for painting
    // TODO: Add your message handler code here
    // Do not call CButton::OnPaint() for painting messages
}


这篇关于如何更改MFC单选按钮的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆