的WinForms标签闪烁 [英] winforms Label flickering

查看:294
本文介绍了的WinForms标签闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Label控件是非常闪烁的问题。

下面是一些code重现该问题。

如何解决此问题?

更新:为previous情况下的解决方案(表格直接包含一个Label)是使form.DoubleBuffered = TRUE。但是,这不是一个通用的解决方案。举例来说,我应该怎么做标签的SplitContainer的内部情况?这是我真实的案例。

更新时间是code:

DoubleBufferedLabel.cs:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;

命名空间FlickerLabelTest
{
    公共类DoubleBufferedLabel:标签
    {
        公共DoubleBufferedLabel()
        {
            DoubleBuffered = TRUE;
        }
    }
}
 

DoubleBufferedSplitContainer.cs:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;

命名空间FlickerLabelTest
{
    公共类DoubleBufferedSplitContainer:SplitContainer的
    {
        公共DoubleBufferedSplitContainer()
        {
            DoubleBuffered = TRUE;
        }
    }
}
 

Form1.cs的:

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;

命名空间FlickerLabelTest
{
    公共部分类Form1中:形态
    {
        公共Form1中()
        {
            的InitializeComponent();
        }

        私人无效timer1_Tick(对象发件人,EventArgs的)
        {
            label1.Text + =0;
        }
    }
}
 

Form1.Designer.cs:

 命名空间FlickerLabelTest
{
    部分Form1类
    {
        ///<总结>
        ///必需的设计变量。
        ///< /总结>
        私人System.ComponentModel.IContainer成分= NULL;

        ///<总结>
        ///清理所有正在使用的资源。
        ///< /总结>
        ///< PARAM NAME =处理>如此,如果管理资源应该处理;否则为false< /参数>
        保护覆盖无效的Dispose(BOOL处置)
        {
            如果(处置和放大器;&安培;!(成分= NULL))
            {
                components.Dispose();
            }
            base.Dispose(处置);
        }

        #region Windows窗体设计器生成的code

        ///<总结>
        ///设计器支持所需的方法 - 不要修改
        ///此方法的code编辑器的内容。
        ///< /总结>
        私人无效的Ini​​tializeComponent()
        {
            this.components =新System.ComponentModel.Container();
            this.timer1 =新System.Windows.Forms.Timer(this.components);
            this.label1 =新FlickerLabelTest.DoubleBufferedLabel();
            this.splitContainer1 =新DoubleBufferedSplitContainer();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            //
            //定时器1
            //
            this.timer1.Enabled = TRUE;
            this.timer1.Interval = 1;
            this.timer1.Tick + =新System.EventHandler(this.timer1_Tick);
            //
            // LABEL1
            //
            this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.label1.Location =新System.Drawing.Point(0,0);
            this.label1.Name =LABEL1;
            this.label1.Size =新System.Drawing.Size(186,262);
            this.label1.TabIndex = 0;
            this.label1.Text =LABEL1;
            //
            // splitContainer1
            //
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location =新System.Drawing.Point(0,0);
            this.splitContainer1.Name =splitContainer1;
            //
            // splitContainer1.Panel2
            //
            this.splitContainer1.Panel2.Controls.Add(this.label1);
            this.splitContainer1.Size =新System.Drawing.Size(284,262);
            this.splitContainer1.SplitterDistance = 94;
            this.splitContainer1.TabIndex = 1;
            //
            // Form1中
            //
            this.AutoScaleDimensions =新System.Drawing.SizeF(6F,13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize =新System.Drawing.Size(284,262);
            this.Controls.Add(this.splitContainer1);
            this.DoubleBuffered = TRUE;
            this.Name =Form1的;
            this.Text =Form1的;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.splitContainer1.Panel2.ResumeLayout(假);
            this.splitContainer1.ResumeLayout(假);
            this.ResumeLayout(假);

        }

        #endregion

        私人System.Windows.Forms.Timer定时器1;
        私人DoubleBufferedLabel LABEL1;
        私人DoubleBufferedSplitContainer splitContainer1;
    }
}
 

Program.cs的:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Windows.Forms的;

命名空间FlickerLabelTest
{
    静态类节目
    {
        ///<总结>
        ///的主入口点的应用程序。
        ///< /总结>
        [STAThread]
        静态无效的主要()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(假);
            Application.Run(新Form1中());
        }
    }
}
 

解决方案

现在的问题是与对接。如果更改了 Label.Dock 填写 ,手动设置标签的大小,以填补分隔面板,然后固定它在各方面,也不会闪烁。

如果你想看到闪烁的原因,而码头仍设置为填写,覆盖 onResize受到 DoubleBufferedLabel 类,启动应用程序,并在其运行时设置断点 onResize受到。添加一个手表的尺寸键,你会看到它的翻转设计时和运行时的大小之间。

我想在你的例子使用的是普通的的SplitContainer 标签,设置 DoubleBuffer 的形式,并没有忽悠,如果我离开码头设置为标签

I have a problem with the Label control that is terribly flickering.

Below is some code to reproduce the problem.

How to solve this?

UPDATE: The solution for the previous case (a Form contains a Label directly) was to make the form.DoubleBuffered = true. But this is not a generic solution. For example, what should I do in the case of label inside a SplitContainer? this is my real case.

UPDATED CODE:

DoubleBufferedLabel.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FlickerLabelTest
{
    public class DoubleBufferedLabel : Label
    {
        public DoubleBufferedLabel()
        {
            DoubleBuffered = true;
        }
    }
}

DoubleBufferedSplitContainer.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FlickerLabelTest
{
    public class DoubleBufferedSplitContainer : SplitContainer
    {
        public DoubleBufferedSplitContainer()
        {
            DoubleBuffered = true;
        }
    }
}

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FlickerLabelTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text += "0";
        }
    }
}

Form1.Designer.cs:

namespace FlickerLabelTest
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.label1 = new FlickerLabelTest.DoubleBufferedLabel();
            this.splitContainer1 = new DoubleBufferedSplitContainer();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.Interval = 1;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // label1
            // 
            this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(186, 262);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.label1);
            this.splitContainer1.Size = new System.Drawing.Size(284, 262);
            this.splitContainer1.SplitterDistance = 94;
            this.splitContainer1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.splitContainer1);
            this.DoubleBuffered = true;
            this.Name = "Form1";
            this.Text = "Form1";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Timer timer1;
        private DoubleBufferedLabel label1;
        private DoubleBufferedSplitContainer splitContainer1;
    }
}

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace FlickerLabelTest
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

解决方案

The problem is with the docking. If you change the Label.Dock from Fill to None, manually set the size of the label to fill the split panel and then anchor it on all sides, it won't flicker.

If you want to see the cause of the flicker, while Dock is still set to Fill, override OnResize in your DoubleBufferedLabel class, start the application, and while it is running set a breakpoint in OnResize. Add a watch for the Size and you'll see it flipping between its design time and runtime sizes.

I tried using a regular SplitContainer and Label in your example, set DoubleBuffer to False on the form, and it did not flicker if I left Dock set to None on the Label.

这篇关于的WinForms标签闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆