Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Edit control with a colored border?

756 views
Skip to first unread message

Woody

unread,
Jun 19, 2011, 3:41:34 AM6/19/11
to
What is the easiest way to get an edit control (for an MFC dialog)
which as an interior border that can be turned on or off.

The effect is to show the edit box is "active" in some way (control
will not have focus). So when the border is off, it will look like a
normal edit control; when the border is on, user will see the text
within a colored box (inside the normal border of the edit control).

Nobody

unread,
Jun 19, 2011, 8:09:13 AM6/19/11
to
"Woody" <ols...@sbcglobal.net> wrote in message
news:cfb9f2c6-5c39-4f49...@e17g2000prj.googlegroups.com...

Are you talking about the background color? If so, see SetBkColor().


ScottMcP [MVP]

unread,
Jun 19, 2011, 12:05:17 PM6/19/11
to

It is not practical to put a border inside an edit control. You can
change the control's background color, or you can paint a border
outside the control, or you can put a CStatic under the control and
change its visibility on/off.

Woody

unread,
Jun 19, 2011, 1:39:38 PM6/19/11
to
No, I didn't want to change the background color. The static control
would work if the border were around the edit control, but not within
it (I used this technique once). And, it depends on the painting
order, as the static would have to be painted first.

I suppose you'd have to use some kind of owner draw.

Joseph M. Newcomer

unread,
Jun 19, 2011, 4:16:33 PM6/19/11
to
I have tried doing things like this, and it is very difficult; edit controls have their
own notion of reality, and trying to do something else with it will be doomed.

What I ended up doing (and what worked) was putting a static control, owner-drawn, around
the edit control, and drawing it as either entirely in the dialog color (COLOR_3DFACE) or
in the desired highlight color (I was using red to indicate required fields that were not
filled in, for example).

In my validating edit control, I used background color to indicate completeness of a
value; even managing background color took a lot of work to force the edit control to
cooperate.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Joseph M. Newcomer

unread,
Jun 19, 2011, 4:18:50 PM6/19/11
to
Painting order does not matter. If the painting order DOES matter, you have done
something else wrong.

You need to have the correct z-axis position (note that prior to the VS .NET versions, the
z-axis specified in the dialog editor was inverted from the correct z-axis), and you
really should have the WS_CLIPCHILDREN and WS_CLIPSIBLINGS styles set for your dialog. But
it does work, and I've done it, and drawing order, over which you have no control anyway,
doesn't matter.
joe

Woody

unread,
Jun 27, 2011, 3:41:46 AM6/27/11
to
I found a way to accomplish this in a fairly straight-forward manner.
I used the background erase to generate the border, in WM_CTLCOLOR
handler. I created a bitmap of the correct size (74 x 20), and then a
brush from the bitmap. I passed this brush in the handler.

For demonstrating that it works, I hand-calculated the bitmap size,
but it could be done dynamically.

The only drawback with this method is that the border can only be 1
pixel wide to maintain separation from the text background. I'm using
a normal edit control, not rich edit, in which I would have more
control over the margins.

0 new messages