面板图表将在表单最小化后进行,C#

我的表单中有一个名为Pan_Paint的面板,我的代码如下:

 Graphics graph = Pan_Paint.CreateGraphics(); graph.FillEllipse(new SolidBrush(Blue), 10, 10, 100, 100); 

当我缩小表格时,以及当我恢复它时,线条将消失。 或者当我按Tab键时,同样的事情会发生。 我该怎么做才能解决这个问题? 谢谢。

这是基本的。

您需要在一些数据结构中缓存所有绘图 ,并在Paint事件中绘制它们。 一次又一次,只要Windows需要恢复屏幕。

每当您向绘图队列添加一些绘图操作时,最初通过执行Panel.Invalidate()调用Paint事件。

没有办法除了:

将它们绘制 PictureBox图像中(而不仅仅是PictureBox!)..

这是使用PictureBox正确执行的代码,以及如何做错:

  // this will change the Image of a PictureBox, assuming it has one. // These changes are persistent: using (Graphics G = Graphics.FromImage(pictureBox1.Image)) { G.DrawEllipse(Pens.Red, new Rectangle(0, 0, 444, 444)); pictureBox1.Invalidate(); } // This is the wrong, non-persistent way to paint, no matter which control: //The changes go away whenever the Window is invalidated: using (Graphics G = pictureBox2.CreateGraphics() ) { G.DrawEllipse(Pens.Green, new Rectangle(0, 0, 444, 444)); } 

而是在Paint事件中创建一类绘制操作并循环遍历它们的列表!