winforms Label flickering

戏子无情 提交于 2019-12-01 00:31:09

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.

Paste this into your form code to help the Dock layout calculations:

    protected override void OnLoad(EventArgs e) {
        label1.Size = this.ClientSize;
        base.OnLoad(e);
    }
Dan Abramov

Not really an answer, but why would you want to update your label each millisecond? If you meant 1 second, you'd have to set your interval to 1000.

You can solve this by giving the form time to redraw itself and using a larger interval.

Update: turned out, setting DoubleBuffered to true solves the problem. Thanks for csharptest.net for pointing this out and DxCK for correcting me.

csharptest.net

I think you're looking for this: http://msdn.microsoft.com/en-us/library/3t7htc9c.aspx

example:

class Form1 : Form
{
   public Form1() {
      this.DoubleBuffered = true;
   }
}

Stop setting timers to 1ms. No seriously, what you see here is that the Label tries to keep up with it's changes, but fails to do so because they're to frequent. So possible solutions are:

  • Choose a way to change the Label less often
  • Activate Double-Buffering on the Form

Why not run your label update function via an asynchronous delegate? Or use System.Threading namespace for a different flavor.

Also, as people before me mentioned, it would be useful if you set the DoubleBuffer property on your form to true (it's no silver bullet though).

Activating Double Buffering on the form will fix the problem. But that is actually an expensive solution. If you just add the following to the form:

 SetStyle(ControlStyles.AllPaintingInWmPaint, true);

the flicker will be over too.

The default behavior of the Windows OS it to first let all windows paint their background, and later, let them do actual painting. This is from the old days, when painting letters actually took considerable amount of time. This flag tells it to fold the background paint and the regular paint (for the same window) immediately after each other.

Real Double buffering can be reserved for the cases where you actually do painting yourself (when you override OnPaint).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!