views:

80

answers:

4

I have a form that contains a lot of runtime generated controls (image, button, panel,...), about 100 controls (I'm making a card matching game so there is a lot of controls in the form).

I place the generating code into the constructor and each time the app starts, it took about 3-5s to load all the controls completely.

If I bring another window on top and then back to my app, the controls will be redrawn again.

How can I prevent the controls from being redrawn? If you don't mind, please give me a simple example in C#.

Any help is appreciated!

+1  A: 

Hi,

I found this article that explains how to do this in .NET by calling the WIN API SET_MESSAGE function to set the WM_SETREDRAW flag for the control you do not want updated. Although you can stop certain controls from updating, are you sure you can't approach this issue by reducing the number of controls on the page? 100 Controls seems like a lot and may be an indication that you need to have multiple views.

Enjoy!

Doug
Thanks for your fast reply but the problem is still here.Honestly, I'm making a card matching game so there is a lot of controls in the form (each card is a panel with a picturebox inside). Could you suggest me a solution?
NVA
My suggestion would be similar to James Westgate's suggestion of using a graphics object to draw your cards on the screen. There might be some benefit in reading up on this pattern as well http://en.wikipedia.org/wiki/Flyweight_pattern
Doug
A: 

You might want to consider using a single data table control. A ListView (or something like ObjectListView) may be a good option.

If your data isn't really a list/table, you should split the controls into separate tab pages, which would improve both performance and usability.

Justin
Thanks for your reply ^^
NVA
+1  A: 

My suggestion is to use the form as a drawing surface and draw your card bitmaps directly onto the form. Its not hard to do.

You can add a handler to the form Paint event which will give you parameters with a Graphics object. Use graphics.DrawImageUnscaled to draw each card at the location you want.

This will make the app much much faster.

James Westgate
Thanks for your reply but if I use Graphics object to draw the card, how can I simulate the hover effect?
NVA
Keep track of the mouse which will tell you if you are over or clicking on a card, then call your paint code again to draw all the cards, including the hover card.
James Westgate
+1  A: 

Preventing a control from redrawing is fairly pointless. You'll get a hole where a control was supposed to appear, your user won't have any use for that hole.

It redraws slowly simply because you have too many controls. You can only get it to redraw faster by using less controls. Or by using controls that can display multiple items in one window, like ListBox, ListView, TreeView, DataGridView.

Note that your specific issue is fixed in Vista and Windows 7. The Aero theme uses off-screen buffering for windows. Which means that windows don't need to repaint themselves anymore when they are obscured by another window. You will however still get slow redraws when the user minimizes the window and restores it.

Hans Passant