Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I suspend painting a form until all its controls are initialized?

There is not currently a way to do this built into the framework, but WM_SETREDRAW will do what you're looking for. It can't be called recursively, so here's code for a property you can add to your form to handle it. A VB sample is also available.

[ DllImport( "user32" ) ] 
private static extern bool SendMessage( IntPtr hWnd, int msg,
  int wParam, int lParam ); 
private const int WM_SETREDRAW = 0xB; 

int paintFrozen; 

private bool FreezePainting 
{ 
  get { return paintFrozen > 0; } 
  set 
  { 
    if ( value && IsHandleCreated && Visible ) 
      if ( 0 == paintFrozen++ ) 
        SendMessage( Handle, WM_SETREDRAW, 0, 0 ); 

    if ( !value ) 
    { 
      if ( paintFrozen == 0 ) return; 
      if ( 0 == --paintFrozen ) 
      { 
        SendMessage( Handle, WM_SETREDRAW, 1, 0 ); 
        Invalidate( true ); 
      } 
    } 
  } 
}

Shawn Burke, Microsoft



Page view counter