用鼠标添加图片框单击WinForm

时间:2013-02-01 10:53:42

标签: c# winforms picturebox

目前我正在尝试创建一个黑色图像框,当我按下鼠标左键时出现。但是,当我点击时,没有任何反应。

有人可以看看我做错了吗?

在我的Image类中:

 PictureBox _pictureBoxTag = new PictureBox();

    private List<PictureBox> _displayedImage = new List<PictureBox>();

    public void AddPictureBox()
    {
        try
        {
            PictureBox _picBox = new PictureBox();
            _picBox.Size = new Size(100, 100);
            _picBox.SizeMode = PictureBoxSizeMode.StretchImage;
            _picBox.BackColor = Color.Black;
            _picBox.Location = new Point(100, 100);
            _displayedImage.Add(_picBox);



        }
        catch (Exception e)
        {
            Trace.WriteLine(e.Message);
        }
    }

然后在我的Form1.cs类

   HV_Image _testImage;
_testImage = new HV_Image();
  private void MouseDown( object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _testImage.AddPictureBox();
                Trace.WriteLine("Picture box added");
            }

            Trace.WriteLine("Mouse Click");
        }

我的想法是我的图像类应该包含一个图片框列表,这些图片框会填充所需的信息以创建一个图片框。例如大小,颜色,位置等。然后在我的Form1.cs类中,我只需调用该函数即可绘制。

如果我的方式很糟糕,或者不会工作,还有另一种方法可以做到这一点吗?

2 个答案:

答案 0 :(得分:0)

您不会将新PictureBox添加到表单的控件集合中。

您应该从AddPictureBox返回新创建的图片框,然后添加到表单的集合

public PictureBox AddPictureBox()
{
    try
    {
        PictureBox _picBox = new PictureBox();
        ......
        return _picBox;
    }
    catch (Exception e)
    {
        Trace.WriteLine(e.Message);
        return null;
    }
}


private void MouseDown( object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        PictureBox pic = _testImage.AddPictureBox();
        if(pic != null)
        {
            this.Controls.Add(pic);
            Trace.WriteLine("Picture box added");
    }
} 

或者,将表单实例传递给AddPictureBox方法

public void AddPictureBox(Form f)
{
    try
    {
        PictureBox _picBox = new PictureBox();
        ......
        f.Controls.Add(_picBox);
    }
    catch (Exception e)
    {
        Trace.WriteLine(e.Message);
    }
}

答案 1 :(得分:0)

您需要更改图像的x和y位置,否则每次都会将其添加到同一位置。

public void AddPictureBox(int x, int y)
    {
        try
        {
            PictureBox _picBox = new PictureBox();
            _picBox.Size = new Size(100, 100);
            _picBox.SizeMode = PictureBoxSizeMode.StretchImage;
            _picBox.BackColor = Color.Black;
            _picBox.Location = new Point(x, y);
            _displayedImage.Add(_picBox);

            return _picBox;

        }
        catch (Exception e)
        {
            Trace.WriteLine(e.Message); return null;
        }
    }


private void MouseDown( object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            PictureBox pic = _testImage.AddPictureBox(e.X, e.Y);
            if(pic != null)
            {
                this.Controls.Add(pic);
                Trace.WriteLine("Picture box added");
            }
        }

        Trace.WriteLine("Mouse Click");
    }