WinForms 用户控件上的标签背景轻拂已启用背景图像

发布于 2024-08-25 06:50:02 字数 409 浏览 9 评论 0原文

我正在开发一个 Windows 窗体项目,并且在 UserControl 双缓冲方面遇到一些问题。 我创建了一个用户控件并有一个背景图像,然后在它上面有几个单选按钮和标签。单选按钮和标签都有透明背景颜色。 但是,当我显示和隐藏用户控件时,我可以看到那些具有透明背景的标签和单选按钮闪烁。

我尝试

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒芜了季节 2024-09-01 06:50:02

这不是双缓冲可以解决的闪烁源。当 UC 重新绘制自身时,它会绘制背景图像,在控件所在的位置留下孔。然后,每个控件都可以绘制自己,首先要求 UC 再次绘制自己以生成背景像素,然后在顶部绘制自己,从而填补漏洞。临时的洞就是你所看到的闪烁。

您可以通过允许 UC 在控件的工作区中绘制自身来减少令人反感的情况,这样背景就已经正确设置了。将此代码粘贴到 UserControl 类中:

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

这不会使绘制速度更快,并且可能会产生副作用。如果这仍然是一个问题,那么您需要使背景图像绘制速度更快。将其预先缩放为用户控件的客户端大小,以便无需重新缩放即可绘制它。使用 PixelFormat.Format32bppPArgb 格式作为位图,它比大多数视频适配器上的任何其他格式快大约 10 倍。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文