是否可以更改CMFCEditBrowseCtrl控件的背景颜色? [英] Is it possible to change the background colour of a CMFCEditBrowseCtrl control?
问题描述
VS2015对话框MFC
VS2015 Dialog MFC
我在对话框上实现了几个 CMFCEditBrowseCtrl ,并且具有浏览按钮的自定义行为:
I have several CMFCEditBrowseCtrl implemented on my dialog with custom behavior for the browse button:
是否可以在运行时有条件地将控件的 edit 部分的背景设置为 red ?并在需要时将编辑背景设置回默认?
Is it possible to conditionally set the background of the edit part of the control to red at run time? And when required set the background of the edit back to the default?
谢谢.
更新 我看到该控件是从 CEdit 派生的,因此我将尝试一下:
Update I see that the control is derived from CEdit, so I am going to give this a try:
推荐答案
链接上方的答案在右侧方向,但是未正确实施. CtlColor
应该返回一个刷柄.它还需要使用CDC::SetBkColor
The answer from above link is in the right direction, however it is not implemented correctly. CtlColor
should return a brush handle. It also needs to set text background color with CDC::SetBkColor
class cmfc_edit : public CMFCEditBrowseCtrl
{
public:
COLORREF bkcolor;
CBrush brush;
void setBrushColor(COLORREF clr)
{
bkcolor = clr;
brush.DeleteObject();
brush.CreateSolidBrush(clr);
}
HBRUSH CtlColor(CDC* pDC, UINT)
{
if (!brush.GetSafeHandle())
return GetSysColorBrush(COLOR_WINDOW);
pDC->SetBkColor(bkcolor);
return brush;
}
//optional, change color on focus change
void OnSetFocus(CWnd* w)
{
setBrushColor(RGB(255, 0, 0));
CMFCEditBrowseCtrl::OnSetFocus(w);
}
void OnKillFocus(CWnd* w)
{
setBrushColor(RGB(255, 255, 255));
CMFCEditBrowseCtrl::OnKillFocus(w);
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(cmfc_edit, CMFCEditBrowseCtrl)
ON_WM_CTLCOLOR_REFLECT()
//optional
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
END_MESSAGE_MAP()
用法:
mfc_edit.setBrushColor(RGB(255, 0, 0));
这篇关于是否可以更改CMFCEditBrowseCtrl控件的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!