我有一个项目需要做一层容器。
容器必须具有以下内容:
Form.Opacity = 0;
用户可以看到顶层下的元素,但不能使用它们。
我看到了许多具有透明背景的示例,但以我的方式,我需要在这个容器上移动元素。我发现的更好:
class xPanel : Panel
{
public xPanel()
{
SetStyle(ControlStyles.Opaque, true);
}
protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
return createParams;
}
}
protected override void OnPaint(PaintEventArgs e)
{
//PaintParentBackground(e);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, Color.White)),
0, 0, Width, Height);
}
public void InvalidateEx()
{
if (Parent == null)
return;
Rectangle rc = new Rectangle(this.Location, this.Size);
Parent.Invalidate(rc, true);
}
}
但是在拖动元素时会有痕迹,或者在重绘时会闪烁。
我不知道如何解决这个问题。有想法吗?
我使用 InvalidateEx(),在:
protected override void OnLocationChanged(EventArgs e)
{
if (Parent != null)
((xPanel)Parent).InvalidateEx();
}