Drawing selected region in a custom control

27 views
Skip to first unread message

QuentinC

unread,
Mar 13, 2021, 4:33:16 AMMar 13
to wx-u...@googlegroups.com
Hello,

I'm implementing a custom control where the user can select some part of
the content of the control, with whether the keyboard or the mouse.
The content of the control is drawn using wxGraphicsContext.

I would like to indicate to the user what is currently selected:
1 - by inverting colors inside the selected region, a.k.a. inverse
video. Something that is normally black should appear white when
selected, and conversely. Something that is blue should appear yellow
when selected, etc.
2 - Draw the focus rectangle around the selected region, well known for
windows users a.k.a. focus ring. The selection is always rectangle.

For part 1, I don't see any specific method to do it, and don't
understand what kind of brush I'm supposed to use in order to achieve
color inversion.

For part 2, is the specific pen and color normally used for focus
indication under windows documented somewhere ?

void MyCustomCtrl::OnPaint (wxPaintEvent& e) {
wxAutoBufferedPaintDC dc(this);
std::unique_ptr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
...
gc->SetPen(wxPen(wxColour(0, 0, 0), 1, wxPENSTYLE_DOT)); // black dotted
line: is that correct ?
gc->SetBrush(???); // What to do here ?
gc.DrawRectangle(x, y, width, height);
...
}

Thank you for your answers.


QuentinC

unread,
Mar 15, 2021, 5:50:14 PMMar 15
to wx-u...@googlegroups.com
Hello,

Nobody has an answer to my last question ?

So let me add a precision. In my question 1, in fact, I would like to do
the same as InvertRect function in win32, using wxGraphicsContext.

MSDN documentation of InvertRect is here:
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-invertrect

I imagine that, even if the equivalent was available in wxPaintDC, it is
bad practice and/or it doesn't look right to mix drawing with
wxGraphicsContext and wxPaintDC once the wxGraphicsContext has been
created from the DC ?

My original message is below.

Vadim Zeitlin

unread,
Mar 15, 2021, 6:14:51 PMMar 15
to wx-u...@googlegroups.com
On Sat, 13 Mar 2021 10:33:06 +0100 QuentinC wrote:

Q> I'm implementing a custom control where the user can select some part of
Q> the content of the control, with whether the keyboard or the mouse.
Q> The content of the control is drawn using wxGraphicsContext.
Q>
Q> I would like to indicate to the user what is currently selected:
Q> 1 - by inverting colors inside the selected region, a.k.a. inverse
Q> video. Something that is normally black should appear white when
Q> selected, and conversely. Something that is blue should appear yellow
Q> when selected, etc.
Q> 2 - Draw the focus rectangle around the selected region, well known for
Q> windows users a.k.a. focus ring. The selection is always rectangle.
Q>
Q> For part 1, I don't see any specific method to do it, and don't
Q> understand what kind of brush I'm supposed to use in order to achieve
Q> color inversion.

You can't do it with a crush in modern graphics APIs. The solution is to
use wxOverlay and draw something (e.g. just a semitransparent rectangle) on
top of the normal drawing. This may require some rather important changes
to your existing code, but there is just no way around it.

Q> For part 2, is the specific pen and color normally used for focus
Q> indication under windows documented somewhere ?

The pen colour must correspond to some wxSYS_COLOUR_XXX but I don't know
which one, to be honest.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

QuentinC

unread,
Mar 16, 2021, 12:35:15 PMMar 16
to wx-u...@googlegroups.com
Hello,

VZ> You can't do it with a crush in modern graphics APIs. The solution
is to use wxOverlay and draw something (e.g. just a semitransparent
rectangle) on top of the normal drawing. This may require some rather
important changes to your existing code, but there is just no way around it.

OK. That easily explain why I don't find anything beside the InvertRect
function of win32 API.

I think I'll just abandon that idea and stay only with drawing the focus
rectangle. I hope it's sufficient for partially sighted people.
I'll make tests to find the matching system color to use. It isn't easy
because I have the impression that several of the system colors are the
same or almost.

Thank you.
Kind regards.

PB

unread,
Mar 16, 2021, 2:27:13 PMMar 16
to wx-users
Hi,

I'll make tests to find the matching system color to use.

FWIW, there is a utility class I wrote which shows many system and other options, including system colors.

It should be easy to run or include in your application (it's just one header and one source file); you can find it at https://github.com/PBfordev/wxsysinfoframe

Regards,
PB
Reply all
Reply to author
Forward
0 new messages