1

I have an mfc application. I have some richedit controls on the dialog. I want to show a yellow colored filled frame around the controls. What is the way to do this?

enter image description here I tried to create one more rich edit ctrl around the existing richedit ctrl and use SetBackgroundColor on its variable, but it colors the entire area and other richedit ctrls become invisible. Also, I want to change the surrounding color at run time. Please help me. I am stuck with this.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
SGP
  • 83
  • 10
  • another way to deal with this would be changing the background color of the dialog itself. check it here http://stackoverflow.com/questions/17051982/change-the-background-color-of-a-dialog-box-mfc so you can get the rect from the edit control, inflate it and then color that area as you wish – Robson Jul 16 '15 at 07:29

1 Answers1

2

There may be a better way to accomplish this, but, the following should work. If you derive your own class from CRichEditCtrl, you can leverage the WM_NCPAINT message to render the border. Something like…

void RichEdit::OnNcPaint()
    {
    CPaintDC dc(this); // device context for painting
    CRect rect;
    GetWindowRect(&rect);
    ScreenToClient(rect);

    CPen pen;
    pen.CreatePen(PS_SOLID, 10, RGB(255, 255, 0));
    dc.SelectObject(pen);
    dc.Rectangle(&rect);

    CHARFORMAT cf = { 0 };
    int txtLen = GetTextLength();

    cf.cbSize = sizeof(cf);
    cf.dwMask = CFM_ITALIC;

    SetSel(txtLen, -1); ReplaceSel("Some text"); 

    // Apply formating to the just inserted text.
    SetSel(txtLen, GetTextLength());
    SetSelectionCharFormat(cf);
    SetFocus();

    // Do not call CRichEditCtrl::OnNcPaint() for painting messages
    }

Will render the border as Yellow, and, write the corresponding text. Here’s what it will look like.

enter image description here

rrirower
  • 4,338
  • 4
  • 27
  • 45
  • is the yellow border Inside the edit's client area or is it outside? I'm asking because the pen has width=10, so where will it be actually centered? – Robson Jul 16 '15 at 07:25
  • @rrirower your solution is working fine for the first time when I draw my RichEdit ctrl. But in the beginning I want it to be of default color and at run time when my code enters a certain condition, I want this box to become yellow and again after sometime when I encounter some other condition, I want its color to change to the original color. Please suggest what should be done in this particular case. – SGP Jul 16 '15 at 11:23
  • 1
    @Robson The pen width of 10 is arbitrary. If you use Spy++ to spy the window, you'd see that the painting occurs outside the client area of the window, but, within the non-client area. – rrirower Jul 16 '15 at 12:11
  • 1
    @SGP You should look at using a member variable or flag within the derived rich edit control that defaults to the color (and any other attributes - like pen width) that you want to use based on "some condition". Then define a member method (or some other mechanism) that allows you to update the current state of the drawing attributes before the control is rendered. – rrirower Jul 16 '15 at 12:15