Winforms semi-transparent PNG over semi-transparent PNG

前端 未结 2 1907
梦毁少年i
梦毁少年i 2020-12-04 03:25

I think I must be missing something obvious, but I\'m unable to find this after several hours of searching. Is there no way to use a PictureBox or other control to contain

2条回答
  •  一个人的身影
    2020-12-04 04:06

    The PictureBox control supports transparency well, just set its BackColor property to Transparent. Which will make the pixels of its Parent visible as the background.

    The rub is that the designer won't let you make the 2nd picture box a child of the 1st one. All you need is a wee bit of code in the constructor to re-parent it. And give it a new Location since that is relative from the parent. Like this:

        public Form1() {
            InitializeComponent();
            pictureBox1.Controls.Add(pictureBox2);
            pictureBox2.Location = new Point(0, 0);
            pictureBox2.BackColor = Color.Transparent;
        }
    

    Don't hesitate to use OnPaint() btw.

提交回复
热议问题