【问题标题】:Adding a picture box with a mouse click WinForm鼠标点击添加图片框 WinForm
【发布时间】:2013-02-01 10:53:41
【问题描述】:

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

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

在我的图像类中:

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

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

【问题讨论】:

    标签: c# winforms picturebox


    【解决方案1】:

    您需要更改图像的 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");
        }
    

    【讨论】:

      【解决方案2】:

      您不会将新的 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);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多