带有 TransparencyKey 的 WS_EX_TOOLWINDOW 导致 Win32Exception [英] WS_EX_TOOLWINDOW with TransparencyKey causes Win32Exception
问题描述
我正在尝试创建一个空的表单窗口,但使用的是工具窗口样式.但是,调用 Show()
会导致以下异常:
I'm trying to have an empty form window, but using the tool window style. However, calling Show()
results in the following exception:
Win32Exception: 参数不正确.
Win32Exception: The parameter is incorrect.
本机错误代码:87
在 System.Windows.Forms.Form.UpdateLayered() 在System.Windows.Forms.Control.WmCreate(Message& m) atSystem.Windows.Forms.Control.WndProc(Message& m) atSystem.Windows.Forms.Form.WmCreate(Message& m) atSystem.Windows.Forms.Form.WndProc(Message& m) atSystem.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Form.UpdateLayered() at System.Windows.Forms.Control.WmCreate(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Form.WmCreate(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
错误代码 87 是 ERROR_INVALID_PARAMETER.
Error code 87 is 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;
}
}
推荐答案
首先尝试使用 OR 赋值而不是普通赋值:
First try using an OR-assignment instead of plain assignment:
cp.ExStyle |= WS_EX_TOOLWINDOW;
如果这不起作用,您可以尝试另外对这些相关样式进行 OR 运算:
If that doesn't work, you can try additionally OR'ing some of these related styles:
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;
行.
The WS_EX_TRANSPARENT
flag may possibly allow the transparency you want without requiring the TransparencyKey = BackColor;
line.
这篇关于带有 TransparencyKey 的 WS_EX_TOOLWINDOW 导致 Win32Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!