Change the background of text in an edit control

Can you change the background of the text in the editing area that remains stationary?

+3
source share
4 answers

In the parent edit control, handle the WM_CTLCOLORSTATIC message , the wParam of this message is the HDC that the Edit control draws with, for most CTLCOLOR messages, if you set the text and background colors to this DC, the control will use the colors you set.

You can also return HBRUSH, and contol will use this for any brush it will do, but many controls don’t use brushes much, so this will have a limited effect for some CTLCOLOR Messages. It is best here to return the DC brush and set the color of the DC brush in accordance with BkColor DC.

 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;
+11
source

WM_CTLCOLORSTATIC is intended for managing static text.

To be simple, you can do this in your winproc:

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

If you have more than one edit control, you can use the element identifier and lParam to check which one needs to be changed.

+4
source

WM_CTLCOLOREDIT allows you to set the color of text and background (+ brush), if you want more control over this, you must subclass and draw yourself

+2
source

you can 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

0
source

Source: https://habr.com/ru/post/1732774/


All Articles