I´m trying to make label with transparent background by hiding it, taking snapshot of parent and then drawing part of the parent as background of that label.
Here is my code
protected override void OnMove(EventArgs e)
{
base.OnMove(e);
Control highestParent = this;
this.Hide();
//Finds the topmost control, its panel that holds all other controls
while (!(highestParent is Workground))
{
if(highestParent.Parent == null)
{
this.Show();
return;
}
highestParent = highestParent.Parent;
}
Rectangle rec = new Rectangle(this.Location.X + 1, this.Location.Y + 1, Width, Height);
Bitmap bmp = new Bitmap(Width, Height);
highestParent.DrawToBitmap(bmp, rec);
this.BackgroundImage = bmp;
this.Show();
}
The problem I have encountered is that if I put it over eg. picturebox it does not set the background. I have actually tried this
protected override void OnMove(EventArgs e)
{
base.OnMove(e);
Control highestParent = this;
this.Hide();
//Finds the topmost control, its panel that holds all other controls
while (!(highestParent is Workground))
{
if(highestParent.Parent == null)
{
this.Show();
return;
}
highestParent = highestParent.Parent;
}
Bitmap bmp = new Bitmap(Width, Height);
highestParent.DrawToBitmap(bmp, this.ClientRectangle);
this.BackgroundImage = bmp;
this.Show();
}
and it worked but just showed the top left of parent control.
Thank you for your help :)
Update: There is no way to do this in Winforms. Solution is to switch to wpf.