WinForms用户控件上的标签背景轻拂已启用背景图像 [英] label backgrond flicking on a WinForms user control has background image enabled

查看:96
本文介绍了WinForms用户控件上的标签背景轻拂已启用背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows窗体项目中工作,并且UserControl Double Buffering出现了一些问题. 我创建了一个usercontrol并有一个背景图片,然后在它上面没有几个单选按钮和标签.单选按钮和标签都具有透明的背景作为颜色. 但是,当我显示和隐藏User控件时,可以看到那些标签和具有透明背景的单选按钮上的闪烁.

I am working on a windows form project and having some problem with UserControl Double Buffering. I created a usercontrol and has a background image, then on top of it I have few radio buttons and labels. Radio buttons and labels are all having transparent background as color. However, when I show and hide the User control, I can see the flickering on those labels and radio buttons that has transparent background.

我尝试了

Me.SetStyle(ControlStyles.DoubleBuffer _
Or ControlStyles.AllPaintingInWmPaint _
Or ControlStyles.UserPaint _
Or ControlStyles.SupportsTransparentBackColor, _
True)

在initializeComponent()之后在此用户控件上启用双缓冲,但是它似乎不起作用.

After initializeComponent() to enable double buffer on this user control, but it doesn’t seem to work.

推荐答案

这不是双重缓冲可以解决的闪烁问题.当UC重新绘制自身时,它将绘制背景图像,从而在控件所在的位置留下孔.然后,每个控件都可以自己绘画,通过先要求UC重新绘制自身以生成背景像素,然后在顶部绘制自己,来填充孔.临时孔就是您所看到的闪烁.

This is not a source of flicker that double-buffering can solve. When the UC repaints itself, it draws the background image, leaving holes where the controls go. The controls then each get to paint themselves, filling in the hole by first asking the UC to draw itself again to produce the background pixels, then draw themselves on top. The temporary hole is what you see as flicker.

您可以通过允许UC在控件的客户区域中进行绘制来减轻反对感,从而可以正确设置背景.将此代码粘贴到UserControl类中:

You can make it less objectionable by allowing the UC to draw itself in the client area of the controls so the background is already set correctly. Paste this code in the UserControl class:

protected override CreateParams CreateParams {
  get {
    var parms = base.CreateParams;
    parms.Style &= ~0x02000000;  // Turn off WS_CLIPCHILDREN
    return parms;
  }
}

这不会使绘画更快,并且可能会有副作用.如果仍然存在问题,则需要使BackgroundImage绘制得更快.将其预缩放为用户控件的客户端大小,以便可以绘制它而无需重新缩放.对位图使用PixelFormat.Format32bppPArgb格式,它比大多数视频适配器上的任何其他格式快约10倍.

This doesn't make the painting any faster and may have side-effects. If that is still a problem then you need to make the BackgroundImage draw faster. Prescale it to the client size of the user control so it can be drawn without rescaling. Use the PixelFormat.Format32bppPArgb format for the bitmap, it is about 10x faster than any other one on most video adapters.

这篇关于WinForms用户控件上的标签背景轻拂已启用背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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