Simon Simon - 2 months ago 23
C# Question

How do I suspend painting for a control and its children?

I have a control which I have to make large modifications to. I'd like to completely prevent it from redrawing while I do that - SuspendLayout and ResumeLayout aren't enough. How do I suspend painting for a control and its children?


At my previous job we struggled with getting our rich UI app to paint instantly and smoothly. We were using standard .Net controls, custom controls and devexpress controls.

After a lot of googling and reflector usage I came across the WM_SETREDRAW win32 message. This really stops controls drawing whilst you update them and can be applied, IIRC to the parent/containing panel.

This is a very very simple class demonstrating how to use this message:

class DrawingControl
    public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);

    private const int WM_SETREDRAW = 11; 

    public static void SuspendDrawing( Control parent )
        SendMessage(parent.Handle, WM_SETREDRAW, false, 0);

    public static void ResumeDrawing( Control parent )
        SendMessage(parent.Handle, WM_SETREDRAW, true, 0);

There are fuller discussions on this - google for C# and WM_SETREDRAW, e.g.

C# Jitter

Suspending Layouts

And to whom it may concern, this is similar example in VB:

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Integer, _
                                                                ByVal wMsg As Integer, _
                                                                ByVal wParam As Integer,
                                                                ByVal lParam As Integer) As Integer

Private Const WM_SETREDRAW As Integer = 11

' Extension methods for Control
Public Sub ResumeDrawing(ByVal Target As Control, ByVal Redraw As Boolean)
  SendMessage(Target.Handle, WM_SETREDRAW, 1, 0)
  If Redraw Then
  End If
End Sub

Public Sub SuspendDrawing(ByVal Target As Control)
  SendMessage(Target.Handle, WM_SETREDRAW, 0, 0)
End Sub

Public Sub ResumeDrawing(ByVal Target As Control)
  ResumeDrawing(Target, True)
End Sub