[英]Cannot figure out how to change MFC control background color
I have been searching for days to figure out how to change a control's background color. 我一直在寻找几天,以找出如何更改控件的背景色。 The most logical solution I found is here: 我发现的最合乎逻辑的解决方案是在这里:
http://www.codeguru.com/cpp/controls/editctrl/backgroundcolor/article.php/c3929/Changing-the-Background-Color-of-Edit-Controls.htm http://www.codeguru.com/cpp/controls/editctrl/backgroundcolor/article.php/c3929/Changing-the-Background-Color-of-Edit-Controls.htm
I implemented this tutorial as follows: 我实现了本教程,如下所示:
class Cbackgroundcolor_mfc_testDlg : public CDialogEx
{
public:
Cbackgroundcolor_mfc_testDlg(CWnd* pParent = NULL);
enum { IDD = IDD_BACKGROUNDCOLOR_MFC_TEST_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
CBrush m_redbrush,m_bluebrush; //<---- here
COLORREF m_redcolor,m_bluecolor,m_textcolor; //<---- here
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
CStatic m_staticTODO;
protected:
// | | | and here
// v v v
HBRUSH Cbackgroundcolor_mfc_testDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr;
switch (nCtlColor)
{
case CTLCOLOR_EDIT:
case CTLCOLOR_MSGBOX:
switch (pWnd->GetDlgCtrlID())
{
case IDC_STATICTODO:
pDC->SetBkColor(m_bluecolor);
hbr = (HBRUSH) m_bluebrush;
break;
}
default:
hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
}
return hbr;
}
};
I also added the necessary lines to OnInitDialog(). 我还向OnInitDialog()添加了必要的行。
As far as I can tell, I have implemented the tutorial properly and yet the control background colors are still not changing. 据我所知,我已经正确实施了本教程,但是控件的背景色仍然没有改变。 Can someone help me figure out what else I need to do to change the background color of a control? 有人可以帮助我找出更改控件背景颜色的其他步骤吗?
OnCtlColor is a message handler for WM_CTLCOLOR. OnCtlColor是WM_CTLCOLOR的消息处理程序。 You need to add this message to the dialog's message map to get the function called. 您需要将此消息添加到对话框的消息映射中才能调用该函数。 Add this line inside the message map: 在消息映射中添加以下行:
ON_WM_CTLCOLOR()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.