【问题标题】:Painting to panel after .Update() call.Update() 调用后绘制到面板
【发布时间】:2015-04-23 09:36:22
【问题描述】:

在调用 panel.Update(); 后尝试立即绘制到面板时遇到问题; 代码如下:

    public void AddAndDraw(double X, double Y){
        AddPoint (X, Y);
        bool invalidated = false;

        if (X > _master.xMaxRange) {
            _master.Update ();
            invalidated = true;
        }
        if (Y > _master.yMaxRange) {
            _master.Update ();
            invalidated = true;
        }

        if (!invalidated) {
            _master.UpdateGraph (this);
        } else {
            _master.PaintContent ();
        }
    }

运行此问题时,我只看到已清除的面板,而不是我尝试在 .PaintContent() 方法中绘制的内容。我已经尝试在面板上使用 .Invalidate() 和 .Refresh() 而不是 .Update()

关于如何解决此问题的任何建议?

【问题讨论】:

  • 我看不到你在哪里画,但它真的应该在 Paint 事件中! (在从那里调用的某个函数中传递出 e.Graphics 对象!) - 我坚持这个规则,一切都会奏效。您需要在某处存储绘图参数! - 另请注意,Update 仅刷新具有可更改内容的控件,例如 ListBox 等。要刷新 Panel,您调用 panel.Invalidate()。
  • 感谢您的快速回复。问题是我的面板由三个“层”组成。我订阅了一个方法,它将我的图表的轴绘制到绘制事件。然后我必须分开绘制整个图形的方法,称为 PaintContent 和一个更新单个点并重新绘制整个图形的方法,如果该点超出 x 轴或 y 轴的范围。我希望能够单独调用这些。到目前为止,当通过按钮单击调用时,它与 PaintContent 一起工作,但在上面的代码中不起作用。 UpdateGraph 方法在这里可以正常工作。
  • 那么你需要标志或其他类型的指示器来指示 Paint 事件在被调用时应该绘制的内容。如果您想要持久绘图,这是无法避免的,除非您使用位图。尝试最小化/最大化表单,你就会明白我的意思! (如果绘图的某些部分没有太大变化,位图可能是一个选项。顺便说一句,PictureBox 有三层:2 个图像和您可以在其上绘制的控制表面。您可以将轴绘制到背景图像中,绘制到图像中并在绘制事件中绘制单点..)

标签: c# panel paint


【解决方案1】:

看来您的情况需要PictureBox

PBs 在这里很有趣,因为它们可以显示 三层 层:

  • 他们有一个BackgroundImage 属性
  • 他们有一个Image 属性
  • 与大多数控件一样,它们都有一个可以在 Paint 事件中绘制的表面

由于您需要一个固定的轴,并且图表不会一直在变化,并且您想要经常更新的点PB 似乎适合您!

根据需要调用函数,当点发生变化时,请在您的PictureBox 上调用Invalidate()

Bitmap GraphBackground = null;
Bitmap GraphImage = null;
Point aPoint = Point.Empty;

private void Form1_Load(object sender, EventArgs e)
{
    PictureBox Graph = pictureBox1;  // short reference, optional

    GraphBackground = new Bitmap(Graph.ClientSize.Width, Graph.ClientSize.Height);
    GraphImage = new Bitmap(Graph.ClientSize.Width, Graph.ClientSize.Height);

    // intial set up, repeat as needed!
    Graph.BackgroundImage = DrawBackground(GraphBackground);
    Graph.Image = DrawGraph(GraphImage);
}

Bitmap DrawBackground(Bitmap bmp)
{
    using (Graphics G = Graphics.FromImage(bmp) )
    {
        // my little axis code..
        Point c = new Point(bmp.Width / 2, bmp.Height / 2);
        G.DrawLine(Pens.DarkSlateGray, 0, c.Y, bmp.Width, c.Y);
        G.DrawLine(Pens.DarkSlateGray, c.X, 0, c.X, bmp.Height);
        G.DrawString("0", Font, Brushes.Black, c);
    }
    return bmp;
}

Bitmap DrawGraph(Bitmap bmp)
{
    using (Graphics G = Graphics.FromImage(bmp))
    {
        // do your drawing here

    }

    return bmp;
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    // make it as fat as you like ;-)
    e.Graphics.FillEllipse(Brushes.Red, aPoint.X - 3, aPoint.Y - 3, 7, 7);
}

【讨论】:

  • 非常感谢您的回答,非常有用!添加了我自己的绘图代码后,它现在似乎可以正常工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多