C#解決MDI表單閃屏的方法

來源:互聯網
上載者:User

標籤:winform   style   http   strong   os   art   

最近從師兄手上接了一個C#的項目,需要用到MDI表單,可是每當我顯示子表單的時候會有一次“閃爍”,很明顯,看起來非常不爽,尋找許久,知道是每次在show()子表單的時候都會調用子表單建構函式重繪表單,其中需要將子表單的尺寸調整到我在程式中設定的大小,無論我這樣設定,這個視窗大小變化總會在show()的時候顯示出來,我試過網上說的設定雙緩衝、先隱藏表單等啟動之後再顯示、藉助定時器設定表單的opacity屬性,可是問題依舊,沒有任何變化,一個偶然的機會找到了微軟的MSDN論壇,發現遇到這個問題的哥們兒還不少,各種國家的程式員都有,其中一個哥們提供了一種一勞永逸的解法,徹底的解決了我的問題,天降救世主啊,為了這個問題我茶飯不思了好多天,現將方法分享一下,網上有很多人都有遇到這個問題,可是這是我唯一看到的解法,值得各位碼農收藏啊,原文網址如下,謝謝這位美國小夥子:

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

解決辦法很easy:

將以下代碼塊加在父表單中的任意位置

protected override CreateParams CreateParams

{

get

{

CreateParams cp = base.CreateParams;

cp.ExStyle |= 0x02000000;

return cp;

}

}

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

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. 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.