Make Picture boxes transparent, each overlapping the other with a corner?

前端 未结 1 1681
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 01:17

TL;DR: Look at the picture below

So I\'m trying to make a little picture, and I and people around me are kinda out of ideas.

I have a table

相关标签:
1条回答
  • 2020-12-02 02:04

    Transparency in winforms is kind of misleading, since it's not really transparency.
    Winforms controls mimic transparency by painting the part of their parent control they would hide instead of their own background.
    However, they will not paint the other controls that might be partially covered by them.
    This is the reason your top most picture boxes hides your big picture box.

    You can overcome this by creating a custom control that inherits from PictureBox and override its OnPaintBackground method (taken, with slight adjustments, from this code project article):

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        base.OnPaintBackground(e);
        Graphics g = e.Graphics;
    
        if (this.Parent != null)
        {
            var index = Parent.Controls.GetChildIndex(this);
            for (var i = Parent.Controls.Count - 1; i > index; i--)
            {
                var c = Parent.Controls[i];
                if (c.Bounds.IntersectsWith(Bounds) && c.Visible)
                {
                    using (var bmp = new Bitmap(c.Width, c.Height, g))
                    {
                        c.DrawToBitmap(bmp, c.ClientRectangle);
                        g.TranslateTransform(c.Left - Left, c.Top - Top);
                        g.DrawImageUnscaled(bmp, Point.Empty);
                        g.TranslateTransform(Left - c.Left, Top - c.Top);
                    }
                }
            }
        }
    }
    

    Microsoft have published a Knowledge base article to solve this problem a long time ago, however it's a bit out-dated and it's code sample is in VB.Net.

    Another option is to paint the images yourself, without picture boxes to hold them, by using Graphics.DrawImage method.
    The best place to do it is probably in the OnPaint method of the form.

    0 讨论(0)
提交回复
热议问题