【发布时间】:2012-08-17 12:35:55
【问题描述】:
我创建了一个简单的自定义面板,使用ContainerControl 作为我的基础。我添加了自定义属性来创建边框和渐变背景。如果我覆盖 OnPaint 和 OnPaintBackground,则父级的所有子控件都会继承渐变和边框样式。作为一种解决方法,我使用了父母 BackgroundImage 属性,它工作正常,但有一些随机的怪癖。必须有更好的方法来解决这个问题,但我没有找到解决方案。是否有任何通过 Interop 或其他 C# 方法来解决此问题的 Window API 函数?如果有,请举个例子。
编辑!这是被复制的样式(丑陋的例子,但很重要):
编辑 2! 这是一个简单的硬编码 ContainerControl,没有所有属性、设计器属性等。
public class Container : ContainerControl
{
protected override void OnPaintBackground( PaintEventArgs e )
{
using ( var brush = new LinearGradientBrush( e.ClipRectangle, Color.Red, Color.Blue, LinearGradientMode.Vertical ) )
{
e.Graphics.FillRectangle( brush, e.ClipRectangle );
}
}
}
【问题讨论】:
-
有趣,我认为这不应该发生。你能发布你的
OnPaint()和OnPaintBackground()覆盖吗? -
label2是标准的Label控件还是其他自定义控件? -
您是在将子控件拖动/添加到容器后,还是在将子控件的背景设置为透明后立即在子控件上获得这种效果?
标签: c# winforms custom-controls containers onpaint