具有透明子问题的 Windows 窗体自定义面板
我创建了一个自定义面板(继承自 Panel),它重写 OnPaint 方法以使用 LinearGradientBrush 绘制内部矩形。
public void PaintPanel()
{
// Re-calculate the
CalculatePanelHeight();
Graphics _g = this.CreateGraphics();
Point _startPoint = new Point(0, m_TopAreaHeight);
Size _size = new Size(Width, m_BtmAreaHeight);
Rectangle _btmRect = new Rectangle(_startPoint, _size);
LinearGradientBrush _btmGradBrush = new LinearGradientBrush(_btmRect, BackColorBottom, BackColorBottom2, LinearGradientMode.Vertical);
_btmGradBrush.GammaCorrection = true;
_g.FillRectangle(_btmGradBrush, _btmRect);
...
}
protected override void OnPaint(PaintEventArgs e)
{
PaintPanel();
base.OnPaint(e);
}
然而,有两个怪癖:
每当任何具有透明背景的控件被拖到自定义面板中时,其背景都会变为白色。
我的自定义面板不支持透明背景(每当我将其中一种渐变颜色设置为透明时,颜色就会变为白色)。
有人可以提供一些见解吗?
谢谢。
I've created a custom panel (inherited from Panel) that override OnPaint method to paint the inner rectangle with LinearGradientBrush.
public void PaintPanel()
{
// Re-calculate the
CalculatePanelHeight();
Graphics _g = this.CreateGraphics();
Point _startPoint = new Point(0, m_TopAreaHeight);
Size _size = new Size(Width, m_BtmAreaHeight);
Rectangle _btmRect = new Rectangle(_startPoint, _size);
LinearGradientBrush _btmGradBrush = new LinearGradientBrush(_btmRect, BackColorBottom, BackColorBottom2, LinearGradientMode.Vertical);
_btmGradBrush.GammaCorrection = true;
_g.FillRectangle(_btmGradBrush, _btmRect);
...
}
protected override void OnPaint(PaintEventArgs e)
{
PaintPanel();
base.OnPaint(e);
}
However there're 2 quirks:
Whenever any control with transparent background is dragged into the custom panel, its background becomes White.
My custom panel doesn't support transparent background (the color turns to White whenever I set one of the gradient color to transparent).
Would anyone offers some insights please?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Windows 窗体中不存在真正的透明度。这是 Windows 限制,子窗口不支持它。有一些解决方法,例如 WS_EX_TRANSPARENT 窗口样式以及 winforms 中内置的一些对透明 BackColor 的支持。它们的工作原理都是要求控件的父级在控件窗口中绘制自身,并提供背景像素。
当您开始重叠控件时,这种情况就会崩溃,您会看到父级(通常是窗体)的背景,而不是重叠的控件。如果表单的背景色是白色,那么您确实会看到白色,而不是中间控件的渐变。
对此没有实际的解决方法。如果您想要真正的透明度,那么您应该考虑 WPF。它不使用窗户,只使用油漆层。透明度现在是微不足道的,只是不要绘制。
True transparency doesn't exist in Windows Forms. It is a Windows restriction, it doesn't support it on child windows. There are a few workaround tricks for it, like the WS_EX_TRANSPARENT window style and some support built into winforms for a BackColor that's transparent. They both work by asking the parent of the control to draw itself in the control window, providing the background pixels.
This breaks down when you start to overlap controls, you see the background of the parent (the form usually), not the overlapped control. And if your form's BackColor is white then you'll indeed see white, not the gradient of the intermediate control.
There's no practical workaround for this. If you want true transparency then you should consider WPF. It doesn't use windows, just layers of paint. Transparency is now trivial, just don't draw.