且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

需要帮助C#中的绘图程序

更新时间:2023-02-08 11:18:00

不,这不是因为刷新。你只是不知道基本的想法。现在,我会告诉你这个想法,你会很容易解决它。原则上, Graphics 实例上的绘图不存储在任何地方。您必须在请求时进行渲染,这在事件的处理程序中完成 System.Windows.Forms.Control.Paint 或重写虚拟方法 System.Windows.Forms.Control.OnPaint 。在这些处理程序中,使用通过 CreateGraphics 创建的 Graphics 的实例是没用的(即使你可以在某些中使用它)例如,在使用手写笔绘图时,但稍后您需要将绘图结果放入数据中,以便能够重新渲染它。相反,你应该使用在参数参数中传递给你的处理程序的那个。



请看我过去对这个主题的回答:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ],

mdi子表单之间的绘制线 [ ^ ]。



祝你好运,

-SA
No, this is not "because of Refresh". You just don't know the basic idea. Now, I'll tell you the idea and you will easily fix it. The drawing on a Graphics instance is not stored anywhere in principle. You have to do rendering whenever it is requested, which is done in a handler of the event System.Windows.Forms.Control.Paint or overridden virtual method System.Windows.Forms.Control.OnPaint. In these handler, it's useless to use the instance of Graphics created via CreateGraphics (even though you can use it in some cases, for example at the moment of drawing with the stylus, but later you need to put the result of drawing in data, to be able to re-render it). Instead, you should use the one passed to your handler in the event arguments parameter.

Please see my past answers on this topic:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

Good luck,
—SA