MessageWindow.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Runtime.InteropServices;
  5. using System.Windows;
  6. using System.Windows.Threading;
  7. namespace Standard;
  8. internal sealed class MessageWindow : DispatcherObject, IDisposable
  9. {
  10. public IntPtr Handle { get; private set; }
  11. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  12. public MessageWindow(CS classStyle, WS style, WS_EX exStyle, Rect location, string name, WndProc callback)
  13. {
  14. this._wndProcCallback = callback;
  15. this._className = "MessageWindowClass+" + Guid.NewGuid().ToString();
  16. WNDCLASSEX wndclassex = new WNDCLASSEX
  17. {
  18. cbSize = Marshal.SizeOf(typeof(WNDCLASSEX)),
  19. style = classStyle,
  20. lpfnWndProc = MessageWindow.s_WndProc,
  21. hInstance = NativeMethods.GetModuleHandle(null),
  22. hbrBackground = NativeMethods.GetStockObject(StockObject.NULL_BRUSH),
  23. lpszMenuName = "",
  24. lpszClassName = this._className
  25. };
  26. NativeMethods.RegisterClassEx(ref wndclassex);
  27. GCHandle value = default(GCHandle);
  28. try
  29. {
  30. value = GCHandle.Alloc(this);
  31. IntPtr lpParam = (IntPtr) value;
  32. this.Handle = NativeMethods.CreateWindowEx(exStyle, this._className, name, style, (int) location.X, (int) location.Y, (int) location.Width, (int) location.Height, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, lpParam);
  33. }
  34. finally
  35. {
  36. value.Free();
  37. }
  38. }
  39. ~MessageWindow()
  40. {
  41. this._Dispose(false, false);
  42. }
  43. public void Dispose()
  44. {
  45. this._Dispose(true, false);
  46. GC.SuppressFinalize(this);
  47. }
  48. [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "disposing")]
  49. private void _Dispose(bool disposing, bool isHwndBeingDestroyed)
  50. {
  51. if (this._isDisposed)
  52. {
  53. return;
  54. }
  55. this._isDisposed = true;
  56. IntPtr hwnd = this.Handle;
  57. string className = this._className;
  58. if (isHwndBeingDestroyed)
  59. {
  60. base.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new DispatcherOperationCallback((object arg) => MessageWindow._DestroyWindow(IntPtr.Zero, className)));
  61. }
  62. else if (this.Handle != IntPtr.Zero)
  63. {
  64. if (base.CheckAccess())
  65. {
  66. MessageWindow._DestroyWindow(hwnd, className);
  67. }
  68. else
  69. {
  70. base.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new DispatcherOperationCallback((object arg) => MessageWindow._DestroyWindow(hwnd, className)));
  71. }
  72. }
  73. MessageWindow.s_windowLookup.Remove(hwnd);
  74. this._className = null;
  75. this.Handle = IntPtr.Zero;
  76. }
  77. [SuppressMessage("Microsoft.Usage", "CA1816:CallGCSuppressFinalizeCorrectly")]
  78. private static IntPtr _WndProc(IntPtr hwnd, WM msg, IntPtr wParam, IntPtr lParam)
  79. {
  80. IntPtr result = IntPtr.Zero;
  81. MessageWindow messageWindow = null;
  82. if (msg == WM.CREATE)
  83. {
  84. messageWindow = (MessageWindow) GCHandle.FromIntPtr(((CREATESTRUCT) Marshal.PtrToStructure(lParam, typeof(CREATESTRUCT))).lpCreateParams).Target;
  85. MessageWindow.s_windowLookup.Add(hwnd, messageWindow);
  86. }
  87. else if (!MessageWindow.s_windowLookup.TryGetValue(hwnd, out messageWindow))
  88. {
  89. return NativeMethods.DefWindowProc(hwnd, msg, wParam, lParam);
  90. }
  91. WndProc wndProcCallback = messageWindow._wndProcCallback;
  92. if (wndProcCallback != null)
  93. {
  94. result = wndProcCallback(hwnd, msg, wParam, lParam);
  95. }
  96. else
  97. {
  98. result = NativeMethods.DefWindowProc(hwnd, msg, wParam, lParam);
  99. }
  100. if (msg == WM.NCDESTROY)
  101. {
  102. messageWindow._Dispose(true, true);
  103. GC.SuppressFinalize(messageWindow);
  104. }
  105. return result;
  106. }
  107. private static object _DestroyWindow(IntPtr hwnd, string className)
  108. {
  109. Utility.SafeDestroyWindow(ref hwnd);
  110. NativeMethods.UnregisterClass(className, NativeMethods.GetModuleHandle(null));
  111. return null;
  112. }
  113. private static readonly WndProc s_WndProc = new WndProc(MessageWindow._WndProc);
  114. private static readonly Dictionary<IntPtr, MessageWindow> s_windowLookup = new Dictionary<IntPtr, MessageWindow>();
  115. private WndProc _wndProcCallback;
  116. private string _className;
  117. private bool _isDisposed;
  118. }