设置重叠的两个PictureBox

时间:2013-03-04 12:01:54

标签: c# image picturebox overlapping

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

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

编辑: 图像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()
    {
    }
}

0 个答案:

没有答案