转载:解决 C# Winform 窗体打开时闪烁问题

这个问题属于必须解决的问题,而且界面的控件越多,闪烁也越多,试过多种解决办法效果都不理想。

解决办法:把此段代码加入到窗体代码中

protected override CreateParams CreateParams {
            get {
                CreateParams paras = base.CreateParams;
                paras.ExStyle |= 0x02000000;
                return paras;
            }
        }

主要原因是对于Winform来说,一个窗体中绘制多个控件是很花时间的。特别是默认的按钮控件。Form先画出背景,然后留下控件需要的“洞”。如果控件的背景是透明的,那么这些“洞”就会先以白色或黑色出现,然后每个控件的“洞”再被填充,就是我们所看到的闪烁,在WinForm中没有现成的解决方案。设置控件双缓冲并不能解决它,因为它只适用于自己,而不是复合控件集。

而原文作者发现了一种新的Windows样式,可用于WindowsXP和Vista。只要你设置该样式,WindowsXP对窗体及其所有子控件进行双重缓冲。

原理原文
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.




 




 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
回答: 在C# WinForm窗体中,如果你想要在表格控件上填充颜色,但是鼠标经过其他控件整个表格里的数据都变了,可能是因为你在窗体上直接绘制了表格,而没有使用自定义控件来解决这个问题。\[1\] 你可以尝试使用自适应类来解决这个问题。首先,将自适应类的代码复制到你的工程命名空间中,然后声明自适应类的实例。接下来,在窗体上添加SizeChanged事件,并在事件方法中记录窗体和控件的初始位置和大小。最后,调用自适应类的自适应方法,完成自适应。这样,你就可以在表格控件上填充颜色而不会受到其他控件的影响了。\[2\] 你还可以使用Panel、DataGridView等控件来实现自适应。\[2\] 在窗体的代码中,你需要引入System、System.Collections.Generic、System.ComponentModel、System.Drawing和System.Windows.Forms这些命名空间,并在窗体的Load事件中调用自适应类的初始化方法,记录窗体和控件的初始位置和大小。然后,在窗体的SizeChanged事件中调用自适应类的自适应方法,完成自适应。\[3\] 这样,你就可以在C# WinForm窗体中使用表格控件,并且实现自适应的效果了。 #### 引用[.reference_title] - *1* [C#自定义控件实现窗体绘制表格](https://blog.csdn.net/qq_22955427/article/details/76252582)[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^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [C# WinForm 窗体及其控件的自适应](https://blog.csdn.net/chbxgbin/article/details/109690260)[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^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值