0

目前,我正在尝试创建一个黑色图像框,当我按下鼠标左键时会出现该框。但是,当我单击时,什么也没有发生。

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

在我的图像类中:

 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 类中,我只需调用该函数,它就会绘制。

如果我的方法很糟糕,或者不起作用,还有其他方法可以做到这一点吗?

4

2 回答 2

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);
    }
}
于 2013-02-01T10:59:01.763 回答
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");
    }
于 2013-02-01T11:02:36.377 回答