2015-04-23 57 views
0

在调用panel.Update()后立即绘制到面板时遇到问题。 下面的代码:在.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()

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

+0

我看不出你在哪里画,但它确实应该是__only__在Paint事件中! (__Or__在某个函数中从那里调用,__将e.Graphics__对象抛出!) - 我坚持这个规则,事情就会起作用。您将需要__存储绘图参数___某处! - 还要注意,Update只刷新具有可更改内容的控件,如ListBox等。要刷新Panel,请调用panel.Invalidate()。 – TaW

+0

感谢您的快速回复。 问题是我的面板由三个“层”组成。我订阅了一个方法,它将我的图的轴绘制到绘画事件中。然后,我必须分离方法来绘制一个名为PaintContent的整个图形,并且如果该点超出了x轴或y轴的范围,则会更新单个点并重新绘制整个图形。我希望能够分开打电话给他们。 到目前为止,它在从按钮点击调用PaintContent时起作用,但在上面的代码中不起作用。 UpdateGraph方法在这里工作正常。 –

+0

然后,您需要标志或Paint事件在调用时应绘制的其他类型的指示符。如果你想持久性绘图,这是无法避免的,除非你使用位图。尝试最小化/最大化表单,你明白我的意思! (如果绘图的某些部分没有改变很多Bitmaps可能是一个选项,顺便说一句,一个PictureBox有三个层次:2个图像和你可以绘制的控制表面。你可以将轴绘制到背景图像中,绘制成图像并在绘画事件中绘制单点..) – TaW

回答

0

看来你的情况需要PictureBox

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

  • 他们有一个BackgroundImage财产
  • 他们有一个Image财产
  • 而作为最控件他们有一个表面你可以在Paint事件中借鉴

由于您需要一个固定轴,并且图形不会一直在改变,并且您想要更新的点经常是PB似乎是为您而做的!

根据需要调用函数,当点改变时请致电Invalidate()您的PictureBox

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); 
} 
+0

非常感谢您的回答,它非常有用!它似乎现在工作得很好,我自己添加了绘图代码。 –