private static int WndProc(IntPtr hwnd, uint msg, IntPtr wparam, IntPtr lparam) { switch (msg) { case CreateWindowMessage: lock (_crossThreadWindowLock) { _tempHandle = CreateWindow(); Monitor.Pulse(_crossThreadWindowLock); } break; case (uint)WindowMessage.Destroy: break; default: MessageListener listener; if (_listeners.TryGetValue(hwnd, out listener)) { Message message = new Message(hwnd, msg, wparam, lparam, 0, new NativePoint()); listener.MessageReceived.SafeRaise(listener, new WindowMessageEventArgs(message)); } break; } return(ShellObjectWatcherNativeMethods.DefWindowProc(hwnd, msg, wparam, lparam)); }
private static IntPtr CreateWindow() => ShellObjectWatcherNativeMethods.CreateWindowEx( 0, //extended style MessageWindowClassName, //class name "MessageListenerWindow", //title 0, //style 0, 0, 0, 0, // x,y,width,height new IntPtr(-3), // -3 = Message-Only window IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
private static void RegisterWindowClass() { WindowClassEx classEx = new WindowClassEx(); classEx.ClassName = MessageWindowClassName; classEx.WndProc = wndProc; classEx.Size = (uint)Marshal.SizeOf(typeof(WindowClassEx)); var atom = ShellObjectWatcherNativeMethods.RegisterClassEx(ref classEx); if (atom == 0) { throw new ShellException(LocalizedMessages.MessageListenerClassNotRegistered, Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error())); } _atom = atom; }
private void ThreadMethod() // Message Loop { lock (_crossThreadWindowLock) { _running = true; if (_atom == 0) { RegisterWindowClass(); } WindowHandle = CreateWindow(); Monitor.Pulse(_crossThreadWindowLock); } while (_running) { Message msg; if (ShellObjectWatcherNativeMethods.GetMessage(out msg, IntPtr.Zero, 0, 0)) { ShellObjectWatcherNativeMethods.DispatchMessage(ref msg); } } }