简体   繁体   English

从派生的 CEdit 更改文本和背景颜色 class

[英]Change text and background color from a CEdit derived class

I have a derived class from CEdit and I want to change the text and background color.我有一个来自CEdit的 class,我想更改文本和背景颜色。 I've tried with the messages ON_WM_CTLCOLOR_REFLECT and ON_WM_CTLCOLOR , but I never receive these messages.我试过消息ON_WM_CTLCOLOR_REFLECTON_WM_CTLCOLOR ,但我从未收到这些消息。 Is there another option beside ON_PAINT ?除了ON_PAINT还有其他选择吗?

Here's how I initialize my CColorEdit control:以下是我如何初始化CColorEdit控件:

//in my dialog.h
CColorEdit m_test;
//in .cpp
DDX_Control(pDX, IDC_TEST, m_test);

I can't set the color manually like this:我不能像这样手动设置颜色:

m_test.SetTextColor(...);

Here's how I've tried to handle ON_WM_CTLCOLOR :这是我尝试处理ON_WM_CTLCOLOR的方式:

BEGIN_MESSAGE_MAP(CColorEdit, CEdit)
    ON_WM_CTLCOLOR()
END_MESSAGE_MAP()

HBRUSH CColorEdit::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CEdit::OnCtlColor(pDC, pWnd, nCtlColor);
    if (m_theme != 0)
    {
        pDC->SetTextColor(RGB(0, 255, 0));
    }
    return hbr;
}

Here's my try with WM_CTLCOLOR_REFLECT :这是我对WM_CTLCOLOR_REFLECT的尝试:

BEGIN_MESSAGE_MAP(CColorEdit, CEdit)
    //{{AFX_MSG_MAP(CColorEdit)
    ON_WM_CTLCOLOR_REFLECT()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CColorEdit::SetTextColor(COLORREF textColor)
{
    m_textColor = textColor;
    Invalidate();
}
void CColorEdit::SetBkColor(COLORREF backgroundColor)
{
    m_backgroundColor = backgroundColor;
    m_brBkgnd.DeleteObject();
    m_brBkgnd.CreateSolidBrush(backgroundColor);
    Invalidate();
}

HBRUSH CColorEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
    pDC->SetBkColor(m_backgroundColor);
    pDC->SetTextColor(m_textColor);

    if (nCtlColor)       // To get rid of compiler warning
        nCtlColor += 0;

    return hbr;
}

Thx.谢谢。

Look here .这里 To quote:去引用:

It was a real challenge for me to make a simple change of background color for CEdit .简单地更改CEdit的背景颜色对我来说是一个真正的挑战。 I was suprised to find out that there are no standard function, like CEdit::SetBkColor , to do this.我很惊讶地发现没有标准的 function(例如CEdit::SetBkColor )来执行此操作。

First step is to create class derived from CEdit and declare function CtlColor :第一步是创建从CEdit派生的 class 并声明 function CtlColor

// CustomEdit.h
class CCustomEdit : public CEdit
{
protected:
  HBRUSH CtlColor(CDC *pDC, UINT);
  DECLARE_MESSAGE_MAP()
};

Second, handle WM_CTLCOLOR_REFLECT event:二、处理WM_CTLCOLOR_REFLECT事件:

// CustomEdit.cpp
BEGIN_MESSAGE_MAP(CCustomEdit, CEdit)
  ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()

HBRUSH CCustomEdit::CtlColor(CDC *pDC, UINT)
{
  COLORREF bkColor = RGB(255, 255, 255);
  pDC->SetBkColor(bkColor);
  return CreateSolidBrush(bkColor);
}

So you need to use both of them!所以你需要同时使用它们!

If you made the CColorEdit class only for performing this kind of custom color drawing, it wasn't needed, because you can simply process the WM_CTLCOLOR messages in the parent window. The message is actually WM_CTLCOLOREDIT ( WM_CTLCOLOR was used in old windows versions), however MFC maps all WM_CTLCOLORxxxx messages to the ON_WM_CTLCOLOR handler and passes the control-type as a paremeter.如果你制作CColorEdit class 只是为了执行这种自定义颜色绘图,则不需要它,因为你可以简单地处理父 window 中的WM_CTLCOLOR消息。该消息实际上是WM_CTLCOLOREDITWM_CTLCOLOR在旧的 windows 版本中使用),然而 MFC 将所有WM_CTLCOLORxxxx消息映射到ON_WM_CTLCOLOR处理程序并将控件类型作为参数传递。

Here is some code:这是一些代码:

#define COLOR_YELLOW RGB(255,255,0)
HBRUSH hBrYellow = []() { return CreateSolidBrush(COLOR_YELLOW); }();

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    // Our custom edit-control
    if (nCtlColor == CTLCOLOR_EDIT && pWnd->GetDlgCtrlID() == IDC_MYCUSTOMEDIT)
    {
        pDC->SetBkColor(COLOR_YELLOW);
        return hBrYellow;
    }

    // All the rest
    return CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
}

This code sample sets the background color of the edit-control to yellow.此代码示例将编辑控件的背景颜色设置为黄色。 The edit-control is a simple and standard windows edit-box, no need for subclassing.编辑控件是一个简单标准的 windows 编辑框,无需子类化。

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

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