2020-12-18 19:49:56 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using static Vanara.PInvoke.Kernel32;
|
|
|
|
|
using static Vanara.PInvoke.User32;
|
|
|
|
|
|
|
|
|
|
namespace Vanara.PInvoke
|
|
|
|
|
{
|
|
|
|
|
/// <summary>Simple window to process messages.</summary>
|
|
|
|
|
/// <seealso cref="System.MarshalByRefObject"/>
|
|
|
|
|
/// <seealso cref="System.IDisposable"/>
|
|
|
|
|
/// <seealso cref="Vanara.PInvoke.IHandle"/>
|
|
|
|
|
public class BasicMessageWindow : MarshalByRefObject, IDisposable, IHandle
|
|
|
|
|
{
|
2020-12-20 10:29:09 -05:00
|
|
|
|
private SafeHWND hwnd;
|
2020-12-18 19:49:56 -05:00
|
|
|
|
private bool isDisposed;
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="BasicMessageWindow"/> class.</summary>
|
|
|
|
|
/// <param name="callback">
|
|
|
|
|
/// Specifies the callback method to use to process messages. A <see langword="null"/> value will just use <c>DefWindowProc</c>.
|
|
|
|
|
/// </param>
|
|
|
|
|
public BasicMessageWindow(WindowProc callback = null)
|
|
|
|
|
{
|
|
|
|
|
Callback = callback;
|
2020-12-20 10:29:09 -05:00
|
|
|
|
ClassName = $"MessageWindowBase+{Guid.NewGuid()}";
|
2020-12-18 19:49:56 -05:00
|
|
|
|
|
2020-12-20 10:29:09 -05:00
|
|
|
|
hwnd = CreateWindow();
|
2020-12-18 19:49:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Finalizes an instance of the <see cref="BasicMessageWindow"/> class.</summary>
|
2020-12-20 10:29:09 -05:00
|
|
|
|
~BasicMessageWindow() => Dispose(false);
|
2020-12-18 19:49:56 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>Gets or sets the callback method used to filter window messages.</summary>
|
|
|
|
|
/// <value>The callback method.</value>
|
|
|
|
|
public WindowProc Callback { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets the name of the class.</summary>
|
|
|
|
|
/// <value>The name of the class.</value>
|
|
|
|
|
public string ClassName { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets the handle.</summary>
|
|
|
|
|
/// <value>The handle.</value>
|
2020-12-20 10:29:09 -05:00
|
|
|
|
public HWND Handle => hwnd;
|
2020-12-18 19:49:56 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
System.GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 10:29:09 -05:00
|
|
|
|
/// <summary>Returns the value of the handle field.</summary>
|
|
|
|
|
/// <returns>An IntPtr representing the value of the handle field.</returns>
|
|
|
|
|
IntPtr IHandle.DangerousGetHandle() => (IntPtr)Handle;
|
|
|
|
|
|
2020-12-18 19:49:56 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Method used to create the window. When overriding, the <c>hWnd</c> field must be set to the handle of the created window.
|
|
|
|
|
/// </summary>
|
2020-12-20 10:29:09 -05:00
|
|
|
|
protected virtual SafeHWND CreateWindow()
|
2020-12-18 19:49:56 -05:00
|
|
|
|
{
|
2020-12-20 10:29:09 -05:00
|
|
|
|
if (0 == RegisterClassEx(new WNDCLASSEX { cbSize = (uint)Marshal.SizeOf(typeof(WNDCLASSEX)), lpfnWndProc = WndProc, hInstance = GetModuleHandle(), lpszClassName = ClassName }))
|
|
|
|
|
Win32Error.ThrowLastError();
|
|
|
|
|
return Win32Error.ThrowLastErrorIfInvalid(CreateWindowEx(lpClassName: ClassName, lpWindowName: ClassName, hWndParent: HWND.HWND_MESSAGE));
|
2020-12-18 19:49:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Releases unmanaged and - optionally - managed resources.</summary>
|
|
|
|
|
/// <param name="disposing">
|
|
|
|
|
/// <see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.
|
|
|
|
|
/// </param>
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (isDisposed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
isDisposed = true;
|
2020-12-20 10:29:09 -05:00
|
|
|
|
|
|
|
|
|
hwnd?.Dispose();
|
|
|
|
|
|
2020-12-18 19:49:56 -05:00
|
|
|
|
UnregisterClass(ClassName, GetModuleHandle());
|
|
|
|
|
ClassName = null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 10:29:09 -05:00
|
|
|
|
private IntPtr WndProc(HWND hwnd, uint msg, IntPtr wParam, IntPtr lParam)
|
2020-12-18 19:49:56 -05:00
|
|
|
|
{
|
2020-12-20 10:29:09 -05:00
|
|
|
|
var ret = (Callback ?? DefWindowProc).Invoke(hwnd, msg, wParam, lParam);
|
2020-12-18 19:49:56 -05:00
|
|
|
|
|
|
|
|
|
if (msg == (uint)WindowMessage.WM_NCDESTROY)
|
2020-12-20 10:29:09 -05:00
|
|
|
|
Dispose(true);
|
2020-12-18 19:49:56 -05:00
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|