C# 带有透明键的WS_EX_TOOLWINDOW会导致Win32Exception

C# 带有透明键的WS_EX_TOOLWINDOW会导致Win32Exception,c#,winforms,transparency,C#,Winforms,Transparency,我试图创建一个空的窗体窗口,但使用的是工具窗口样式。但是,调用Show()会导致以下异常: Win32异常:参数不正确 国家错误代码:87 位于System.Windows.Forms.Form.UpdateLayered()的 System.Windows.Forms.Control.WmCreate(Message&m)位于 System.Windows.Forms.Control.WndProc(Message&m)位于 System.Windows.Forms.Form.WmCreate

我试图创建一个空的窗体窗口,但使用的是工具窗口样式。但是,调用
Show()
会导致以下异常:

Win32异常:参数不正确

国家错误代码:87

位于System.Windows.Forms.Form.UpdateLayered()的 System.Windows.Forms.Control.WmCreate(Message&m)位于 System.Windows.Forms.Control.WndProc(Message&m)位于 System.Windows.Forms.Form.WmCreate(Message&m)位于 System.Windows.Forms.Form.WndProc(Message&m)位于 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg、IntPtr wparam、IntPtr lparam)

错误代码87是错误\无效\参数

private class ToolForm : Form {
    public ToolForm() {
        AllowTransparency = true;
        BackColor = System.Drawing.Color.FromArgb(0, 0, 1);
        TransparencyKey = BackColor;
    }

    private const int WS_EX_TOOLWINDOW = 0x00000080;
    protected override CreateParams CreateParams {
        get {
            var cp = base.CreateParams;
            cp.ExStyle = WS_EX_TOOLWINDOW;
            return cp;
        }
    }
}
编辑:

这项工作:

public class ToolForm : Form {
    public ToolForm() {
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
        this.AllowTransparency = true;
        this.BackColor = Color.FromArgb(0, 0, 1);
        this.TransparencyKey = this.BackColor;
    }
}

首先尝试使用OR赋值而不是普通赋值:

cp.ExStyle |= WS_EX_TOOLWINDOW;
如果不起作用,您可以尝试添加或使用以下相关样式:

cp.ExStyle |= ( int )(
  WS_EX_LAYERED |
  WS_EX_TRANSPARENT |
  WS_EX_NOACTIVATE |
  WS_EX_TOOLWINDOW );
相关值为:

WS_EX_LAYERED = 0x00080000,
WS_EX_NOACTIVATE = 0x08000000,
WS_EX_TOOLWINDOW = 0x00000080,
WS_EX_TRANSPARENT = 0x00000020

WS_EX_TRANSPARENT
标志可能允许您想要的透明度,而不需要
TransparencyKey=BackColor行。

这是错误的代码,设置TransparencyKey属性将打开WS_EX_分层样式位。您可以再次重置它。不清楚为什么不直接使用FormBorderStyle属性。谢谢Hans,我忘记了该属性。