0

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.

pajamac
  • 113
  • 13
  • `this.Hide() ... this.Show()` = flickering. In winforms drawing is `this.Invalidate()` + double buffer + paint logic in `OnPaint`. Another thing, `Label` = [`graphics.DrawString`](https://msdn.microsoft.com/en-us/library/76c5db29(v=vs.110).aspx). – Sinatr Oct 11 '17 at 15:25
  • Nope, it is not flickering. And I don't override onpaint becouse I dont alter painting logic. I only set backgroundimage. – pajamac Oct 11 '17 at 15:57
  • This kind of transparency is already implemented by the Control class. Simply call SetStyle(ControlStyles.SupportsTransparentBackColor, true) in the constructor and set BackColor to Color.Transparent. But it has the same limitation as your code, it only draws the Parent as the background, not any other controls you overlap. The subject of [this KB article](https://support.microsoft.com/en-us/help/943454/winforms-how-to-create-a-control-transparent-to-other-controls). – Hans Passant Oct 11 '17 at 16:14
  • I know it does but as you said it has limitations. But by taking screenshot on my own it seems to do the job. Only need to fix the problem that it does not show specified rectangle. – pajamac Oct 11 '17 at 16:16

0 Answers0