Transparency

Have you ever wanted to create your own transparent user controls? Ones that have real transparency instead of just a single crummy masked color.

Well today kiddies we are going to take a look at doing this in .net code. I have done this in vb.net however it is trivial to convert to c# if you need to.

Prep work: Create a new class and have it inherit usercontrol. Of course you can a different class if you want as long as it can be added to a form and given a region.

In our new class create a Sub New and set up the following base styles:

Me.SetStyle(ControlStyles.Opaque, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
MyBase.BackColor = Color.Transparent

Note depending on how you want to draw your control you might want to set ControlStyles.AllPaintingInWmPaint to True

Then override the CreateParams property:

Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
    Get
      Dim cp As CreateParams = MyBase.CreateParams
      cp.ExStyle = cp.ExStyle Or &H20  ' Turn on WS_EX_TRANSPARENT
      Return cp
    End Get
  End Property

Next step override OnPaint and then get to work with your new fancy transparent user control. To get a feel of how it works you can just write something simple.

e.graphics.clear(color.fromargb(50,250,100,250))

Compile and then add the control to your form and move it around on top of buttons. Or instead of inheriting usercontrol inherit panel and then add some controls to it.

Advanced Note

If you start layering multiple transparent controls you will want to set up your own region clipping in the onpaint events and invalidate your controls parent with your controls specific rectangle to paint. This will avoid multiplication of transparencies with successive refreshes.

Cradle of Inception

For a long time now this website has been on life support. Content wasn’t posted and the reasons to visit were emotional. Like an aging parent in an assisted living center, cared about only on holidays or when really bored.

T

hat all changed though at the beginning of last month. With my long time service provider kicking me out on the street a new host had to be found. The chance for fresh beginnings was ripe. The choice to not copy over all of the previous content was a difficult one. People still download a lot of things here, regularly, amazing as that seems. Luckily the framework for this new site had been residing on my laptop for almost a year.

Virtually nothing of the old site still remains. I’m sorry if you were looking for something I posted in the 90’s. If it is that important to you drop me a line and I will try to help you. New content should start flowing again soon, giving a reason for my sites existence, and for you a reason to return.