Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the opacity or transparency of a Panel in WinForms?

I was wondering how to change or modify the transparency of a Panel in C#, not the whole form, but the panel only.. I've seen many C# tutorials on Opacity, but its for the Form. im looking for how it could be possible with the Panel only. Thank You!

like image 354
Gian Santillan Avatar asked Dec 16 '10 17:12

Gian Santillan


People also ask

How do I make panels transparent in Windows Forms?

I first create a Windows Forms application using Visual Studio . NET and give project a name called TransparentPanel. After that I place a Panel control on the Form and set background color of the Panel to red. Now I set Form's TransparentKey property to the same color as Panel's background color - red in this case.

How to make transparent form in c#?

BackColor = Color. Lime; TransparencyKey = Color. Lime; This will make the form transparent.

When setting the Opacity of a form in code its value ranges from?

Edit: Opacity as a value ranges from 0 to 1 (which, as displayed in % means 0% to 100%).


2 Answers

For whoever is still looking for a totally transparent panel, I found a nice solution in this blog by William Smash who in turn has taken it from Tobias Hertkorn on his T# blog. I thought its worth posting it as an answer here.

C# code:

public class TransparentPanel : Panel {     protected override CreateParams CreateParams      {                     get {             CreateParams cp =  base.CreateParams;             cp.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT             return cp;             }     }     protected override void OnPaintBackground(PaintEventArgs e)      {         //base.OnPaintBackground(e);     } } 

VB.Net code:

Public Class TransparentPanel Inherits Panel     Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams         Get             Dim cp As CreateParams = MyBase.CreateParams             cp.ExStyle = cp.ExStyle Or &H20 ''#WS_EX_TRANSPARENT             Return cp         End Get     End Property     Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)     ''#MyBase.OnPaintBackground(e)     End Sub End Class 
like image 83
Abdusalam Ben Haj Avatar answered Sep 28 '22 02:09

Abdusalam Ben Haj


Yes, opacity can only work on top-level windows. It uses a hardware feature of the video adapter, that doesn't support child windows, like Panel. The only top-level Control derived class in Winforms is Form.

Several of the 'pure' Winform controls, the ones that do their own painting instead of letting a native Windows control do the job, do however support a transparent BackColor. Panel is one of them. It uses a trick, it asks the Parent to draw itself to produce the background pixels. One side-effect of this trick is that overlapping controls doesn't work, you only see the parent pixels, not the overlapped controls.

This sample form shows it at work:

public partial class Form1 : Form {     public Form1() {         InitializeComponent();         this.BackColor = Color.White;         panel1.BackColor = Color.FromArgb(25, Color.Black);     }     protected override void OnPaint(PaintEventArgs e) {         e.Graphics.DrawLine(Pens.Yellow, 0, 0, 100, 100);     } } 

If that's not good enough then you need to consider stacking forms on top of each other. Like this.

Notable perhaps is that this restriction is lifted in Windows 8. It no longer uses the video adapter overlay feature and DWM (aka Aero) cannot be turned off anymore. Which makes opacity/transparency on child windows easy to implement. Relying on this is of course future-music for a while to come. Windows 7 will be the next XP :)

like image 26
Hans Passant Avatar answered Sep 28 '22 03:09

Hans Passant