开发者

Prevent controls from being redrawn in C#?

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 eac开发者_Go百科h 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!


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!


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜