2

我正在尝试开发一个“PictureBox”,它显示一个小圆圈“环绕”一个大圆圈。关键方面是这个小圆圈的位置(以及整体情况)可以根据我每秒进行的半昂贵的硬件调用而改变。我用“AngleValueTextBox”调用来说明。

研究有关该主题的几篇文章,这似乎是正确的方法:

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

    AngleValueTextBox_TextChanged(null, null); // Why doesn't this make the box appear on program start?
    PictureBox.Invalidate();  // Or this?
  }

  public class CircleCenterClass { public double X; public double Y; 
    public CircleCenterClass(double X, double Y) { this.X = X; this.Y = Y; }
  }

  CircleCenterClass GetCenterOfOrbitCircle(CircleCenterClass OrbitCenter, double OrbitRadius, double Angle)
  {
    CircleCenterClass Result = new CircleCenterClass(0, 0);
    Result.X = OrbitCenter.X + OrbitRadius * Math.Sin(Angle * (3.141592654 / 180));
    Result.Y = OrbitCenter.Y - OrbitRadius * Math.Cos(Angle * (3.141592654 / 180));
    return Result;
  }

  private void drawPictureBox(Graphics graphics, bool ownGraphics)
  {
    double Angle= 0; 
    // Normally this is an expensive hardware call which I don't want to make more than 
    // once a second.
    try {
      Double.TryParse(AngleValueTextBox.Text, out Angle);
    } catch (Exception e) {
      MessageBox.Show("Could not parse Angle " + 
                       AngleValueTextBox.Text);
    }

    using (Bitmap bitmap = new Bitmap(PictureBox.Width, PictureBox.Height))
    {
      using (Graphics gx = Graphics.FromImage(bitmap))
      {
        double OrbitedCircleRadius = 40;
        CircleCenterClass OrbitedCircleCenter = new CircleCenterClass(60, 60);
        double OrbitingCircleRadius = 7;
        Pen MyRedPen = new Pen(Color.Red, 2f);
        Brush MyBlackBrush = new SolidBrush(Color.Black);

        gx.DrawEllipse(MyRedPen, (Int32)(OrbitedCircleCenter.X - OrbitedCircleRadius),
                                        (Int32)(OrbitedCircleCenter.Y - OrbitedCircleRadius),
                                        (Int32)(2 * OrbitedCircleRadius),
                                        (Int32)(2 * OrbitedCircleRadius));

        CircleCenterClass CircleCenter = GetCenterOfOrbitCircle(
                            OrbitedCircleCenter, OrbitedCircleRadius,
                            Angle);

        gx.FillEllipse(MyBlackBrush, (Int32)(CircleCenter.X - OrbitingCircleRadius),
                                     (Int32)(CircleCenter.Y - OrbitingCircleRadius),
                                     (Int32)(2 * OrbitingCircleRadius),
                                     (Int32)(2 * OrbitingCircleRadius));

        // Draw Electronics box. 
        Graphics g = PictureBox.CreateGraphics();
        g.Clear(BackColor);
        g.DrawImage(bitmap, 0, 0);  
      }
    }
  }

  private void PictureBox_Paint(object sender, PaintEventArgs e)
  {
    drawPictureBox(e.Graphics,false);
  }

  private void AngleValueTextBox_TextChanged(object sender, EventArgs e)
  {
    drawPictureBox(PictureBox.CreateGraphics(), true);
  }
}

关键问题:

  1. 我最初无法显示图片(即当 GUI 启动时)。我尝试将事件添加到我的 Form 构造函数(如您所见)但没有运气。什么是“正确”的方式?

  2. 有没有更好的方法来绘制不断变化的图形?

4

3 回答 3

0

将 draw 方法调用添加到 Form 的Load事件中。

于 2013-04-10T14:07:50.397 回答
0
  1. 使用 CreateGraphics 很少是更新显示的正确方法。只需让您的绘画方法完成所有绘画并调用控件 Invalidate() 方法来更新屏幕。

  2. 确保绘制事件已连接(只是为了确保)。如果它没有在设计器中连接,请确保它在表单的构造函数中:

    PictureBox.Paint += PictureBox_Paint;

    另外,命名您的控件。PictureBox 的描述性不是很强。

  3. 任何“半昂贵的硬件调用”都应该在 BackgroundWorker 中进行,这样 UI 就不会被冻结。

  4. 如果只是不断尝试用新的绘图更新屏幕,请使用计时器。

  5. 您不需要在每次绘制调用时都创建一个新位图。要么使用给定的图形对象,要么使用仅在调整控件大小时创建的位图。

于 2013-04-10T14:16:47.577 回答
0

用调用的 PictureBox替换PictureBox控件Panel

或将图片框的Image属性设置为创建的位图

请注意,这不是一种特别有效的方法。

如果图像仅每秒更新一次,那么您可能仅在值更改时才绘制图像,将位图保留为表单上的私有属性,并且仅在绘画中复制位图(就像您所做的那样,但取出所有绘制代码,并且仅在值更改时才执行)

在此处输入图像描述

    Bitmap bitmap;
    public Form1()
    {
        InitializeComponent();
        bitmap = new Bitmap(PictureBox.Width, PictureBox.Height);
        AngleValueTextBox_TextChanged(null, null); // Why doesn't this make the box appear on program start?
        PictureBox.Invalidate();  // Or this?
    }

    public class CircleCenterClass
    {
        public double X; public double Y;
        public CircleCenterClass(double X, double Y) { this.X = X; this.Y = Y; }
    }

    CircleCenterClass GetCenterOfOrbitCircle(CircleCenterClass OrbitCenter, double OrbitRadius, double Angle)
    {
        CircleCenterClass Result = new CircleCenterClass(0, 0);
        Result.X = OrbitCenter.X + OrbitRadius * Math.Sin(Angle * (3.141592654 / 180));
        Result.Y = OrbitCenter.Y - OrbitRadius * Math.Cos(Angle * (3.141592654 / 180));
        return Result;
    }

    private void drawPictureBox(Graphics graphics, bool ownGraphics)
    {



    }

    private void DrawImage()
    {
        double Angle = 0;
        // Normally this is an expensive hardware call which I don't want to make more than 
        // once a second.
        try
        {
            Double.TryParse(AngleValueTextBox.Text, out Angle);
        }
        catch (Exception e)
        {
            MessageBox.Show("Could not parse Angle " +
                             AngleValueTextBox.Text);
        }

        using (Graphics gx = Graphics.FromImage(bitmap))
        {
            gx.Clear(BackColor);
            double OrbitedCircleRadius = 40;
            CircleCenterClass OrbitedCircleCenter = new CircleCenterClass(60, 60);
            double OrbitingCircleRadius = 7;
            Pen MyRedPen = new Pen(Color.Red, 2f);
            Brush MyBlackBrush = new SolidBrush(Color.Black);

            gx.DrawEllipse(MyRedPen, (Int32)(OrbitedCircleCenter.X - OrbitedCircleRadius),
                                            (Int32)(OrbitedCircleCenter.Y - OrbitedCircleRadius),
                                            (Int32)(2 * OrbitedCircleRadius),
                                            (Int32)(2 * OrbitedCircleRadius));

            CircleCenterClass CircleCenter = GetCenterOfOrbitCircle(
                                OrbitedCircleCenter, OrbitedCircleRadius,
                                Angle);

            gx.FillEllipse(MyBlackBrush, (Int32)(CircleCenter.X - OrbitingCircleRadius),
                                         (Int32)(CircleCenter.Y - OrbitingCircleRadius),
                                         (Int32)(2 * OrbitingCircleRadius),
                                         (Int32)(2 * OrbitingCircleRadius));
        }
    }

    private void PictureBox_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(bitmap, 0, 0);
    }

    private void AngleValueTextBox_TextChanged(object sender, EventArgs e)
    {
        DrawImage();
        PictureBox.Invalidate();
    }
于 2013-04-10T14:33:47.657 回答