Skip to content

Instantly share code, notes, and snippets.

@gerrited
Last active September 3, 2015 14:22
Show Gist options
  • Save gerrited/8971719 to your computer and use it in GitHub Desktop.
Save gerrited/8971719 to your computer and use it in GitHub Desktop.
Fix this flickering behaviour of old Win32 controls. The code is based on a discussion on StackOverflow http://stackoverflow.com/questions/487661/how-do-i-suspend-painting-for-a-control-and-its-children
public static class ControlExtensions
{
public static IDisposable GetRedrawSuspender(this Control cuntrol) { return new RedrawSuspender(cuntrol); }
}
public class RedrawSuspender : IDisposable
{
private Control _control;
internal RedrawSuspender(Control control)
{
if (control == null) return;
_control = control;
SuspendRedraw();
}
void IDisposable.Dispose()
{
if (_control == null) return;
ResumeRedraw();
_control = null;
}
private void SuspendRedraw()
{
if (_control == null) return;
var m = Message.Create(_control.Handle, WindowMessage.SetRedraw, IntPtr.Zero, IntPtr.Zero);
var window = NativeWindow.FromHandle(_control.Handle);
window.DefWndProc(ref m);
}
private void ResumeRedraw()
{
if (_control == null) return;
var wparam = new IntPtr(1);
var msgResumeUpdate = Message.Create(_control.Handle, WindowMessage.SetRedraw, wparam, IntPtr.Zero);
var window = NativeWindow.FromHandle(_control.Handle);
window.DefWndProc(ref msgResumeUpdate);
_control.Invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment