What is the difference between CreateGraphics and

2019-03-07 07:34发布

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条回答
时光不老,我们不散
2楼-- · 2019-03-07 07:56

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!

查看更多
登录 后发表回答