windows forms - transparence question

  • Thread starter Thread starter Matthias S.
  • Start date Start date
M

Matthias S.

hi there,

i'm using .net framework 2.0 and i need a panel to be 50%
transparent/transluctant. i was desperately looking for an opacity
property with no luck. any ideas on how to roll your own?

thanks in advance!

matthias
--
 
Matthias S. said:
hi there,

i'm using .net framework 2.0 and i need a panel to be 50%
transparent/transluctant. i was desperately looking for an opacity
property with no luck. any ideas on how to roll your own?

thanks in advance!

matthias

Hi Matthias,

Assembling various code bits from the net the I've created a
TransparentPanel class that may work for you.

public class TransparentPanel : Panel
{
private int _alfa;

public int Alfa
{
get { return _alfa; }
set { _alfa = value; }
}

public TransparentPanel()
{
SetStyle(ControlStyles.Opaque, true);
}

protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.ExStyle |= 0x00000020;
return createParams;
}
}

protected override void OnPaint(PaintEventArgs e)
{
using (SolidBrush brush = new SolidBrush(Color.FromArgb(Alfa,
BackColor)))
{
e.Graphics.FillRectangle(brush, ClientRectangle);
}
}
}

It does not appear to like being tweaked in the designerwindow much, but
when adding it manually it does display the background in a faded manner.

public Form1()
{
InitializeComponent();

Controls.Add(new TextBox());

TransparentPanel p = new TransparentPanel();
p.Alfa = 64;
p.BackColor = Color.DarkOrange;
Controls.Add(p);
p.BringToFront();
}
 
Peter Duniho said:
[...]
It does not appear to like being tweaked in the designerwindow much, but
when adding it manually it does display the background in a faded manner.

Try placing it on top of something else. You'll find that while the
"background" is painted with the alpha, it's not actually transparent. :(

Unfortunately, I'm not aware of a way to do this using the existing Forms
namespace. Even with the BackColor property set to Transparent, it's not
really transparent. It just inherits the parent's BackColor. And Windows
clips underlying controls, making the assumption that your own control is
opaque, so even if you configure the control to be completely owner-drawn,
including the background, any underlying controls are never drawn
underneath your own.

It's possible that there's a way to do this using WPF. My understanding
is that WPF uses something more akin to Java's idea of "lightweight"
controls (i.e. controls that aren't individually tied to an OS window
handle object), and if so it wouldn't be constrained by how Windows
works. But I don't know enough about WPF to say how this would actually
be done.

Pete

It should, although it wouldn't render correctly unless I added it
programmatically. Putting it over the parent form would render the parent
and its controls in a faded manner, the color of the panel, using the
provided alfa level. However, partially covered controls on the parent form
would pop above the panel when focused so its uses are limited.
 
Back
Top