简体   繁体   English

更改编辑控件中文本的背景

[英]Changing background of text in edit control

Can you change the background of text in area of edit control that would stay static?你能改变编辑控件区域的文本背景吗? static?

In the parent of the edit control, handle the WM_CTLCOLORSTATIC message, the wParam of this message is the HDC that the Edit control is about to draw with, for most CTLCOLOR messages, if you set text and background colors into this DC, the control will use the colors you set. 在编辑控件的父级中,处理WM_CTLCOLORSTATIC消息,此消息的wParam是Edit控件即将绘制的HDC,对于大多数CTLCOLOR消息,如果将文本和背景颜色设置为此DC,控件将使用你设置的颜色。

You can also return an HBRUSH and the contol will use that for any brush painting that it wil do, but many controls don't use brushes much, so that will have limited effect for some CTLCOLOR messages. 你也可以返回一个HBRUSH,并且控制器会将它用于任何画笔绘画,但许多控件不会使用很多画笔,因此对某些CTLCOLOR消息的影响有限。 Your best bet here is to return the DC brush, and set the DC Brush color to match the BkColor of the DC. 这里最好的选择是返回DC画笔,并设置DC画笔颜色以匹配DC的BkColor。

 LRESULT lRet = 0; // return value for our WindowProc.
 COLORREF crBk = RGB(255,0,0); // use RED for Background.

 ... 

 case WM_CTLCOLORSTATIC:
    {
    HDC hdc = (HDC)wParam;
    HWND hwnd = (HWND)lParam; 

    // if multiple edits and only one should be colored, use
    // the control id to tell them apart.
    //
    if (GetDlgCtrlId(hwnd) == IDC_EDIT_RECOLOR)
       {
       SetBkColor(hdc, crBk); // Set to red
       SetDCBrushColor(hdc, crBk);
       lRet = (LRESULT) GetStockObject(DC_BRUSH); // return a DC brush.
       }
    else
       {
       lRet = DefWindowProc(hwnd, uMsg, wParam, lParam);
       }
    }
    break;

WM_CTLCOLORSTATIC is for static text control. WM_CTLCOLORSTATIC用于静态文本控制。

To be simple, you can do this in your winproc: 简单来说,您可以在winproc中执行此操作:

...
case WM_CTLCOLOREDIT:
{
    HDC hdc = (HDC)wParam;
    SetTextColor(hdc, yourColor);  // yourColor is a WORD and it's format is 0x00BBGGRR
    return (LRESULT) GetStockObject(DC_BRUSH); // return a DC brush.
}
...

If you have more than 1 edit control, you can use the item id and lParam to check which one need to be change. 如果您有多个编辑控件,则可以使用item id和lParam来检查哪一个需要更改。

WM_CTLCOLOREDIT允许你设置文本和背景颜色(+画笔),如果你想要更多的控制,你必须子类化并自己画画

you could do something like this: 你可以这样做:

CBrush bkBrush;
RECT ctrlRect;
COLORREF crBk = RGB(255,0,0); // Red color
bkBrush.CreateSolidBrush(crBk);

CWnd* pDlg = CWnd::GetDlgItem(IDC_EDIT);
pDlg->GetClientRect(&ctrlRect);
pDlg->GetWindowDC()->FillRect(&ctrlRec, &bkBrush);
pDlg->GetWindowDC()->SetBkColor(crBk);

This should change the background color of the edit control 这应该会更改编辑控件的背景颜色

All you need is to set the required color in control's device context and pass an HBRUSH with same color in WM_CTLCOLOREDIT message.您只需要在控件的设备上下文中设置所需的颜色,并在 WM_CTLCOLOREDIT 消息中传递具有相同颜色的 HBRUSH。 If you want to change both foreground & background colors, use SetTextColor t0 change the text color.如果要更改前景和背景 colors,请使用 SetTextColor t0 更改文本颜色。 But you must pass the background color HBRUSH.但是你必须通过背景色HBRUSH。 But if you want to change the text color only, then you must pass a DC_BRUSH with GetStockObject function.但是,如果您只想更改文本颜色,则必须使用 GetStockObject function 传递 DC_BRUSH。

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

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