I am looking for how I can draw a transparent control (being able to see behind of it) in a C# WinForms program.
I need something like the image below. As you can see, in the center of image there is a semi transparent component (really it works like a cursor), which is filling a circular sector for a round.
I am focused in get a semi transparent control, now, if I set a transparent background on my component (inherited from the standard control class), its background has the same color of parent background. Apparently is complicated get transparent controls in WinForms, but the image is taken on a WinForms program.
Do you have any idea? Is this possible?
Edit: Sorry if It was a duplicated question, I am going to paste my code below wich is taken of other programmer but I am including the suggestions in your links. (Only related code with the question is shown, no attributes, no properties, etc.)
public partial class LoadingCircle : Control
{
public LoadingCircle()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
//Added following StackOverflow
SetStyle(ControlStyles.Opaque, true);
this.BackColor = Color.Transparent;
m_Color = DefaultColor;
GenerateColorsPallet();
GetSpokesAngles();
GetControlCenterPoint();
m_Timer = new Timer();
m_Timer.Tick += new EventHandler(aTimer_Tick);
ActiveTimer();
this.Resize += new EventHandler(LoadingCircle_Resize);
}
void aTimer_Tick(object sender, EventArgs e)
{
m_ProgressValue = ++m_ProgressValue % m_NumberOfSpoke;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
if (m_NumberOfSpoke > 0)
{
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
int intPosition = m_ProgressValue;
for (int intCounter = 0; intCounter < m_NumberOfSpoke; intCounter++)
{
intPosition = intPosition % m_NumberOfSpoke;
DrawLine(e.Graphics,
GetCoordinate(m_CenterPoint, m_InnerCircleRadius, m_Angles[intPosition]),
GetCoordinate(m_CenterPoint, m_OuterCircleRadius, m_Angles[intPosition]),
m_Colors[intCounter], m_SpokeThickness);
intPosition++;
}
}
base.OnPaint(e);
}
//Added following StackOverflow
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20; //WS_EX_TRANSPARENT
return cp;
}
}
protected override void OnBackColorChanged(EventArgs e)
{
if (this.Parent != null) Parent.Invalidate(this.Bounds, true);
base.OnBackColorChanged(e);
}
protected override void OnParentBackColorChanged(EventArgs e)
{
this.Invalidate();
base.OnParentBackColorChanged(e);
}
//----- End
private void DrawLine(Graphics _objGraphics, PointF _objPointOne, PointF _objPointTwo,
Color _objColor, int _intLineThickness)
{
using(Pen objPen = new Pen(new SolidBrush(_objColor), _intLineThickness))
{
objPen.StartCap = LineCap.Round;
objPen.EndCap = LineCap.Round;
_objGraphics.DrawLine(objPen, _objPointOne, _objPointTwo);
}
}
private void ActiveTimer()
{
if (m_IsTimerActive)
m_Timer.Start();
else
{
m_Timer.Stop();
m_ProgressValue = 0;
}
GenerateColorsPallet();
Invalidate();
}
}
Edit 2: I added an image of the result, as you can see background of my control (LoadingCircle) is the same of its parent (form) but the button stays hidden.