如何在Winform C#中消除闪烁。

huangapple go评论55阅读模式
英文:

How to remove Flicker in Winform c#

问题

有没有办法在C#的Windows表单中停止闪烁?

private void btnBFS_MouseEnter(object sender, EventArgs e)
{
    infoGroupbox.Text = "广度优先搜索";
    travinfo.Hide();
    bfsinfo.Show();
}
private void btnBFS_MouseLeave(object sender, EventArgs e)
{
    infoGroupbox.Text = "遍历";
    bfsinfo.Hide();
    travinfo.Show();
}

我期望有一个平滑的过渡,但当我切换到其他面板或分组框时会出现闪烁。

英文:

Is there a way to stop the flickering in Windows form c#?

private void btnBFS_MouseEnter(object sender, EventArgs e)
{
    infoGroupbox.Text = "Breadth First Search";
    travinfo.Hide();
    bfsinfo.Show();
}
private void btnBFS_MouseLeave(object sender, EventArgs e)
{
    infoGroupbox.Text = "Traversal";
    bfsinfo.Hide();
    travinfo.Show();
}
    

Im expecting smooth transition but when i transition to other panel or groupbox it flickers.

答案1

得分: 1

以下代码有助于消除闪烁。仅将DoubleBuffered属性设置为true通常是不够的。以下代码可应用于包含有问题控件的窗体或面板。

public void EnableDoubleBuffering()
{
   // 将双缓冲样式位的值设置为true。
   this.SetStyle(ControlStyles.DoubleBuffer | 
      ControlStyles.UserPaint | 
      ControlStyles.AllPaintingInWmPaint,
      true);
   this.UpdateStyles();
}

来源:
https://learn.microsoft.com/de-de/dotnet/api/system.windows.forms.control.setstyle?view=windowsdesktop-8.0

在WinForms中实现平滑过渡并不容易。您需要重写控件的Paint方法并使用定时器来工作。

英文:

The following code helps to remove flickering. Just setting DoubleBuffered = true is often not enough. The following code can be applied to a form or a panel in which the problematic controls are located.

public void EnableDoubleBuffering()
{
   // Set the value of the double-buffering style bits to true.
   this.SetStyle(ControlStyles.DoubleBuffer | 
      ControlStyles.UserPaint | 
      ControlStyles.AllPaintingInWmPaint,
      true);
   this.UpdateStyles();
}

Source:
https://learn.microsoft.com/de-de/dotnet/api/system.windows.forms.control.setstyle?view=windowsdesktop-8.0

Smooth transitions in WinForms are not easy to accomblisch. You need to override the Paint of a control and work with timers.

huangapple
  • 本文由 发表于 2023年5月22日 18:26:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定