如何使用 OnResize 事件检查调整大小是否仍在进行中?
我需要在调整大小后重新绘制控件,而不是在调整大小时重新绘制控件,因为重新绘制控件需要花费大量时间并且浪费大量资源。
我的控件继承 Control 类,并且重写 OnResize 事件。
PS: OnSizeChanged 完全相同
I need to redraw a control after it was resized and not while it is being resized because it takes a lot of time to redraw a control and it wastes a lot of resources.
My control inherits Control class and I'm overriding the OnResize event.
PS: OnSizeChanged is absolutely the same
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
让我提出这个问题的另一种观点:
问题不一定是“用户仍在调整控件的大小”。真正的问题是控件大小的调整比重绘所需的时间更频繁。
如果您将问题定义为吞吐量之一,那么解决起来就会更容易。事实上,Bebop 的答案是正确的,但我认为我们可以做得更好:
这里的想法是让控件主动分析自身;如果您在速度较慢的机器上运行它或者机器只是运行缓慢,那么这会减慢其重绘速度。如果您在最先进的硬件上运行它,那么它可能根本不需要跳过任何重绘。这实际上是一个相当简单的“自动跳帧”算法,您经常在设备模拟器中看到这种算法。
需要明确的是,我并不反对 nobugz 所提倡的方法;我选择这个的唯一原因是逻辑是完全独立的,而公开 Resizing 属性(或者可能更恰当地命名为 EnableFullPaint )取决于消费者了解并正确使用它 - 而且,它可以防止在调整大小的整个持续时间内进行任何重新绘制,这可能会导致应用程序“感觉”有问题 - 用户往往不会期望调整大小操作期间出现空白/无意义的屏幕。
这两种方法我都用过,而且都有效;哪一种最适合您取决于您的要求。我建议你尝试一下,看看它对你有多有效,如果它出现问题或者不是你想要的,那么就使用 nobugz 的答案。
Let me present an alternative view of this problem:
The issue is not necessarily that the "user is still resizing the control." The real issue is that the control is being resized more frequently than the time it takes to redraw.
If you define the problem as one of throughput, it becomes easier to solve. And in fact Bebop had the right idea with his answer, but I think we can do better:
The idea here is to have the control actively profile itself; if you run it on a slow machine or the machine is simply running sluggishly then this will slow down its redraw rate. If you're running it on bleeding-edge hardware then it may not have to skip any redraws at all. This is actually a fairly simple "auto frame skip" algorithm of the kind you often see in device emulators.
To be clear, I have nothing against the approach advocated by nobugz; the only reason I would choose this one instead is that the logic is completely self-contained, whereas exposing a
Resizing
property (or perhaps a more aptly-namedEnableFullPaint
) is depending on the consumer to know and use it properly - and also, it prevents any repainting for the entire duration of the resize, which may cause the app to "feel" buggy - users don't tend to expect a blank/nonsense screen during a resize operation.I've used both methods and they both work; which one is best for you depends on your requirements. I suggest you try this, see how well it works for you, and if it becomes problematic or isn't what you want then go with nobugz' answer.
一种解决方案是仅在父控件/窗体完成调整大小后才调整控件的大小。您可以使用“ResizeEnd”事件来执行此操作。
例如:
One solution can be resizeing your control only after the parent control/form finished resizeing. You can do this using the 'ResizeEnd` event.
For example:
当你收到 MouseDown 事件时怎么样:
YourControl.SuspendLayout();
然后在 MouseUp 事件中执行:
YourControl.ResumeLayout();
How about when you get the MouseDown event go:
YourControl.SuspendLayout();
and then in the MouseUp event go:
YourControl.ResumeLayout();
ResizeEnd 事件似乎是最好的主意,但作为替代方案,您可以使用一个计时器,在调整大小事件中重新启动计时器,并在计时器触发时重新绘制。只要计时器的时间相当短,比如 100 毫秒,那么您就不会太严重地注意到重绘延迟。
但另一个活动似乎是更好的选择......
ResizeEnd event seems the best idea, but as an alternative you could have a timer, which you restart in the resize event, and redraw when the timer fires. As long as the time of the timer is fairly short, say 100ms then you shouldn't notice the redraw delay too badly.
But the other event seems like a better option...