Custom Control with Transparent Background
- From: "Martijn Mulder" <i@m>
- Date: Fri, 15 Sep 2006 12:26:05 +0200
/*
How can I give my custom System.Windows.Forms.Control a transparent
background? In the small application below, I expect to see two partially
overlapping circles, a blue one and a red one, but only the blue circle
appears. When the background of the controls iis transparent, both circles
will show.
If this feature is not available or only through some kind of hack, what is
the reason that Microsoft does not support controls with transparent
background?
*/
using System.Drawing;
using System.Windows.Forms;
//class ColorCircleControl
class ColorCircleControl:Control
{
//data member brush
Brush brush;
//data members x,y
int x;
int y;
//constructor
public ColorCircleControl(Brush a,int b,int c)
{
brush=a;
x=b;
y=c;
SetStyle(ControlStyles.SupportsTransparentBackColor,true);
BackColor=Color.Transparent;
Dock=DockStyle.Fill;
Paint+=OnPaint;
}
//OnPaint
void OnPaint(object a,PaintEventArgs b)
{
b.Graphics.FillEllipse(brush,x,y,55,55);
}
}
//class MyForm
class MyForm:Form
{
//constructor
public MyForm()
{
Controls.Add(new ColorCircleControl(Brushes.Blue,34,55));
Controls.Add(new ColorCircleControl(Brushes.Red,55,55));
ClientSize=new System.Drawing.Size(233,144);
}
//Main
[System.STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new MyForm());
}
}
.
- Follow-Ups:
- Re: Custom Control with Transparent Background
- From: Bob Powell [MVP]
- Re: Custom Control with Transparent Background
- Prev by Date: Re: ReportViewer... "the handle is invalid"
- Next by Date: outlook 2003 left panel in c#
- Previous by thread: GDI objects count
- Next by thread: Re: Custom Control with Transparent Background
- Index(es):
Relevant Pages
|