Sign in to follow this  
Hot_Tamalle

[.net] Drawing in C# using GDI+

Recommended Posts

Hi, I've made a tile based Level Editor using C# and GDI+. It works fine until I minimize the program (or make it go in the background by focusing on some other application). On refocusing or restoring it all the tiles disappear. They disappear in the sense that when the level is written to a file the tiles show up there but they just don't show on the screen after restoring. I do not know why. Please advise.... Thank You.

Share this post


Link to post
Share on other sites
Does the same problem occur if you move another window in front of the editor window then bring the editor window back to the top? Sounds like you're not handling your tile drawing correctly. You normally would handle this in the Paint event of the window or control you're drawing on.

Share this post


Link to post
Share on other sites
The Windows window manager does not keep track of the contents of a window. Instead it expects you to be able to repaint it whenever necessary (such as when your window is uncovered by another window, when you restore it etc). If a repaint is necessary, Windows will send the WM_PAINT message to the window, which will be translated by Windows Forms into the Paint event on the control/form in question.

In short: Handle all your custom painting in the Paint event, like Machaira said.

Share this post


Link to post
Share on other sites
Also, you don't necessarily have to (or want to) repaint the entire form. The 2nd parameter of the Paint event is an object with a member that tells you what part of the form needs repainted.

Share this post


Link to post
Share on other sites
Hi,

The drawing is taking place in a mouse click event. Whenever the mouse is clicked in the pictureBox, a tile is drawn at that position [X,Y]. If I handle all the drawing in the Paint event then how will I draw the tile at a mouse click event?

The drawing does go away when another window comes in front of the editor window and then back. One more thing, could you please clarify the 2d parameter that you mentioned.

Thankyou

Share this post


Link to post
Share on other sites
You need to store a list of all the tiles that the user has added, and in the Paint handler you should paint them all. (Okay, so if it gets slow you should just paint those which are in the invalidated region, but don't worry about that to start with.)

Share this post


Link to post
Share on other sites
If you want to do repainting after clicking, you can call this.Invalidate(), which will cause the Paint to be called on all controls.

I'm not sure how do you it right now, but you can do this:
Draw everything onto a Bitmap object. In the Paint() event, you only draw the bitmap, which is significantly faster than several small paint operations(And, while you use the bitmap object, you can only draw what has changed, as you probably do now in the click handler).

Toolmaker

Share this post


Link to post
Share on other sites
I've managed to fix the problem by re-drawing the pictureBox with the updated tiles after Windows calls the Paint Event on the form. A Loop and an Array go tile by tile and update the ones specificed in the array [ keeps track of all the changed tiles with X and Y values]. It's slow but for now it works.

Thanks for the help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this