如果图像是背景,TabControl 会闪烁 [英] TabControl blinks if image is background

查看:28
本文介绍了如果图像是背景,TabControl 会闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,如果我在具有图像背景的面板中有一个 TabControl,当鼠标悬停在选项卡上时,它会闪烁并重新绘制.是否有解决方法来防止这种情况发生?

I have noticed that if I have a TabControl in a Panel that has an Image Background, when the mouse hovers over a tab it blinks and redraws. Is there a workaround to prevent this from happening?

推荐答案

我明白了.发生这种情况是因为 TabControl 部分地通过要求父控件在其自己的窗口内绘制自身来绘制自身.因为标签没有覆盖控件的整个宽度,所以它们伸出"是必要的.如果 BackgroundImage 的绘制速度很慢,您会看到正在绘制的背景和在其上绘制的选项卡之间出现闪烁.

I see it. It happens because TabControl draws itself partly by asking the parent control to draw itself inside its own window. Necessary because the tabs don't cover the full width of the control, they "stick out". If the BackgroundImage is slow to draw, you'll see a flicker between the background being drawn and the tabs drawn on top of that.

这将很难修复,TabControl 不支持任何类型的双缓冲.您只能通过使 BackgroundImage 高效绘制来最小化效果.为此,您需要使图像与面板的 ClientSize 大小完全一致,这样就不必调整图像的大小.并使用 PixelFormat32bppPArgb 像素格式创建该位图,它通常比其他格式快 10 倍.

This is going to be hard to fix, TabControl doesn't support any kind of double buffering. You can only minimize the effect by making the BackgroundImage efficient to draw. You need to do so by making the image exactly the size of the panel's ClientSize so that the image doesn't have to be resized. And create that bitmap with the PixelFormat32bppPArgb pixel format, it is usually 10 times faster than the other ones.

有一个神奇的治疗方法,窗口有一个样式标志,可以为整个窗口(包括其子控件)启用双缓冲.自 XP 起支持,但已报告了一些副作用.将此代码粘贴到您的表单中,它修复了 TabControl 闪烁:

There's one magic cure available, windows have a style flag that enables double-buffering for the entire window, including its child controls. Supported since XP but some side-effects have been reported. Paste this code into your form, it fixes the TabControl flicker:

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

但请注意,TabControl 的视觉样式渲染器与此样式标志存在相当大的不兼容.如果您的选项卡溢出并且您获得了选择箭头,那么它会变成香蕉并开始一遍又一遍地渲染选项卡,从而产生非常高的闪烁率.

But beware that the visual style renderer for TabControl has one rather major incompatibility with this style flag. If your tabs overflow and you get the selection arrows then it goes bananas and starts rendering the tabs over and over again, producing a very high rate of flicker.

这篇关于如果图像是背景,TabControl 会闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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