Bee Eee Blog

c# getting rid of the jitter!

by on Apr.18, 2008, under .NET, c#, c# coding GUI, coding, graphics, GUI

I recently was getting greatly annoyed with some drawing code in my program.  I was drawing a photo that I could zoom and move around.  When I zoomed with the wheel though it would briefly draw the image in two positions.  The first position after it was zoomed, and the second position after it was re-centered on the screen.  Well this looked awful.  Not exactly professional looking.  So I tried a hundered different things.  I tried setting a bool variable in the control to tell the paint function not to draw.  No good.  The drawing was done with asynchronous messages.  Then I tried  BeginInvoke.  That didn’t work also.  I tried SuspendLayout and ResumeLayout functions of the Control class:  No good.  Finally I just used the Visible property which kind of worked, well I didn’t get the double picture, but my background was light color and the foreground dark, so there was still a bad looking flicker.  After a couple of hours I finally found the solution at: http://weblogs.asp.net/jdanforth/archive/2004/03/12/88458.aspx. I worked on the example a little and boiled it down to two functions: StopDrawing and StartDrawing.

The techinque basically turns of drawing and events to the object on the win32 level underneath .Net.  This is very handy and it worked very very well.  Here is the code.

        using System.Runtime.InteropServices;
        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;

        public void StopDrawing()
        {
            if (drawStopCount == 0)
            {
                // Stop redrawing:
                SendMessage(this.Handle, WM_SETREDRAW, 0, IntPtr.Zero);
                // Stop sending of events:
                eventMask = SendMessage(this.Handle, EM_GETEVENTMASK, 0, IntPtr.Zero);
            }
            drawStopCount++;
        }

        public void StartDrawing()
        {
            drawStopCount--;
            if (drawStopCount == 0)
            {
                // turn on events
                SendMessage(this.Handle, EM_SETEVENTMASK, 0, eventMask);

                // turn on redrawing
                SendMessage(this.Handle, WM_SETREDRAW, 1, IntPtr.Zero);

                Invalidate();
                Refresh();
            }
        }
:, , , ,
No comments for this entry yet...

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...