zoukankan      html  css  js  c++  java
  • C# 解决窗体闪烁

    C# 解决窗体闪烁

    题
    登录以投票
    在Windows窗体上造成“闪烁”的窗体上有很多控制。造成这种闪烁的原因有两个:

    1.当控件需要被绘制时,Windows发送一个控件两个消息。第一个(WM_ERASEBKGND)导致背景被绘制(OnPaintBackground),第二个导致前景被绘(WM_PAINT,射击OnPaint)。首先看到背景,然后当绘图缓慢时前景变得明显。Windows窗体使用ControlStyles.OptimizedDoubleBuffer为这种闪烁提供了一个现成的解决方案。

    2.有很多控件的表单需要很长时间来绘制。尤其是按钮控件的默认样式是昂贵的。一旦你得到了超过50个控件,它开始变得明显。Form类首先绘制其背景,并在控件需要去的地方留下“漏洞”。当您使用不透明度或透明度键属性时,这些孔通常是白色的,黑色的。然后每个控件都被绘制,填充在洞中。视觉效果是丑陋的,在Windows窗体中没有现成的解决方案。双缓冲不能解决它,因为它只适用于单一控制,而不是一组复合控件。

    我在SDK头文件中发现了一个新的Windows风格,可用于Windows XP和(推测)Vista:WS_EX_COMPOSITED。随着窗体打开窗体,Windows XP将在窗体及其所有子控件上进行双缓冲。这有效解决了闪烁的第二个原因。这里有一个例子:

    using System; 
    使用System.Drawing; 
    使用System.Windows.Forms; 

    命名空间WindowsApplication1 { 
      公共部分类Form1:窗体{ 
        公共Form1(){ 
          InitializeComponent(); 
          for(int ix = 0; ix <30; ++ ix){ 
            for(int iy = 0; iy <30; ++ iy){ 
              Button btn = new Button(); 
              btn.Location = new Point(ix * 10,iy * 10);
              this.Controls.Add(BTN); 
            } 
          } 
        } 
        protected override CreateParams CreateParams { 
          get { 
            CreateParams cp = base.CreateParams; 
            cp.ExStyle | = 0x02000000; 
            返回cp; 
          } 
        } 
      } 


    要看到它在工作,最大限度地减少和恢复的形式,并观察其绘画行为。注释cp.ExStyle赋值以查看差异。您只需要复制并粘贴CreateParams属性即可。

    一些注意事项:这不会加速绘画。绘画正在发生时,您的表单将保持不可见状态,然后在完成后弹出屏幕。而当您使用不透明度或透明度键属性时不起作用,当绘画发生时,表单轮廓将显示为丑陋的黑色矩形。最好的解决方法是使用计时器将不透明度值增加到99%,以使表单在绘制后可见。

    我还没有尝试过很多,如果你有使用它的问题,请张贴到这个线程。

    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.  

     参考链接:https://social.msdn.microsoft.com/Forums/windows/en-US/aaed00ce-4bc9-424e-8c05-c30213171c2c/flickerfree-painting?forum=winforms

                       http://blog.csdn.net/itoccupant/article/details/32334877

  • 相关阅读:
    幸存者偏差Survivorship Bias
    如何用一月6RMB搭建一个国外服务器
    因果性≠相关性
    三维组态可视化解决方案
    君子生非异也,善假于物也
    机器人制证系统大屏可视化
    C# WPF 嵌入网页版WebGL油田三维可视化监控
    OffscreenCanvas-离屏canvas使用说明
    去掉图片黑背景输出为透明背景
    高清屏下canvas重置尺寸引发的问题
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/8391902.html
Copyright © 2011-2022 走看看