Monday, May 30, 2011

Flicker free controls: SplitContainer

.Net Flicker Fee Controls 


Here are the steps to use this control:
1. Add new class "NonFlickerSplitContainer" to your C# application.
2. Replace auto generated class code with C# code shown below.
3. Use NonFlickerSplitContainer object instead of SplitContainer object in your application.

    public partial class NonFlickerSplitContainer : SplitContainer
    {
        public NonFlickerSplitContainer()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                               ControlStyles.UserPaint |
                               ControlStyles.OptimizedDoubleBuffer, true);

            MethodInfo objMethodInfo = typeof(Control).GetMethod(             "SetStyle",BindingFlags.NonPublic|BindingFlags.Instance);

            object[] objArgs = new object[] { ControlStyles.AllPaintingInWmPaint |
                                                                ControlStyles.UserPaint |
                                                               ControlStyles.OptimizedDoubleBuffer, true };

            objMethodInfo.Invoke(this.Panel1, objArgs);
            objMethodInfo.Invoke(this.Panel2, objArgs);
        }
    }

7 comments:

  1. Amazing solution... spent the last 4 hours hunting and found yours. Thanks a million!!!

    ReplyDelete
  2. Thanks a lot!!! It actually helped me to solve the issue. some how my application was continuously taking 25% CPU for repaint when i used create param. i just commented it and followed your approach, it worked like charm, as it is now painting only the splitter control.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete