diff --git a/PInvoke/User32/BasicMessageWindow.cs b/PInvoke/User32/BasicMessageWindow.cs index 7f8fab54..264408f9 100644 --- a/PInvoke/User32/BasicMessageWindow.cs +++ b/PInvoke/User32/BasicMessageWindow.cs @@ -5,6 +5,19 @@ using static Vanara.PInvoke.User32; namespace Vanara.PInvoke { + /// A filter method that handles messages sent to a window. + /// A handle to the window. + /// The MSG. + /// Additional message information. The contents of this parameter depend on the value of the uMsg parameter. + /// Additional message information. The contents of this parameter depend on the value of the uMsg parameter. + /// The return value is the result of the message processing and depends on the message sent. + /// + /// if the message is handled and should not be called; + /// otherwise. + /// + [UnmanagedFunctionPointer(CallingConvention.Winapi)] + public delegate bool BasicMessageWindowFilter(HWND hwnd, uint msg, IntPtr wParam, IntPtr lParam, out IntPtr lReturn); + /// Simple window to process messages. /// /// @@ -15,12 +28,10 @@ namespace Vanara.PInvoke private bool isDisposed; /// Initializes a new instance of the class. - /// - /// Specifies the callback method to use to process messages. A value will just use DefWindowProc. - /// - public BasicMessageWindow(WindowProc callback = null) + /// Specifies the callback method to use to process messages. + public BasicMessageWindow(BasicMessageWindowFilter callback = null) { - Callback = callback; + MessageFilter = callback; ClassName = $"MessageWindowBase+{Guid.NewGuid()}"; hwnd = CreateWindow(); @@ -29,10 +40,6 @@ namespace Vanara.PInvoke /// Finalizes an instance of the class. ~BasicMessageWindow() => Dispose(false); - /// Gets or sets the callback method used to filter window messages. - /// The callback method. - public WindowProc Callback { get; set; } - /// Gets the name of the class. /// The name of the class. public string ClassName { get; private set; } @@ -41,6 +48,10 @@ namespace Vanara.PInvoke /// The handle. public HWND Handle => hwnd; + /// Gets or sets the callback method used to filter window messages. + /// The callback method. + public BasicMessageWindowFilter MessageFilter { get; set; } + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { @@ -81,7 +92,11 @@ namespace Vanara.PInvoke private IntPtr WndProc(HWND hwnd, uint msg, IntPtr wParam, IntPtr lParam) { - var ret = (Callback ?? DefWindowProc).Invoke(hwnd, msg, wParam, lParam); + IntPtr ret; + if (!(MessageFilter is null) && MessageFilter.Invoke(hwnd, msg, wParam, lParam, out var lRet)) + ret = lRet; + else + ret = DefWindowProc(hwnd, msg, wParam, lParam); if (msg == (uint)WindowMessage.WM_NCDESTROY) Dispose(true); diff --git a/UnitTests/PInvoke/User32/User32Tests.cs b/UnitTests/PInvoke/User32/User32Tests.cs index 1a17fb44..478bd40b 100644 --- a/UnitTests/PInvoke/User32/User32Tests.cs +++ b/UnitTests/PInvoke/User32/User32Tests.cs @@ -36,11 +36,12 @@ namespace Vanara.PInvoke.Tests timer.Stop(); Assert.True(gotMsg); - IntPtr meth(HWND hwnd, uint uMsg, IntPtr wParam, IntPtr lParam) + bool meth(HWND hwnd, uint uMsg, IntPtr wParam, IntPtr lParam, out IntPtr lReturn) { + lReturn = default; TestContext.WriteLine($"{timer.ElapsedMilliseconds} Message: {(WindowMessage)uMsg} ({uMsg})"); gotMsg = true; - return DefWindowProc(hwnd, uMsg, wParam, lParam); + return false; } }