none
Flicker-Free for MDIClient - VB.NET

    General discussion

  •  

    Mods, Please move this if it would be more useful in a different category.

     

    Okay, I'm sure if you're here, you are having a problem with your MDIForm.BackgroundImage.  It seems that Microsoft forgot to give a certain control the "DoubleBuffer" property.  That control is the MDIClient.  When you set the "IsMDIContainer" for a form to true, it creates a control on the form called the MDIClient.  when you set Me.BackgroundImage = "somepicture" the MDIForm then sets the MDIClient.BackgroundImage to that picture.  Any time the form needs to be repainted you see the flicker, and if your computer is as dull as mine, it's not just a flicker, you get to watch the background get repainted.  So just set the DoubleBuffer property for the MDIForm to True you say?  Go ahead, wont make a difference.  Because the MDIClient doesn't inherit that property.  Here's the fix:

     

    In the New() constructor for the MDIForm add this code:

     

     

    Code Snippet

    'First find the mdiClient

    Dim ctl As Control

    For Each ctl In Me.Controls

    If TypeOf ctl Is MdiClient Then

    Dim mdi As MdiClient = CType(ctl, MdiClient)

    mdi.BackColor = Color.DarkGray

    mdi.BackgroundImageLayout = ImageLayout.Stretch

    mdi.BackgroundImage = "yourpicturehere"

    'Because MDIClient for some reason does not inherit the DoubleBuffer setting from

    'the MDIForm, and does not expose that property to the public, we must execute

    'code below to set the flag to true

    Dim styles As ControlStyles = ControlStyles.OptimizedDoubleBuffer

    Dim flags As Reflection.BindingFlags = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic

    Dim method As Reflection.MethodInfo = mdi.GetType.GetMethod("SetStyle", flags)

    Dim param As Object() = {styles, True}

    method.Invoke(mdi, param)

    Exit For

    End If

    Next

     

     

    I added a few things just for sake of showing that you can set them here.  The backcolor, background image and the layout can still be set from the mdiform designer, or you can do it here as I did.  So, there you go.  I hope this code proves to be useful for you.  I know it fixed my problems.

     

    -Jess

    Wednesday, June 04, 2008 7:43 PM

All replies

  • And in C#...

     

    Code Snippet

    // double buffer this form's Mdi client

    foreach (Control control in this.Controls)

      if (control is MdiClient)

      {

        MethodInfo method = ((MdiClient)control).GetType().GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic);

        method.Invoke((MdiClient)control, new Object[] { ControlStyles.OptimizedDoubleBuffer, true});

      }

     

     

    Turn off flickering (double buffer) when using BackgroundImage on an Mdi parent form in C#.

     

    Insert this code into the instance contructor of an Mdi parent form.  You will need to include the namespace System.Reflection. 

     

    Thanks to the poster above for this technique. 

    Monday, June 09, 2008 4:54 PM

  • hi Nik Gibbs,

        I make use of your code in my mdi form constructor, but it dosen't work.
    My MDI form contain lot of controls like splitter another form, button control etc.

    can you help me?


    Thursday, August 28, 2008 9:13 AM
  • Dear jeZteRicp,

     

    despite of your solution, background image flickering problem still persists.

     

    saumitra.

    Friday, March 18, 2011 2:29 PM
  • I used the following vb.net code (converted from above c# as given by Nik) in mdi form load event and flickering has decreased very much.

     

    For Each control As Control In Me.Controls

                If TypeOf control Is MdiClient Then

                    Dim method As MethodInfo = DirectCast(control, MdiClient).[GetType]().GetMethod("SetStyle", BindingFlags.Instance Or BindingFlags.NonPublic)

                    method.Invoke(DirectCast(control, MdiClient), New [Object]() {ControlStyles.OptimizedDoubleBuffer, True})

                End If

            Next

     

    Thanks for the code.

    Sushil Soni

    Email : info.glcom@gmail.com

    Skype: sushil.soni02

     

     

    Tuesday, April 26, 2011 10:48 AM
  • It's been a long while since you posted this but I wanted to post that your solution worked GANGBUSTERS for me! I simply added the following code to the Load() routine of my main form (the MDI Parent form in my application):

    'Code(Snippet) as per: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/99093134-b73a-47d9-8b59-f6d3bd4d2c45

     

    'First find the mdiClient

     

    Dim ctl As Control

     

    For Each ctl In Me.Controls

     

    If TypeOf ctl Is MdiClient Then

     

    Dim mdi As MdiClient = CType(ctl, MdiClient)

    mdi.BackColor = Color.DarkGray

    mdi.BackgroundImage = CSS.My.Resources.Resources.CSS

    mdi.BackgroundImageLayout = ImageLayout.Stretch

     

    'Because MDIClient for some reason does not inherit the DoubleBuffer setting from

     

    'the MDIForm, and does not expose that property to the public, we must execute

     

    'code below to set the flag to true

     

    Dim styles As ControlStyles = ControlStyles.OptimizedDoubleBuffer

     

    Dim flags As Reflection.BindingFlags = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic

     

    Dim method As Reflection.MethodInfo = mdi.GetType.GetMethod("SetStyle", flags)

     

    Dim param As Object() = {styles, True}

    method.Invoke(mdi, param)

     

    Exit For

     

    End If

     

    Next
    Wednesday, November 16, 2011 9:37 PM
  • GANGBUSTERS is good right?  ;)

    I'm glad this is working for you guys.

    those who had issues:  I'm not sure what the issue is exactly.  But look at the code again AND look at the flicker again.  Is it the background image flickering or is it the other controls.  It could be the other controls CAUSING the image to flicker - I can't be sure.  My project had nothing in the client area.  there was a file menu and a tool bar at the top and a status bar at the bottom.  the client area was completely open.  Also, what's the size and resolution of the image and the screen its drawn on.  there are a lot of variables to consider.  I'm sorry it didn't work for you, but keep looking, it's probably something small your missing - always is for me.


    the real JeZteR
    Check out my Blog: Titan Blog
    Check my unanswered questions: JeZteR's Unanswered Questions
    Tuesday, November 29, 2011 5:02 PM
  • Thanks! If solved my problem! Nice code!
    Wednesday, December 19, 2012 10:54 AM