What is the difference between CreateGraphics and a Paint event's Graphics object?

我与影子孤独终老i 提交于 2019-12-02 23:49:43

问题


Can someone explain the difference between the Graphics object that is passed as pevent.Graphics and the one that is returned by a call to this.CreateGraphics()?


回答1:


Whenever a Paint event is raised, you are given a Graphics object to draw into. This is passed as pevent.Graphics. Drawing into this Graphics object is how you paint the element.

CreateGraphics should basically never be used. It creates a new Graphics object on-the-fly from a window handle. You can draw into the Graphics object it returns, but anything you draw into it will be obliterated the next time that a Paint event is raised.

The only time you might want to use CreateGraphics is for special effects, like showing real-time feedback during a drag. You want that to be erased the next time that the element is repainted, so you go ahead and use CreateGraphics to get a temporary canvas to draw onto while the drag event is in progress.

You will never use CreateGraphics inside of a Paint event handler method. There is no point—you are given a Graphics object to draw into already!



来源:https://stackoverflow.com/questions/25301743/what-is-the-difference-between-creategraphics-and-a-paint-events-graphics-objec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!