我需要画我的Windows窗体用户控件?我的、窗体、控件、用户

2023-09-06 06:58:51 作者:且以情深共白首

我有一个包含定制和标准控制的混合物自定义控件。当控制变大,我得到的背景颜色的矩形,其中的子控件将涂上,子控件完成的油漆之前。它同时适用于我的自定义控件和标准的。

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.

我是否需要重写paint方法的子控件?当然,这将意味着新的自定义的控件全部更换为标准的,或者我需要重写paint方法的容器?

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?

推荐答案

编辑,以正确的信息和参考

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.

下面的解决方案由Hans帕桑特提供通常会解决这样的问题,而不会导致一些双缓冲的副作用。

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弹上一个WinForms用户控件的背景图像启用

通过去除WS_CLIPCHILDREN属性的空间的控制都应该占据就会充满背景首先它可以减少你看到等待的文物要绘制控件

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

添加以下code到您的自定义控制:

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