C#解决MDI窗体闪屏的方法



最近从师兄手上接了一个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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中的代码是用于在C#中防止闪屏的。通过重写CreateParams属性,将窗体的ExStyle属性设置为0x02000000,可以防止闪屏。 引用\[2\]和\[3\]提到了解决Windows Forms中窗体闪屏问题的方法。当窗体中有大量控件时,特别是默认样式的Button控件,绘制时间会变长。Windows Forms中的Form类首先绘制背景,并在控件需要放置的位置留下“空洞”。这些空洞通常是白色的,当使用Opacity或TransparencyKey属性时,会变成黑色。然后每个控件都会被绘制,填充这些空洞。这种视觉效果很不美观,而且在Windows Forms中没有现成的解决方案。双缓冲不能解决这个问题,因为它只适用于单个控件,而不是一组组合控件。然而,通过在窗体上启用WS_EX_COMPOSITED样式,可以实现双缓冲,使得Windows XP对窗体及其所有控件进行双缓冲绘制,从而解决闪屏问题。 因此,要解决C#窗体闪屏问题,可以使用引用\[1\]中的代码来防止闪屏,或者使用引用\[2\]和\[3\]中提到的WS_EX_COMPOSITED样式来实现双缓冲绘制。 #### 引用[.reference_title] - *1* [C# 解决winform界面闪屏问题及弊端](https://blog.csdn.net/Qin066/article/details/80102927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [C# Winform程序闪屏问题(完美解决)](https://blog.csdn.net/l_mloveforever/article/details/86493284)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值