Sibber.WindowMessageMonitor
A class that lets you listen to window messages for a given window, forked from dotMorten/WinUIEx.Messaging.WindowMessageMonitor.
Usage
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 };
}
Caution
You must subscribe to WindowMessageReceived
on the same thread as the window that this instance monitors.
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();
Caution
If the monitor was created with CreateWithMessageOnlyWindow()
then it must be disposed on the same thread it was created on and in the same executing assembly.
The interface IWindowMessageMonitor
is also provided in case you want to provide custom implementations, useful for things like other libraries that use this library.
License
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.