简体   繁体   English

防止在C#中重新绘制控件?

[英]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). 我有一个包含大量运行时生成的控件(图像,按钮,面板,...)的表单,大约100个控件(我正在制作卡片匹配游戏,因此表单中有很多控件)。

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. 我将生成代码放入构造函数中,每次应用程序启动时,大约需要3-5秒才能完全加载所有控件。

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#. 如果您不介意,请在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. 我发现这篇文章通过调用WIN API SET_MESSAGE函数来解释如何在.NET中执行此操作,以便为您不想更新的控件设置WM_SETREDRAW标志。 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. 100控件似乎很多,可能表明您需要有多个视图。

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. 您可以向表单Paint事件添加处理程序,该事件将为您提供带有Graphics对象的参数。 Use graphics.DrawImageUnscaled to draw each card at the location you want. 使用graphics.DrawImageUnscaled在您想要的位置绘制每张卡片。

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. 或者通过使用可以在一个窗口中显示多个项目的控件,如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. 请注意,您的特定问题已在Vista和Windows 7中修复.Aero主题使用窗口的屏幕外缓冲。 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. ListView(或像ObjectListView之类的东西)可能是个不错的选择。

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. 如果您的数据实际上不是列表/表,则应将控件拆分为单独的选项卡页,这样可以提高性能和可用性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM