Suspend or resume windows controls to be redraw


Recently, I was facing issue with third party Image Viewer to apply multiple operations before redrawing of images.
Third-party control was not allowing to suspend and resume painting of images inherently. After searching for various solutions, I found a solution that can be achieved using windows message WM_SETREDRAW from an application to Windows to allow changes in that control to be redrawn or to prevent changes in that control from being redrawn.

public class UIDrawController
{
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);

private const int WM_SETREDRAW = 11;

public static void SuspendDrawing(Control ctrl)
{
SendMessage(ctrl.Handle, WM_SETREDRAW, false, 0);
}

public static void ResumeDrawing(Control ctrl)
{
SendMessage(ctrl.Handle, WM_SETREDRAW, true, 0);
ctrl.Refresh();
}
}

Example Usage:


public ImageViewer : UserControl
{

public void LoadDocument(string filename)
{

UIDrawController.SuspendDrawing(this);

OpenImage(filename);
FitToWidth();
Rotate90();
ApplyImageFilters();

UIDrawController.ResumeDrawing(this);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: