我需要绘制 Windows 窗体用户控件吗?

发布于 2024-10-20 22:09:49 字数 228 浏览 1 评论 0原文

我有一个自定义控件,其中包含自定义控件和标准控件的混合体。当控件变大时,在子控件的绘制完成之前,我会得到背景颜色的矩形,子控件将在其中绘制。我的自定义控件和标准控件都是一样的。

我认为暂停布局是避免这种情况的方法,但它对我不起作用。

在调整大小时,我暂停容器及其所有控件的布局。

我是否需要重写子控件的绘制方法?当然,这意味着新的自定义控件将取代所有标准控件,或者我是否需要重写容器的绘制方法?

I have a custom control containing a mixture of custom and standard controls. When the control is made larger, I'm getting background coloured rectangles where the child control will be painted, before the paint of the child control completes. It's the same for both my custom controls and the standard ones.

I thought that suspend layout was the way to avoid this, but it isn't working for me.

I suspend layout for the container and all its controls while the resizing takes place.

Do I need to override the paint method for the child controls? Surely that would mean new custom controls to replace all the standard ones, or do I need to override the paint method for the container?

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

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

发布评论

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

评论(1

任谁 2024-10-27 22:09:49

编辑以更正信息和参考

一种方法是双缓冲。人们经常设置这种样式并假设它有效,但实际上由于各种原因您可能无法获得双缓冲。但真正的双缓冲也会产生一些负面后果。

Hans Passant 提供的以下解决方案通常可以解决此类问题,而不会引起双缓冲的一些副作用。

标签背景轻弹WinForms 用户控件启用了背景图像

通过删除 WS_CLIPCHILDREN 属性,控件应占据的空间将首先用背景填充,这可以减少您在等待绘制控件时看到的伪影

将以下代码添加到您的自定义控件:

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

EDITED to correct information and reference

One approach is to double buffer. People often set this style and assume it is working but you may not in fact get double buffering for various reasons. But true double buffering can have some negative consequences too.

The following solution provide by Hans Passant will typically fix this sort of problem without causing some of the side effects of double buffering.

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

By removing the WS_CLIPCHILDREN attribute the spaces the controls are supposed to occupy will be filled with background first which can reduce the artifacts you see waiting for the controls to be drawn

Add the following code to your custom control:

protected override CreateParams CreateParams
    {
        get
        {
            CreateParams parms = base.CreateParams;
            parms.Style &= ~0x02000000;  // Turn off WS_CLIPCHILDREN
            return parms;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文