Eto.Drawing - Rounded Rectangle?

Hello,

I’m attempting to create a “FillRectangle” that has rounded corners which I can specify the radius of.

Here’s the form I’ve created thus far to test with:

Note the pink and orange corners are the corners of the FillRectangles that I would like to be able to set a radius on.

Ideally I could control the radius at each corner with a separate float value.

I see a “GetRoundedRect” in the API docs but I can’t find anything about Drawing a Rounded Rectangle.

Thanks for your help!

Here’s my code I’m testing with:

import Eto
import Rhino
import rhinoscriptsyntax as rs

class drawShapes(Eto.Forms.Form):
    def __init__(self):
        self.Size = Eto.Drawing.Size(400, 600)  # Increase the size of the form
        self.WindowStyle = Eto.Forms.WindowStyle.None
        self.Resizable = True
        self.Padding = Eto.Drawing.Padding(30)
        self.MovableByWindowBackground = True

        self.Styles.Add[Eto.Forms.Panel]("transparent", self.MyFormStyler)
        self.Style = "transparent"

        pixelformat = Eto.Drawing.PixelFormat.Format32bppRgba
        bitmap = Eto.Drawing.Bitmap(self.Size, pixelformat)
        graphics = Eto.Drawing.Graphics(bitmap)

        background_color = Eto.Drawing.Colors.LightGrey
        brush_1 = Eto.Drawing.SolidBrush(background_color)

        button_color = Eto.Drawing.Colors.White
        brush_2 = Eto.Drawing.SolidBrush(button_color)
        button_height = 94
        
        # Create Background Rectangle 
        graphics.FillRectangle(brush_1, 0, 0, self.Width, self.Height)

        # Create Button Rectangle    
        graphics.FillRectangle(brush_2, 10,10, (self.Width - 20), button_height)


        # Load the image (for testing)
        image_path = r"image_file_path_test_insert_your_own_file_path_to_image_here"
        image = Eto.Drawing.Bitmap(image_path)

        # Draw the image with specified size
        image_width = 82
        image_height = 42
        graphics.DrawImage(image, (self.Width - (image_width + 20)), 31, image_width, image_height)

        graphics.Dispose()

        self.image_view = Eto.Forms.ImageView()
        self.image_view.Image = bitmap
        self.Title = "Custom Image Button Toggle"

        closeButtonSize = Eto.Drawing.Size(100,50)

        # Create Close Button
        self.Button = Eto.Forms.Button(Text = "CLOSE")
        self.Button.Size = closeButtonSize
        self.Button.BackgroundColor = Eto.Drawing.Colors.OrangeRed
        self.Button.Click += self.OnCloseButtonClick

        # Add Button To Layout
        self.layout = Eto.Forms.DynamicLayout()
        self.layout.AddRow(self.image_view)
        self.layout.AddRow(self.Button)
        
        self.Content = self.layout

    def MyFormStyler(self, control):
        self.BackgroundColor = Eto.Drawing.Colors.Transparent
        window = control.ControlObject
        if hasattr(window, "AllowsTransparency"):
            window.AllowsTransparency = True
        if hasattr(window, "Background"):
            brush = window.Background.Clone()
            brush.Opacity = 0.1
            window.Background = brush
        else:
            color = window.BackgroundColor
            window.BackgroundColor = color.FromRgba(0,0,0,25)


    def OnCloseButtonClick(self, sender, e):
        self.Close()

def EstablishForm():
    
    form = drawShapes()
    form.Owner = Rhino.UI.RhinoEtoApp.MainWindow
    form.Show()

if __name__=="__main__":
    EstablishForm()

1 Like

Hi @michaelvollrath, try this simplified version of your form:

DrawRoundRectangle.py (2.0 KB)

I’ve used the same radius of 20 for all four corners:

path = Eto.Drawing.GraphicsPath.GetRoundRect(rect, 20, 20, 20, 20)

_
c.

1 Like

Beautiful, thank you so much @clement. The “Get” portion of GetRoundRect confused me because I was thinking of Get/Set in terms get retrieving variable data and set well… setting it. I would have never guessed that you first define a regular rectangle and then use that for a round one haha.

Thanks for your help!

EDIT:

Working great! Thanks again!

image

1 Like