Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a drawing application. The application is based on the .NET core framework and I happen to have a panel on my form where I draw several shapes such as line segments, circles, rectangles and polygons. I have noticed quite a bizarre behavior when I minimize the form, all my drawings disappear on maximizing again. How can I store the state of the panel in some variable and restore the state of the panel when the windows is maximized again?

What I have tried:

I have the code snippet below that listens when the form size is changed and should store the state of the panel and restore on maximize, how to do this is the problem
Posted
Updated 31-Mar-23 6:14am
Comments
0x01AA 31-Mar-23 11:46am    
My guess is: If you draw your shapes etc. in Panel.Paint Event you should be fine.
Tim the Gamer 31-Mar-23 11:49am    
My code does not use the paint event handler method of the panel to draw components on it. I listen on the click events the user performs on the panel such as holding down mouse and moving it around or clicking two points and then checking the line button. Will the paint event let me choose a specific shape then call it to draw that shape?
pdoxtader 31-Mar-23 12:12pm    
The paint event is the standard place to do your graphics / drawing in winforms. When I do that kind of thing, it goes in the paint event. You'll trigger updates to your panel using .Refresh() or Invalidate(), depending. You may need to create some drawing operation objects and add them to a list that gets enumerated and executed during paint.
[no name] 31-Mar-23 12:04pm    
WPF, UWP, and Windows Forms can maintain visual "state" (using "their" Windows and Pages). If you don't use their facilities, you have to "repaint" yourself. Or, you're accidently clearing your own work through some event.
Tim the Gamer 31-Mar-23 12:15pm    
@Gerry Schimtz, no I have minimized the form several times and the panel comes back with all the drawings gone. That is why I needed to learn if there is a way to store the panel state on minimize and restore on maximize unless we can invoke the Panel Paint event handler from the other mouse click event handler where I am listening for clicks and then check the tag and draw the appropriate shape, I also get the compile time error that cannot convert EventArgs to MouseEventArgs

1 solution

Unless you draw on a persistent surface (a bitmap for example) or store the actions that you do draw, all such drawings on your panel are transitory - they will be lost the next time a Paint event is triggered for all of part of your panel.
And that includes minimize, maximize, resize, or the panel being partially or totally covered by a different control or other window.

For editable persistent objects, you need to store the "draw line from (x1, Y1) to (X2, Y2)" and suchlike in a collection, and handle the Paint event to iterate that collection and draw from there. When the user adds a new instruction, you add it to the collection and call Invalidate on the Panel which causes a Paint event to be triggered.
 
Share this answer
 
Comments
Tim the Gamer 31-Mar-23 12:25pm    
Drawing the actions is the next part of the project and I can use a Queue to store the actions so that the user can undo and redo painting steps. Is there a way I can call the Paint Event handler of the panel with a tag specifying the kind of shape to be drawn
OriginalGriff 31-Mar-23 12:31pm    
The kind of shape is what you store in the Queue ... create an abstract DrawItem class, and derive your various items from that, implementing an abstract Draw method that takes a Graphics context.
That way, the Paint handler just iterates the collection and the items draw themselves.
Tim the Gamer 31-Mar-23 12:34pm    
Okay thanks I will write the class and post a new comment in case I need any issue clarified. You also mentioned using a bitmap, I can actually save the drawings on the panel as a bitmap and redraw them on the panel when the form is maximized again.
OriginalGriff 31-Mar-23 12:51pm    
You could, but if you are planning on allowing edits later, remember that you can't "undo" drawings on a bitmap - just clear and restart.
Tim the Gamer 31-Mar-23 12:59pm    
That's the problem with using a bitmap, Undo won't work which can be super annoying. Let me implement the abstract classes and methods which would need inheritance for the various shapes, should I require the graphics context from the constructor or the method?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900