Windows窗体选项卡控件闪烁与背景

时间:2022-07-03 15:53:13

I have a panel on a windows form. A form is loaded into the panel based off what button they click. One of these forms that's loaded into the panel has a tab control. The background of the form is an image and every time i switch between tabs, the form flickers. When I set the background image to nothing or a solid color, it works fine so it has to be the image. Is there any way around this? Thanks in advance.

我在Windows窗体上有一个面板。根据单击的按钮将表单加载到面板中。加载到面板中的这些表单之一具有选项卡控件。表单的背景是一个图像,每次我在选项卡之间切换时,表单都会闪烁。当我将背景图像设置为无或纯色时,它可以正常工作,因此它必须是图像。有没有办法解决?提前致谢。

4 个解决方案

#1


Images are expensive to draw, especially on form backgrounds. The only thing that I've found to help this is to set the BackgroundImageLayout to None, which helped reduce flicker in an application that I had developed.

绘制图像的成本很高,尤其是在表格背景上。我发现唯一能帮助它的是将BackgroundImageLayout设置为None,这有助于减少我开发的应用程序中的闪烁。

You can also try setting the DoubleBuffered property to true but I'm not sure how much mileage you will get out of that.

您也可以尝试将DoubleBuffered属性设置为true,但我不确定您将获得多少里程数。

The problem is that when a control that is placed on the form has to redraw (as in when changing tab pages), everything underneath that control has to be redrawn as well. This causes your form background to invalidate and redraw, sometimes multiple times as the invalidation happens on multiple controls (they don't batch).

问题是当放置在窗体上的控件必须重绘时(如更改标签页时),该控件下面的所有内容也必须重绘。这会导致您的表单背景无效并重绘,有时多次因为多个控件上发生失效(它们不批处理)。

#2


To address your follow-up question about converting to PArgb32, the following code will do the trick:

要解决有关转换为PArgb32的后续问题,以下代码可以解决这个问题:

using System.Drawing;
using System.Drawing.Imaging;

Bitmap originalBmp;
var converted = new Bitmap(originalBmp.Width, originalBmp.Height, PixelFormat.Format32bppPArgb);
using (var g = Graphics.FromImage(converted))
{
    g.DrawImage(0, 0, originalBmp);
}

You'll still need to use double-buffered controls to reduce flicker, but converting your images to PArgb32 ought to speed up the drawing considerably.

您仍然需要使用双缓冲控件来减少闪烁,但将图像转换为PArgb32应该可以大大加快绘图速度。

#3


You can use flicker free tab panel to overcome this issue:

您可以使用无闪烁选项卡面板来解决此问题:

Here is sample code for panel control:

以下是面板控件的示例代码:

public partial class NonFlickerPanel : Panel
{
   public NonFlickerPanel() : base()
   {
          this.SetStyle(ControlStyles.AllPaintingInWmPaint,
                              ControlStyles.UserPaint 
                              ControlStyles.OptimizedDoubleBuffer, 
                              true);
   }
}

#4


Look into the DoubleBuffered property of the Image and/or TabControl/

查看Image和/或TabControl /的DoubleBuffered属性

If you set it to true, the flickering will probably stop.

如果将其设置为true,则闪烁可能会停止。

#1


Images are expensive to draw, especially on form backgrounds. The only thing that I've found to help this is to set the BackgroundImageLayout to None, which helped reduce flicker in an application that I had developed.

绘制图像的成本很高,尤其是在表格背景上。我发现唯一能帮助它的是将BackgroundImageLayout设置为None,这有助于减少我开发的应用程序中的闪烁。

You can also try setting the DoubleBuffered property to true but I'm not sure how much mileage you will get out of that.

您也可以尝试将DoubleBuffered属性设置为true,但我不确定您将获得多少里程数。

The problem is that when a control that is placed on the form has to redraw (as in when changing tab pages), everything underneath that control has to be redrawn as well. This causes your form background to invalidate and redraw, sometimes multiple times as the invalidation happens on multiple controls (they don't batch).

问题是当放置在窗体上的控件必须重绘时(如更改标签页时),该控件下面的所有内容也必须重绘。这会导致您的表单背景无效并重绘,有时多次因为多个控件上发生失效(它们不批处理)。

#2


To address your follow-up question about converting to PArgb32, the following code will do the trick:

要解决有关转换为PArgb32的后续问题,以下代码可以解决这个问题:

using System.Drawing;
using System.Drawing.Imaging;

Bitmap originalBmp;
var converted = new Bitmap(originalBmp.Width, originalBmp.Height, PixelFormat.Format32bppPArgb);
using (var g = Graphics.FromImage(converted))
{
    g.DrawImage(0, 0, originalBmp);
}

You'll still need to use double-buffered controls to reduce flicker, but converting your images to PArgb32 ought to speed up the drawing considerably.

您仍然需要使用双缓冲控件来减少闪烁,但将图像转换为PArgb32应该可以大大加快绘图速度。

#3


You can use flicker free tab panel to overcome this issue:

您可以使用无闪烁选项卡面板来解决此问题:

Here is sample code for panel control:

以下是面板控件的示例代码:

public partial class NonFlickerPanel : Panel
{
   public NonFlickerPanel() : base()
   {
          this.SetStyle(ControlStyles.AllPaintingInWmPaint,
                              ControlStyles.UserPaint 
                              ControlStyles.OptimizedDoubleBuffer, 
                              true);
   }
}

#4


Look into the DoubleBuffered property of the Image and/or TabControl/

查看Image和/或TabControl /的DoubleBuffered属性

If you set it to true, the flickering will probably stop.

如果将其设置为true,则闪烁可能会停止。