且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Winforms 半透明 PNG 超过半透明 PNG

更新时间:2023-11-10 20:14:04

PictureBox 控件对透明度的支持很好,只需将其 BackColor 属性设置为 Transparent.这将使其 Parent 的像素作为背景可见.

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;
    }

顺便说一句,不要犹豫使用 OnPaint().

Don't hesitate to use OnPaint() btw.