2

我在创建两个重叠的PictureBoxes 时遇到问题。我像每个人告诉我的那样编写代码,但我有一些问题:

第一:背景是黑色的(为什么?) 第二:为什么树会出现两次?

编辑:图片http://i48.tinypic.com/2llnfrq.png(堆栈溢出有问题)

这是用于测试的示例代码。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.AllowTransparency = true;

        MyPictureBox pb1 = new MyPictureBox();
        MyPictureBox pb2 = new MyPictureBox();

        pb1.Size = new Size(64, 64);
        pb1.Location = new Point(20, 20);
        pb1.BackColor = Color.Transparent;

        //pb1.SizeMode = PictureBoxSizeMode.Zoom;

        pb2.Size = new Size(64, 64);
        pb2.Location = new Point(52, 20);
        pb2.BackColor = Color.Transparent;

        pb1.Image = Image.FromFile("D:\\...\\Graphics\\Grounds\\woda1.png");
        pb2.Image = Image.FromFile("D:\\...\\Graphics\\Objects\\tree1.png");

        panel1.Controls.Add(pb2);
        panel1.Controls.Add(pb1);
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

    }
}


class MyPanel : Panel
    {
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;
                return cp;
            }
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            // Do Nothing
        }

        public MyPanel()
        {

        }
    }

class MyPictureBox : PictureBox
{
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20;
            return cp;
        }
    }
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // Do Nothing
    }

    public MyPictureBox()
    {
    }
}
4

0 回答 0