半透明面板显示下方的控件
我有一个自定义控件,用作橡皮筋,绘制蓝色边框和半透明中间。我正在处理鼠标按下/移动事件来调整面板大小。当调用鼠标移动事件时,一切似乎都工作正常,一切都按照我的预期进行绘制,但是当鼠标停止移动时,半透明下方的某些自定义控件将在顶部重新绘制自身。玩弄 z 顺序没有任何作用。
这是透明面板:
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
return cp;
}
}
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 101, 135, 196)), this.ClientRectangle);
pe.Graphics.DrawRectangle(Pens.DarkBlue,
pe.ClipRectangle.Left,
pe.ClipRectangle.Top,
this.Width - 1,
this.Height - 1);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//do not allow the background to be painted
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
}
I have a custom control that I'm using as a rubber band which paints a blue border and a semi-transparent middle. I'm handling the mouse down / move events to resize the panel. Everything seems to work fine when the mouse move event is called, it all draws as I'd expect, but when the mouse stops moving, certain custom controls below the semi-transparency are redrawing themselves on top. Playing around with the z-order doesn't do anything.
Here is the transparent panel:
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
return cp;
}
}
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 101, 135, 196)), this.ClientRectangle);
pe.Graphics.DrawRectangle(Pens.DarkBlue,
pe.ClipRectangle.Left,
pe.ClipRectangle.Top,
this.Width - 1,
this.Height - 1);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//do not allow the background to be painted
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以,事实证明这是一个相当困难的半透明问题(即使微软已经在资源管理器等中做到了)。最后我想出了一个解决方案,其中中间现在完全透明,橡皮筋只是一个边框。
边框仍然在某些控件下方绘制,因此对于每个控件,我们在其 Paint 事件中添加一条语句,如果 ruberBand 可见,则调用 ruberBand.Invalidate() 。嘿,快点,一根拼凑在一起的橡皮筋,将自己拉到控件上方。
IMO 这仍然比使用
DrawReversibleRectangle
更好,因为您可以在面板内使用它 在边缘自动滚动,而且最重要的是它不会闪烁。So, it turns out this is a pretty difficult problem with the semi-transparency (even if Microsoft have done it in Explorer etc). In the end I hacked up a solution where the middle is now totally transparent, and the rubber band is just a border.
The border still draws underneath certain controls, so for each control, we add a statement in their Paint event that calls
ruberBand.Invalidate()
if the rubberBand is visible. Hey presto, a hacked together rubber band that draws itself above controls.IMO this is still better than using
DrawReversibleRectangle
, because you can use it inside panels for autoscrolling at the edge, and on top of that it doesn't flicker.