Re-painting graphics
-
I have a user control with a panel on it which I paint rectangles and text on using the following method - Graphics mg = this.CallsPanel.CreateGraphics(); When I minimize or switch back and forth between other application windows, I loose the graphics. I have been trying to figure out which event to wire a re-paint function to but am not having much luck. Or, is there a way to anchor the graphics that have been painted until I invalidate and update the panel?
-
I have a user control with a panel on it which I paint rectangles and text on using the following method - Graphics mg = this.CallsPanel.CreateGraphics(); When I minimize or switch back and forth between other application windows, I loose the graphics. I have been trying to figure out which event to wire a re-paint function to but am not having much luck. Or, is there a way to anchor the graphics that have been painted until I invalidate and update the panel?
tjschilling wrote:
Graphics mg = this.CallsPanel.CreateGraphics();
NEVER do this. There is no way to solve the problem you're now having. Draw in your paint event and call Invalidate to force it to occur.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
tjschilling wrote:
Graphics mg = this.CallsPanel.CreateGraphics();
NEVER do this. There is no way to solve the problem you're now having. Draw in your paint event and call Invalidate to force it to occur.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
I moved the calls to my re-draw to the paint event for the user control and all is well. Looks like you gave me what I needed. Thanks.
-
I moved the calls to my re-draw to the paint event for the user control and all is well. Looks like you gave me what I needed. Thanks.
Don't just do the re-draw in the Paint event, do all drawing there. Call the Invalidate method of the control when it needs to be redrawn. Make sure to use the Graphics object that is supplied in the EventArgs for the event, and not create your own. The supplied Graphics object is clipped to the area that should be redrawn, if you create a new one it won't be clipped, and you might draw on top of someone elses window.
--- b { font-weight: normal; }