.NET 中的 RegisterClassEx 失败
RegisterClassEx in .NET fails
我正在通过 RegisterClassEx 注册一个 class,但是在这一步之后 window 不再出现。
我是这样注册的:
wcx = new WNDCLASSEX();
wcx.cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(WNDCLASSEX))); // size of structure
wcx.style = ClassStyles.HorizontalRedraw | ClassStyles.VerticalRedraw; // redraw if size changes
wcx.lpfnWndProc = ProcessWndProc; // points to window procedure
wcx.cbClsExtra = 0; // no extra class memory
wcx.cbWndExtra = 0; // no extra window memory
wcx.hInstance = hinstance; // handle to instance
wcx.hIcon = IntPtr.Zero; // predefined app. icon
wcx.hCursor = IntPtr.Zero; // predefined arrow
wcx.hbrBackground = IntPtr.Zero; // white background brush
wcx.lpszMenuName = "MainMenu"; // name of menu resource
wcx.lpszClassName = "EDIT"; // name of window class
wcx.hIconSm = IntPtr.Zero;
// Register the window class.
return RegisterClassEx(ref wcx)!=0;
下面是我如何创建 window
int wndWidth = hwndRect.Right - hwndRect.Left;
int wndHeight = hwndRect.Bottom - hwndRect.Top;
Debug.WriteLine("Coord: " + hwndRect.Left.ToString() + "//" + hwndRect.Top.ToString() + "--" + wndWidth.ToString() + "//" + wndHeight.ToString());
hwnd = User32.CreateWindowEx(
User32.WindowStylesEx.WS_EX_TRANSPARENT
| User32.WindowStylesEx.WS_EX_TOPMOST
| User32.WindowStylesEx.WS_EX_TOOLWINDOW,
wcx.lpszClassName, this.Name, User32.WindowStyles.WS_POPUP | User32.WindowStyles.WS_VISIBLE,
hwndRect.Left, hwndRect.Top, wndWidth, wndHeight, IntPtr.Zero, IntPtr.Zero, wcx.hInstance, IntPtr.Zero
);
if (hwnd == null)
return false;
RegisterClassEx returns 非零值。所以注册可能会成功。那么原因在哪里呢?如果不执行 RegisterClassEx,它工作正常,但我喜欢设置一些类似引用 WndProc 处理程序的东西。
对我有什么提示吗?
"EDIT"
是预定义标准的名称 window class (edit control)。您不能使用它来注册您的自定义 window class,因为 class 名称在进程中必须是唯一的。请改用其他 class 名称。
我正在通过 RegisterClassEx 注册一个 class,但是在这一步之后 window 不再出现。
我是这样注册的:
wcx = new WNDCLASSEX();
wcx.cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(WNDCLASSEX))); // size of structure
wcx.style = ClassStyles.HorizontalRedraw | ClassStyles.VerticalRedraw; // redraw if size changes
wcx.lpfnWndProc = ProcessWndProc; // points to window procedure
wcx.cbClsExtra = 0; // no extra class memory
wcx.cbWndExtra = 0; // no extra window memory
wcx.hInstance = hinstance; // handle to instance
wcx.hIcon = IntPtr.Zero; // predefined app. icon
wcx.hCursor = IntPtr.Zero; // predefined arrow
wcx.hbrBackground = IntPtr.Zero; // white background brush
wcx.lpszMenuName = "MainMenu"; // name of menu resource
wcx.lpszClassName = "EDIT"; // name of window class
wcx.hIconSm = IntPtr.Zero;
// Register the window class.
return RegisterClassEx(ref wcx)!=0;
下面是我如何创建 window
int wndWidth = hwndRect.Right - hwndRect.Left;
int wndHeight = hwndRect.Bottom - hwndRect.Top;
Debug.WriteLine("Coord: " + hwndRect.Left.ToString() + "//" + hwndRect.Top.ToString() + "--" + wndWidth.ToString() + "//" + wndHeight.ToString());
hwnd = User32.CreateWindowEx(
User32.WindowStylesEx.WS_EX_TRANSPARENT
| User32.WindowStylesEx.WS_EX_TOPMOST
| User32.WindowStylesEx.WS_EX_TOOLWINDOW,
wcx.lpszClassName, this.Name, User32.WindowStyles.WS_POPUP | User32.WindowStyles.WS_VISIBLE,
hwndRect.Left, hwndRect.Top, wndWidth, wndHeight, IntPtr.Zero, IntPtr.Zero, wcx.hInstance, IntPtr.Zero
);
if (hwnd == null)
return false;
RegisterClassEx returns 非零值。所以注册可能会成功。那么原因在哪里呢?如果不执行 RegisterClassEx,它工作正常,但我喜欢设置一些类似引用 WndProc 处理程序的东西。 对我有什么提示吗?
"EDIT"
是预定义标准的名称 window class (edit control)。您不能使用它来注册您的自定义 window class,因为 class 名称在进程中必须是唯一的。请改用其他 class 名称。