Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Flicker-Free for MDIClient - VB.NET

$
0
0

 

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

ForEach ctl InMe.Controls

IfTypeOf 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 AsObject() = {styles, True}

method.Invoke(mdi, param)

ExitFor

EndIf

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


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>