C#解決MDI窗體閃屏的方法

最近用到MDI窗體,每當顯示子窗體的時候會有一次「閃爍」,每次在show()子窗體的時候都會調用子窗體構造函數重繪窗體,其中須要將子窗體的尺寸調整到我在程序中設置的大小,不管我這樣設置,這個窗口大小變化總會在show()的時候顯示出來,設置雙緩衝、先隱藏窗體等啓動以後再顯示、藉助定時器設置窗體的opacity屬性,問題依舊,一個偶然的機會發現遇到這個問題的哥們兒還很多,各類國家的程序員都有,其中一個哥們提供了一種一勞永逸的解法,完全的解決了個人問題,天降救世主啊,爲了這個問題我茶飯不思了好多天,現將方法分享一下,謝謝這位美國小夥子:程序員

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/aaed00ce-4bc9-424e-8c05-c30213171c2c/ide

將如下代碼塊加在父窗體中的任意位置函數

protected override CreateParams CreateParamsspa

{.net

getorm

{blog

CreateParams cp = base.CreateParams;ci

cp.ExStyle |= 0x02000000;get

return cp;it

}

}

原理很簡單,引用如下原話:

 A form that has a lot of controls takes a long time to paint.  Especially the Button control in its default style is expensive.  Once you get over 50 controls, it starts getting noticeable.  The Form class paints its background first and leaves "holes" where the controls need to go.  Those holes are usually white, black when you use the Opacity or TransparencyKey property.  Then each control gets painted, filling in the holes.  The visual effect is ugly and there's no ready solution for it in Windows Forms.  Double-buffering can't solve it as it only works for a single control, not a composite set of controls. 

I discovered a new Windows style in the SDK header files, available for Windows XP and (presumably) Vista: WS_EX_COMPOSITED.  With that style turned on for your form, Windows XP does double-buffering on the form and all its child controls.  --------------------- 原文:https://blog.csdn.net/onejune2013/article/details/7664323

相關文章
相關標籤/搜索