从C#注册自定义Win32窗口类
我有一个用WPF编写的新应用程序,该应用程序需要支持旧的API,该API允许它接收已发布到隐藏窗口的消息。通常,另一个应用程序使用FindWindow通过其自定义窗口类的名称来标识隐藏的窗口。
1)我假设要实现一个自定义窗口类,我需要使用老式的win32调用?
我的旧c ++应用程序使用RegisterClass和CreateWindow制作了尽可能简单的不可见窗口。
我相信我应该能够在c#中完成所有操作。我不希望我的项目必须编译任何非托管代码。
我尝试从System.Windows.Interop.HwndHost继承,并使用System.Runtime.InteropServices.DllImport引入上述API方法。
这样做我可以成功托管一个标准的win32窗口,例如WPF中的"列表框"。
但是,当我为自定义窗口调用CreateWindowEx时,它始终返回null。
我对RegisterClass的调用成功,但是我不确定应该设置什么
WNDCLASS.lpfnWndProc成员。
2)有人知道如何成功执行此操作吗?
解决方案
1)我们可以只继承常规Windows Forms类...不需要所有这些win32调用,我们只需要手动解析WndProc消息即可。
2)我们可以导入System.Windows.Forms命名空间并将其与WPF一起使用,我相信只要我们不将太多的Windows表单交织到WPF应用程序中就不会有任何问题。我们只想实例化自定义的隐藏表单以接收消息,对吗?
WndProc子类化的示例:
protected override void WndProc(ref System.Windows.Forms.Message m) { // *always* let the base class process the message base.WndProc(ref m); const int WM_NCHITTEST = 0x84; const int HTCAPTION = 2; const int HTCLIENT = 1; // if Windows is querying where the mouse is and the base form class said // it's on the client area, let's cheat and say it's on the title bar instead if ( m.Msg == WM_NCHITTEST && m.Result.ToInt32() == HTCLIENT ) m.Result = new IntPtr(HTCAPTION); }
既然我们已经知道RegisterClass和所有这些Win32调用,那么我认为WndProc消息对我们来说将不是问题...
作为记录,我终于使它起作用了。
原来我遇到的困难归结为编组问题。
在导入win32函数时,我必须更加精确。
下面的代码将创建一个自定义窗口类,用于支持我们可能依赖于自定义窗口类的旧API。
只要消息泵在线程上运行,它就可以在WPF或者Winforms中工作。
编辑:
更新以修复由于早期收集包装回调的委托而导致的崩溃报告。现在,该委托被保留为成员,并且该委托被显式编组为函数指针。这可以解决此问题,并使我们更容易理解其行为。
class CustomWindow : IDisposable { delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); [System.Runtime.InteropServices.StructLayout( System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Unicode )] struct WNDCLASS { public uint style; public IntPtr lpfnWndProc; public int cbClsExtra; public int cbWndExtra; public IntPtr hInstance; public IntPtr hIcon; public IntPtr hCursor; public IntPtr hbrBackground; [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] public string lpszMenuName; [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] public string lpszClassName; } [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] static extern System.UInt16 RegisterClassW( [System.Runtime.InteropServices.In] ref WNDCLASS lpWndClass ); [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] static extern IntPtr CreateWindowExW( UInt32 dwExStyle, [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpClassName, [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpWindowName, UInt32 dwStyle, Int32 x, Int32 y, Int32 nWidth, Int32 nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam ); [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] static extern System.IntPtr DefWindowProcW( IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam ); [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] static extern bool DestroyWindow( IntPtr hWnd ); private const int ERROR_CLASS_ALREADY_EXISTS = 1410; private bool m_disposed; private IntPtr m_hwnd; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (!m_disposed) { if (disposing) { // Dispose managed resources } // Dispose unmanaged resources if (m_hwnd != IntPtr.Zero) { DestroyWindow(m_hwnd); m_hwnd = IntPtr.Zero; } } } public CustomWindow(string class_name){ if (class_name == null) throw new System.Exception("class_name is null"); if (class_name == String.Empty) throw new System.Exception("class_name is empty"); m_wnd_proc_delegate = CustomWndProc; // Create WNDCLASS WNDCLASS wind_class = new WNDCLASS(); wind_class.lpszClassName = class_name; wind_class.lpfnWndProc = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(m_wnd_proc_delegate); UInt16 class_atom = RegisterClassW(ref wind_class); int last_error = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if (class_atom == 0 && last_error != ERROR_CLASS_ALREADY_EXISTS) { throw new System.Exception("Could not register window class"); } // Create window m_hwnd = CreateWindowExW( 0, class_name, String.Empty, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero ); } private static IntPtr CustomWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) { return DefWindowProcW(hWnd, msg, wParam, lParam); } private WndProc m_wnd_proc_delegate; }