A class that lets you listen to window messages for a given window, forked from dotMorten/WinUIEx.Messaging.WindowMessageMonitor.
See full documentation and API reference at sibber5.github.io/Sibber.WindowMessageMonitor.
Caution
You must create and dispose of the WindowMessageReceived on the same thread as the window it would monitor was created on (regardless if it was created via the constructor or with CreateWithMessageOnlyWindow).
Make sure to dispose of it when you are done using it. Not doing so may cause problems such as memory leaks.
If you have an existing window:
var monitor = new WindowMessageMonitor(windowHandle);
monitor.WindowMessageReceived += (object sender, ref WindowMessageEventArgs e) =>
{
Debug.WriteLine($"Recieved message: {e.MessageType} with wParam: {e.Message.WParam} and LParam: {e.Message.LParam}");
// set e.Handled to true to return e.Result from the window procedure
// (keep in mind that `WindowMessageEventArgs` is a readonly struct that is passed by ref,
// so we set the parameter `e` to a new value to modify Handled and Result)
e = e with { Handled = true, Result = 0 };
}Or you can create a message-only window:
var monitor = WindowMessageMonitor.CreateWithMessageOnlyWindow();Make sure to dispose when you're done using the instance:
monitor.Dispose();The interface IWindowMessageMonitor is also provided in case you want to provide custom implementations, useful for things like other libraries that use this library.
The file src/WindowMessageMonitor.cs was taken from dotMorten/WinUIEx, MIT License - Copyright (c) 2021 Morten Nielsen. See the license notice at the top of the file for more info.
The rest of the library/repository is licensed under the MIT License - see LICENSE - unless otherwise stated in specific files or sections. See individual files for exceptions.