
CEditView Background Color & Font Color :: MFC
A long time ago, before proper programming techniques were well-understood, Microsoft
decreed that the parent of a control would receive a WM_CTLCOLOR message. This was, of
course, remarkably silly, since there is no sensible way a parent of a control would know
anything about how the control wanted things painted. The result was some of the
worst-structured programs ever written. The same thing applied to WM_MEASUREITEM,
WM_DRAWITEM, and WM_COMPAREITEM, among may other messages.
MFC fixes this disaster by
detecting WM_CTLCOLOR and other such messages, and routing them to the child class, where
they should be processed. By subclassing the control, you put the knowledge about how it
should be painted, compared, etc. down in the control, where it belongs. Strange thing was
that I had discovered this technique years before, and would subclass a control and add a
WM_CTLCOLOR, WM_DRAWITEM, WM_MEASUREITEM, and WM_COMPAREITEM handler to my subclass. I
would then program the mainframe to SendMessage these messages to the control whose handle
appeared in the message. Felt smug when I discovered MFC was doing the same thing.
Note that the =WM_CTLCOLOR and WM_CTLCOLOR handlers both do the same thing; you are
setting the background color FOR THE CHARACTER CELLS ONLY and not for the entire control.
You need to add WM_ERASEBKGND to erase anything outside the character cells. There are
potential problems with this as WM_ERASEBKGND can use a broader set of colors than the
SetBkColor any time you are running on less than a 24-bit-color display, so you will get
ugly mismatches on paletted displays unless you are very, very clever.
You might want to look at my "validating edit control" on my MVP Tips site for some more
insight on how to handle this, including the need to occasionally invalidate the entire
control to cause the proper updating.
joe
Quote:
>Oh. Nice!!!
>Everything works great. I have question additional
>questions.
>First, what is the difference between a normal message
>WM_CTLCOLOR and a "reflected" version =WM_CTLCOLOR? I
>have always wondered about the "=" prefix.
>Secondly, I am using a CEditView. The technique you
>mentioned does work, which I am very happy, this is one
>annoying problem. It seems =WM_CTLCOLOR only changes the
>area of the view where there are texts and where the
>cursor is located. For example:
>// imagine this is what my CEditView looks like
>// imagine I want the background to be green
>-----
>testing // green area
>1 // green area
>2 // green area
> // green area
>3 // green area
> // white area
> // white area
> // white area
> // white area
>-----
>I am not sure how to overcome something like that.
>Kuphryn
Joseph M. Newcomer [MVP]
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm