M
Martijn Mulder
/*
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());
}
}
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());
}
}