在.Update()调用后绘制到面板

在调用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()

对于如何解决这个问题,有任何的建议吗?

看来你的情况需要一个PictureBox

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

  • 它们具有BackgroundImage属性
  • 他们有一个Image属性
  • 而且作为大多数控件,它们都有一个表面,您可以在Paint事件中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, cY, bmp.Width, cY); G.DrawLine(Pens.DarkSlateGray, cX, 0, cX, 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); }