Use LockWindowUpdate to get flicker-free RichTextBox update

Very simple, but I had to dig around to find it so I might as well share it here. I do lots of updates to a RichTextBox while colorparsing some code, and I didn't like the flickering.

Since there is no BeginUpdate() or similar, I had to use Win32 api from user32.dll:

      [DllImport("user32.dll")]

      public static extern bool LockWindowUpdate(IntPtr hWndLock);

To use it just:

try

{

      LockWindowUpdate(richTextBox1.Handle);

      //change colors and stuff in the RichTextBox

}

finally

{

      LockWindowUpdate(IntPtr.Zero);

}

 

UPDATE: I got a tip fom Kevin Westhead to use SendMessage() with WM_SETREDRAW instead:

 

I think it's better to use SendMessage with WM_SETREDRAW to enable/disable drawing on a window since LockWindowUpdate can only be applied to one window. This means that you might not be able to lock your window if someone else has already locked a window, or someone else could unlock your window without you knowing.

So if you prefer this alternative, please do like this:

 

private const int WM_SETREDRAW      = 0x000B;

private const int WM_USER           = 0x400;

private const int EM_GETEVENTMASK   = (WM_USER + 59);

private const int EM_SETEVENTMASK   = (WM_USER + 69);

 

[DllImport("user32", CharSet = CharSet.Auto)]

private extern static IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);

 

IntPtr eventMask = IntPtr.Zero;

try

{

      // Stop redrawing:

      SendMessage(richTextBox1.Handle, WM_SETREDRAW, 0, IntPtr.Zero);

      // Stop sending of events:

      eventMask = SendMessage(richTextBox1.Handle, EM_GETEVENTMASK, 0, IntPtr.Zero);

 

      // change colors and stuff in the RichTextBox

}

finally

{

      // turn on events

      SendMessage(richTextBox1.Handle, EM_SETEVENTMASK, 0, eventMask);

      // turn on redrawing

      SendMessage(richTextBox1.Handle, WM_SETREDRAW, 1, IntPtr.Zero);

}

 

Thanks Kevin!

Published Friday, March 12, 2004 10:45 AM by jdanforth

Comments

# re: Use LockWindowUpdate to get flicker-free RichTextBox update

Friday, March 12, 2004 8:58 AM by Kevin Westhead
I think it's better to use SendMessage with WM_SETREDRAW to enable/disable drawing on a window since LockWindowUpdate can only be applied to one window. This means that you might not be able to lock your window if someone else has already locked a window, or someone else could unlock your window without you knowing.

# Still flickering

Sunday, April 04, 2004 1:57 PM by Marcus Stade
I tried this suggestion of yours, but sadly, my RichtTextBox still flickers when I use AppendText() or resize the form or such. I also tried numerous suggestions about DubbleBuffering and what not, to no avail. Any suggestions?

# Great! Searched for this for a while.

Thursday, May 20, 2004 7:33 AM by Peter Gfader
Thx a lot for your info.

Works great for a Panel with mutiple TextBoxes...

# re: Use LockWindowUpdate to get flicker-free RichTextBox update

Tuesday, June 22, 2004 8:06 PM by Jason Satterfield
I'm new to C#, but I'm a veteran VB( and now VB.NET) programmer, but does SuspendLayout() and ResumeLayout() not work in C#?

# Bee Eee Inventions, LLC Blog » Blog Archive » c# getting rid of the jitter!

Pingback from  Bee Eee Inventions, LLC Blog  » Blog Archive   » c# getting rid of the jitter!

# Some RichTextBox tricks

Monday, July 07, 2008 1:17 PM by Patrick Smacchia [MVP C#]

I have recently been responsible for refactoring the Code Query Language query editor in NDepend to fix

# Some RichTextBox tricks

Monday, July 07, 2008 1:53 PM by Community Blogs

I have recently been responsible for refactoring the Code Query Language query editor in NDepend to fix