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:
public class MyControl : Control
{
private TimeSpan paintTime = 250; // Time to paint, default=250ms
private TimeSpan resizeTime = 100; // Time to update layout, default=100ms
protected override void OnPaint(PaintEventArgs pe)
{
Stopwatch sw = new Stopwatch();
sw.Start();
// Do your painting here, or call base.OnPaint
sw.Stop();
paintTime = sw.Elapsed;
}
protected override void OnResize(EventArgs e)
{
// The "Stop" is not redundant - it will force the timer to "reset"
// if it is already running.
resizeTimer.Stop();
base.OnResize(e);
resizeTimer.Interval =
(int)(paintTime.TotalMilliseconds + resizeTime.TotalMilliseconds);
resizeTimer.Start();
}
private void UpdateSize()
{
Stopwatch sw = new Stopwatch();
sw.Start();
// Resizing code goes here
sw.Stop();
resizeTime = sw.Elapsed;
}
private void resizeTimer_Tick(object sender, EventArgs e)
{
resizeTimer.Stop();
UpdateSize();
}
}
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-named EnableFullPaint
) 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.