【问题标题】:WS_EX_TOOLWINDOW with TransparencyKey causes Win32Exception带有 TransparencyKey 的 WS_EX_TOOLWINDOW 导致 Win32Exception
【发布时间】:2015-06-24 18:04:09
【问题描述】:

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

Win32Exception: 参数不正确。

NativeErrorCode: 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 是 ERROR_INVALID_PARAMETER。

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;
    }
}

【问题讨论】:

  • 这是错误代码,设置 TransparencyKey 属性会打开 WS_EX_LAYERED 样式位。您的分配再次重置它。不清楚为什么不简单地使用 FormBorderStyle 属性。
  • 谢谢汉斯,我忘了那个属性了。

标签: c# winforms transparency


【解决方案1】:

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

cp.ExStyle |= WS_EX_TOOLWINDOW;

如果这不起作用,您可以尝试对这些相关样式进行额外的 OR'ing:

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; 行。

【讨论】:

    猜你喜欢
    • 2010-11-26
    • 2023-04-02
    • 2010-10-18
    • 2020-08-17
    • 2018-07-26
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多