我已经派生了一个TabControl,其明确目的是启用双缓冲,除了没有按预期工作。这是TabControl代码:

class DoubleBufferedTabControl : TabControl
{
    public DoubleBufferedTabControl() : base()
    {
        this.DoubleBuffered = true;
        this.SetStyle
            (
                ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.ResizeRedraw |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.SupportsTransparentBackColor,
                false
            );
    }
}

然后将此Tabcontrol设置为“OwnerDrawnFixed”的绘制模式,以便我可以更改颜色。这是自定义绘图方法:

    private void Navigation_PageContent_DrawItem(object sender, DrawItemEventArgs e)
    {
        //Structure.
        Graphics g = e.Graphics;
        TabControl t = (TabControl)sender;
        TabPage CurrentPage = t.TabPages[e.Index];

        //Get the current tab
        Rectangle CurrentTabRect = t.GetTabRect(e.Index);

        //Get the last tab.
        Rectangle LastTab = t.GetTabRect(t.TabPages.Count - 1);

        //Main background rectangle.
        Rectangle BackgroundRect = new Rectangle(LastTab.Width, t.Bounds.Y - 4, t.Width - (LastTab.Width), t.Height);

        //Tab background rectangle.
        Rectangle TabBackgroundRect = new Rectangle(0, LastTab.Y + LastTab.Height, LastTab.Width, t.Bounds.Height - (LastTab.Y + LastTab.Height));

        //Set anitialiasing for the text.
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

        //String format for the text.
        StringFormat StringFormat = new StringFormat();
        StringFormat.Alignment = StringAlignment.Center;
        StringFormat.LineAlignment = StringAlignment.Center;

        //Fill the background.
        g.FillRectangle(Brushes.LightGray, BackgroundRect);
        g.FillRectangle(Brushes.Bisque, TabBackgroundRect);

        //Draw the selected tab.
        if(e.State == DrawItemState.Selected)
        {
            g.FillRectangle(Brushes.White, e.Bounds);
            Rectangle SelectedTabOutline = new Rectangle(e.Bounds.X + 2, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 4);
            g.DrawRectangle(new Pen(Brushes.LightGray, 4f), SelectedTabOutline);
            g.DrawString(CurrentPage.Text, new Font("Arial", 12f, FontStyle.Bold, GraphicsUnit.Point), new SolidBrush(Color.FromArgb(70, 70, 70)), CurrentTabRect, StringFormat);
        }
        else
        {
            g.FillRectangle(new SolidBrush(Color.FromArgb(230, 230, 230)), e.Bounds);
            g.DrawString(CurrentPage.Text, new Font("Arial", 12f, FontStyle.Regular, GraphicsUnit.Point), Brushes.Gray, CurrentTabRect, StringFormat);
        }

    }

然而,这一切都无济于事,因为这个控件不是双重缓冲的,并且在调整大小时仍然会闪烁。

有什么想法吗?

有帮助吗?

解决方案

如果您阅读文档,则说“此成员对此控件没有意义。”如果您希望使用双缓冲来绘制控件,则必须自己实现它。除了你自己绘制控件之外,无论如何你必须自己实现双缓冲。

其他提示

首先,你可以摆脱你的 TabControl 代码—你打开缓冲,然后立即关闭它,所以它实际上并没有做任何有用的事情。

你的一部分问题是你试图只绘制 TabControl 部分

提供大约90%解决方案的最简单解决方案(仍然可以获得闪烁)是将其添加到您的表单类中:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x02000000;
        return cp;
    }
}

如果你想非常确保没有闪烁,你需要自己绘制整个 TabControl ,并确保忽略背景绘制请求。 / p>

编辑:请注意,这只适用于XP及更高版本。

过去我在控件上遇到双缓冲问题,停止闪烁的唯一方法是确保没有调用继承的OnPaintBackground方法。 (参见下面的代码)您还需要确保在油漆调用期间绘制整个背景。

protected override void OnPaintBackground( PaintEventArgs pevent )
{
    //do not call base - I don't want the background re-painted!
}

不确定,但您可以尝试双缓冲包含选项卡控件的控件。

我环顾四周,尝试了你的代码和我能想到的任何其他东西,但我没有办法摆脱闪烁。不幸的是,在我的测试中,即使是在调整大小期间,常规(非所有者绘制的)选项卡控件也会闪烁。

为了它的价值,关闭“拖动时显示窗口内容”将解决它,但我意识到这可能没有帮助。

我认为它不起作用,因为你禁用双缓冲!

所有 this.DoubleBuffered = true 将ControlStyles.OptimizedDoubleBuffer设置为true。由于您在程序的下一行中禁用该标志,因此您实际上什么都不做。删除ControlStyles.OptimizedDoubleBuffer(也许还有ControlStyles.AllPaintingInWmPaint),它应该适合你。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top