How to clip out the region outside the custom painted rectangle

19 views
Skip to first unread message

Sean Mayard

unread,
Jun 5, 2022, 10:11:23 AM6/5/22
to wx-users
I am trying to know if there is a way to remove(clip out) the uneeded region outside the custom painted rectangle. The code that i currently have is:

void CustomPanel::onPaint(wxPaintEvent &event)
{
    wxSize current_Screensize = this->GetSize();
    wxPaintDC dc(this);
     wxBrush brush(themeColour, wxBRUSHSTYLE_TRANSPARENT);
    dc.SetBrush(brush);
    dc.SetPen(wxPen(wxColour("#000000"), 1, wxPENSTYLE_DOT));
    dc.DrawRoundedRectangle(3,3, GetSize().x -6,GetSize().y-6, 5);
}

The above draws a rounded rectangle on the screen as can be seen in the attached screenshot. But as you will notice in the screenshot there is this region(corresponding to the current wxPanel) outside the rounded rectangle and i want to get rid of this outside region. Is there a way to do this so that the final result will be as shown in the attached screenshot named expectedresult.png. Basically i want that only the custom painted rounded rectangle should be visible and the extra outside part should be cut/clip out.




Currentresult.png
Expectedresult.png

Vadim Zeitlin

unread,
Jun 5, 2022, 1:06:37 PM6/5/22
to wx-u...@googlegroups.com
On Sun, 5 Jun 2022 06:50:17 -0700 (PDT) Sean Mayard wrote:

SM> I am trying to know if there is a way to remove(clip out) the uneeded
SM> region outside the custom painted rectangle. The code that i currently have
SM> is:
SM>
SM> void CustomPanel::onPaint(wxPaintEvent &event)
SM> {
SM> wxSize current_Screensize = this->GetSize();
SM> wxPaintDC dc(this);
SM> wxBrush brush(themeColour, wxBRUSHSTYLE_TRANSPARENT);
SM> dc.SetBrush(brush);
SM> dc.SetPen(wxPen(wxColour("#000000"), 1, wxPENSTYLE_DOT));
SM> dc.DrawRoundedRectangle(3,3, GetSize().x -6,GetSize().y-6, 5);
SM> }

You're intentionally leaving the border of size 3 outside of the rectangle
here. If you don't want to have it, why do you do it? Just don't, and use
0,0 as the top-left corner and GetSize() as the bottom-right one and it
should give you the result you're looking for.

Regards,
VZ

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

Sean Mayard

unread,
Jun 6, 2022, 12:38:58 AM6/6/22
to wx-u...@googlegroups.com
No, i also want to do the same for a circle. I know that here in this rectangle example i have intentionally left the region outside as you said. But suppose instead of creating a rectangle i created a circle, then i want the region outside that circle to be clipped out. Is there a way to do that? In case of a circle we're not leaving the outside region intentionally.

Gunter Königsmann

unread,
Jun 6, 2022, 12:58:06 AM6/6/22
to wx-u...@googlegroups.com, Sean Mayard
I believe if you want to crop to a circle the best method is to first render the image to a bitmap and to then to blit only the area within the circle to the final destination.

Since blitting supports transparency you might use a transparency map for doing so.

Sean Mayard

unread,
Jun 6, 2022, 2:23:25 AM6/6/22
to Gunter Königsmann, wx-u...@googlegroups.com
@Gunter Is there a working demo where I can find out how this is done?

Sean Mayard

unread,
Jun 6, 2022, 4:02:43 AM6/6/22
to Gunter Königsmann, wx-u...@googlegroups.com
Also i don't know what you mean when you said: "first render the image to a bitmap" . I mean i don't have any image. I am just drawing a circle or a rectangle. This seems like a simple enough problem to me in theory but i don't know what i should do to make this work. I also tried to search about the same(that how to create a circular wxPanel) but didn't find any working demo.

Vadim Zeitlin

unread,
Jun 6, 2022, 9:44:33 AM6/6/22
to wx-u...@googlegroups.com
On Mon, 6 Jun 2022 13:32:28 +0530 Sean Mayard wrote:

SM> Also i don't know what you mean when you said: *"first render the image to
SM> a bitmap" . *I mean i don't have any image. I am just drawing a circle or a
SM> rectangle. This seems like a simple enough problem to me in theory but i
SM> don't know what i should do to make this work. I also tried to search about
SM> the same(that how to create a circular wxPanel) but didn't find any working
SM> demo.

Do you actually want to make the main window itself non-rectangular? If
so, you're looking for wxTopLevelWindow::SetShape(), demonstrated in the
"shaped" sample.

Regards,
VZ

Sean Mayard

unread,
Jun 7, 2022, 6:38:32 AM6/7/22
to wx-u...@googlegroups.com
@VZ I want to do it for wxPanel instead of a top level window like wxFrame. I read about wxDCClipper in wxDCClipper documentation but couldn't find any example of how to use it to achieve the task.

Vadim Zeitlin

unread,
Jun 7, 2022, 9:51:51 AM6/7/22
to wx-u...@googlegroups.com
On Tue, 7 Jun 2022 16:08:16 +0530 Sean Mayard wrote:

SM> @VZ I want to do it for wxPanel instead of a top level window like wxFrame.

All non top level windows are always rectangular. Of course, it's not
really a problem because what else could they be? Even if you could create
a wxPanel in a shape of a circle, what would happen with the pixels inside
the square bounding this circle but outside of the circle itself? They
would just show the wxFrame which is behind the wxPanel, which wouldn't be
very useful at all.

Again, if you want your window to have a non rectangular shape, you need
to use SetShape(). If you want to draw something inside your (rectangular)
window, just draw it -- you don't need to care about the window shape at
all.

From what you're saying it looks like you want to do something else, but
even if it's clear what it is to you, I can assure you that you don't
express it clearly in your post and that it's very difficult to understand
what exactly are you trying to do here.

SM> I read about *wxDCClipper in wxDCClipper documentation
SM> <https://docs.wxwidgets.org/3.0/classwx_d_c_clipper.html> *but couldn't
SM> find any example of how to use it to achieve the task.

You really need to define the task first. wxDC::SetClippingRegion() allows
you to avoid drawing outside of the given region, which can be very useful,
but it's not going to help you at all if you're not drawing anything anyhow
in the first place.
Reply all
Reply to author
Forward
0 new messages