Delphi:如何诊断缓慢的 UI?
我有一个表单,您可以假装它的布局与 Windows 资源管理器类似:
- 左侧
- 拆分器
- 上的面板
客户端面板
<前><代码>+------------+#+------------------------+ | |#| | | |#| | | |#| | | |#| | |左|#|客户| | |#| | | |#| | | |#| | | |#| | | |#| | +------------+#+------------------------+ ^ | +----分配器
左侧面板和客户端区域面板都包含丰富的控件。
问题是使用分离器非常缓慢。我希望现代 2 GHz 计算机能够像人类推动鼠标一样快地重新显示表格。但事实绝对不是这样,大约需要200-300毫秒,表格才会完全重新调整。
该表单上有大约 100 个可视控件,没有代码或自定义控件。
我该如何追踪造成缓慢的原因?
i have a form, which you can pretend is laid out like Windows Explorer:
- panel on the left
- splitter
client panel
+------------+#+-----------------------+ | |#| | | |#| | | |#| | | |#| | | Left |#| Client | | |#| | | |#| | | |#| | | |#| | | |#| | +------------+#+-----------------------+ ^ | +----splitter
The the left and client area panels are each rich with controls.
The problem is that using the splitter is very sluggish. i would expect that a modern 2 GHz computer can re-display the form as fast as a human can push the mouse around. But that's definitely not the case, and it takes about 200-300 ms before the form is fully re-adjusted.
The form has about 100 visual controls on it, no code, or custom controls.
How do i go about tracing who's the cause of the sluggishness?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用分析器。 Eric Grange 的 Sampling Profiler 很好。 AutomatedQA 的 AQtime 非常出色。
这很可能是由于控件调整其布局和大小时重复调整大小和重新绘制造成的。大量嵌套或大量控件通常会导致问题。您可以通过重写 AlignControls 并在每次拆分器移动时仅调整一次对齐方式来避免这种情况,但这将涉及相当多的工作。
或者,TSplitter 有一个
ResizeStyle
属性,用于控制控件是立即移动还是在表单上对一行进行异或,并且控件仅在最后更新。视觉上不太好,但工作量少了很多。Use a profiler. Eric Grange's Sampling Profiler is good. AutomatedQA's AQtime is excellent.
This is more than likely due repeated resizes and repaints as the controls adjust their layouts and sizes. Lots of nesting or just lots of controls in general can cause problems. You can avoid that by overriding
AlignControls
and only adjusting the alignment once each time the splitter moves, but it will involve quite a bit of work.Alternatively, TSplitter has a
ResizeStyle
property that controls whether the controls move immediately or a line is XOR'd over the form, and the controls only update at the end. Not as nice visually, but a lot less work.我建议您在分割器移动时暂停 2 个面板的重绘,或者使用特定值(例如 50px),然后再次重绘。
I would suggest that you suspend the redrawing of the 2 panels, as long as the splitter is moving, or use a specific value, such as 50px, where you redraw again.
制作表单的测试版本,删除所有控件,只留下面板和拆分器。看看性能是否仍然低迷。
在调整大小事件中放置一些非阻塞消息(到文件、控制台调试器、gexperts 调试器)。你会得到如你所期望的几个,还是几十个?
左面板和分割器应对齐alLeft,与另一个alClient 对齐。
Make a test version of your form, delete all of the controls and leave yourself with just teh panels and splitter. See if performance is still sluggish.
Put some non-blocking messages (to a file, console debugger, gexperts debugger) in the resizing events. Do you get just a few, as you'd expect, or dozens?
Left panel and splitter should be aligned alLeft, and the other one alClient.
这主要是由于许多控件的重绘事件造成的
您可以尝试在表单中禁用背景绘制(减少绘制数量):
It is mostly due to repaint events of those many controls
You could try disabling background paint in your form (reduces # of paints):