Define the form as transparent

I want to define the form as transparent how to do that, control and form transparency separate

import Eto
import Eto.Forms as ef
import Eto.Drawing as ed
from eto_image_py import Images_form
form = ef.Form()
form.Width = 300
form.Height = 300
form.WindowStyle = ef.WindowStyle.NONE
form.Location = ed.PointF(120,120)

img = ef.ImageView()
img.Image = Images_form.title_bar_icon()

but = ef.Button()
but.Text = "Transparency"

layout = ef.DynamicLayout()
layout.Spacing = ed.Size(1,1)
layout.AddRow(img)
layout.AddRow(but,None)
form.Content = layout

form.Show()

@ Michael Vollrath Sir, I need some help

Hi @tom33,

Here’s what you’re looking for:

Cheers!

1 Like

This is great, but I can’t convert this py2 transparent form code to py3 and it works.

@tom33 this version will work for Python 3:

#! python 3

import Rhino
import Eto


class SampleTransparentEtoForm(Eto.Forms.Form):
    def __init__(self):
        super().__init__()

        self.WindowStyle = Eto.Forms.WindowStyle.NONE

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

        self.AutoSize = False
        self.Resizable = True
        self.TopMost = True
        self.ShowActivated = False
        self.Size = Eto.Drawing.Size(300, 300)
        self.Padding = Eto.Drawing.Padding(80)
        self.MovableByWindowBackground = True
        self.Location = Eto.Drawing.Point(100, 100)

        self.Button = Eto.Forms.Button()
        self.Button.Text = "Close"
        self.Button.Click += self.OnButtonClick

        self.layout = Eto.Forms.DynamicLayout()
        self.layout.AddRow(self.Button)

        self.Content = self.layout

    def TransparentFormStyler(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  # Modify this value to 0 on Windows for full transparency
            window.Background = brush
        else:
            color = window.BackgroundColor
            window.BackgroundColor = color.FromRgba(0, 0, 0, 25)  # Modify this value (the 25) to 0 on Mac for full transparency

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


def EstablishForm():

    form = SampleTransparentEtoForm()
    form.Owner = Rhino.UI.RhinoEtoApp.MainWindow
    form.Show()


if __name__ == "__main__":
    EstablishForm()
1 Like

Thank you, Sir

1 Like

You’re welcome, happy coding!

1 Like

Hi Michael, im beginning to start learning eto. I see this working in rhino 8, any chance you could help with getting this working in rhino 7? Currently I get this message

Message: Cannot change AllowsTransparency after a Window has been shown or WindowInteropHelper.EnsureHandle has been called.

Traceback:
  line 38, in MyFormStyler, "C:\Users\thoma\Downloads\SampleTransparentForm_CG (1).py"
  line 54, in DoSomething, "C:\Users\thoma\Downloads\SampleTransparentForm_CG (1).py"
  line 57, in <module>, "C:\Users\thoma\Downloads\SampleTransparentForm_CG (1).py"

Hi @Tommy804,

This means you are trying to do something to your window or panel after setting the style. I need to see your code where you are trying to apply the form styler

Im using the code from the your above post from Clement.

#! python2

import Rhino
import scriptcontext
import System

import Eto

class SampleTransparentEtoForm(Eto.Forms.Form):
    def __init__(self):
        
        self.WindowStyle = Eto.Forms.WindowStyle.None
        
        self.Styles.Add[Eto.Forms.Panel]("transparent", self.MyFormStyler)
        self.Style = "transparent"
        
        self.AutoSize = False
        self.Resizable = True
        self.TopMost = True
        self.ShowActivated = False
        self.Size = Eto.Drawing.Size(300,300)
        self.Padding = Eto.Drawing.Padding(80)
        self.MovableByWindowBackground = True
        self.Location = Eto.Drawing.Point(100,100)
        
        self.Button = Eto.Forms.Button(Text = "Hello World")
        self.Button.Click += self.OnButtonClick
        
        self.layout = Eto.Forms.DynamicLayout()
        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.0
            window.Background = brush
        else:
            color = window.BackgroundColor
            window.BackgroundColor = color.FromRgba(0,0,0,25)

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

def DoSomething():
    
    form = SampleTransparentEtoForm()
    form.Owner = Rhino.UI.RhinoEtoApp.MainWindow
    form.Show()

if __name__=="__main__":
    DoSomething()

Sorry @Tommy804 ,

It has something to do with the order of trying to apply the styling and the Eto.Form.Form window already being created first.

I tried to debug and fix in R7 but no luck and I don’t develop in R7 so I’m not familiar enough with the nuances between R7 and 8 regarding Eto.Forms.

Given that the code works in both Python2 and Python3 in R8… I’m guessing its not a python issue but something with the order of execution or form handling in R7 Eto…

@clement and @curtisw do either of you happen to know why applying the style code to the window works fine in R8 but not R7?

Hi @all, does below work any better ? (I’ve only tested V7 & V8 Windows)

TransparentForm_V2.py (1.7 KB)

_
c.

2 Likes

Thank you Clement, that is working in v7 for me.

Hey,

Just as an aside, I was able to get window transparency built-in to Eto recently, so you’ll be able to simply set the Window.BackgroundColor to a semi or fully transparent color in Rhino 8.20 or later.

Cheers!
Curtis.

2 Likes

Not an aside, that’s the main feature :laughing: awesome @curtisw, thank you! That’s going to help a lot of people

1 Like

Hi Clement, does this also work with dialogs. I tried set one up but couldn’t figure it out. I tried self.Opacity but that seems to also make the button transparent as well which I want to avoid.

HI @Tommy804, in Rhino 8 it works with Form or Dialog, Dialogs can be modal or semimodal. The button stays opaque as in my example. I hope with SR20 this all gets easier…

_
c.

Could you let me know if im missing or messing up something here. Im currently run on v7.
TransparentDialog.py (1.7 KB)

Hi @Tommy804, as i wrote above, my example only works with Form in Rhino 7. If you want to use Dialog you’ll need to use Rhino 8.

_
c.

1 Like

Sorry I miss read what you had said, I see that now