简体   繁体   English

MFC:如何更改整个editBox的背景颜色?

[英]MFC: How do I change the background color of the whole editBox?

My questions is similar to: win32 : display editbox with black color in text area on windows mobile 5 我的问题类似于: win32:在Windows Mobile 5的文本区域中以黑色显示编辑框

However I'm using MFC which doesn't have the same solution available as the one in the above link. 但是,我使用的MFC的解决方案与上述链接中的解决方案不一样。

How do I change the background color of the whole background, not just the background behind the text of an edit box? 如何更改整个背景的背景颜色,而不仅是编辑框文本后面的背景?

Below is my code, that only changes the background behind the text, not the whole background of the edit box. 下面是我的代码,仅更改文本后面的背景,而不更改编辑框的整个背景。

HBRUSH CGadgetStandardDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CStandardDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    pDC->SetBkColor(RGB(255,255,255));

    return hbr;
}

In addition to calling SetBkColor you need to return a HBRUSH of the desired background color. 除了调用SetBkColor之外,您还需要返回所需背景色的HBRUSH。 So create a brush earlier (say, in the dialog constructor): 因此,请较早创建画笔(例如,在对话框构造函数中):

m_brBack.CreateSolidBrush(RGB(0, 255, 0));

And then return that brush when called for the control of interest: 然后在需要控制感兴趣时返回该画笔:

HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_EDIT2)
{
    pDC->SetBkColor(RGB(0,255,0));
    hbr = m_brBack;
}

Rename your button resource like below. 重命名您的按钮资源,如下所示。

CButton  m_StopButtonto;

to

CMFCButton  m_StopButton;

Change some visible features 更改一些可见的功能

// Set the background color for the button text.
  m_StopButton.SetFaceColor(RGB(255,0,0),true);
  m_StopButton.SetTextColor(RGB(0,0,255));
  // Set the tooltip of the button.
  m_StopButton.SetTooltip(_T("This is my Stop Button!"));

I tried this solution for button and it worked for me. 我尝试了按钮的此解决方案,它为我工作。 I guess it will work for the other components. 我想它将对其他组件起作用。

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

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