label backgrond flicking on a WinForms user control has background image enabled

孤街浪徒 提交于 2019-12-18 07:15:56

问题


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.

And I tried

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

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


回答1:


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.

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;
  }
}

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.



来源:https://stackoverflow.com/questions/2454111/label-backgrond-flicking-on-a-winforms-user-control-has-background-image-enabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!