FormEx.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Windows.Forms;
  3. namespace SmartCoalApplication.SystemLayer
  4. {
  5. public sealed class FormEx : Control
  6. {
  7. private Form host;
  8. private RealParentWndProcDelegate realParentWndProc;
  9. private bool forceActiveTitleBar = false;
  10. public bool ForceActiveTitleBar
  11. {
  12. get
  13. {
  14. return this.forceActiveTitleBar;
  15. }
  16. set
  17. {
  18. this.forceActiveTitleBar = value;
  19. }
  20. }
  21. public FormEx(Form host, RealParentWndProcDelegate realParentWndProc)
  22. {
  23. this.host = host;
  24. this.realParentWndProc = realParentWndProc;
  25. }
  26. public class ProcessCmdKeyEventArgs : EventArgs
  27. {
  28. private bool handled;
  29. public bool Handled
  30. {
  31. get
  32. {
  33. return this.handled;
  34. }
  35. set
  36. {
  37. this.handled = value;
  38. }
  39. }
  40. private Keys keyData;
  41. public Keys KeyData
  42. {
  43. get
  44. {
  45. return this.keyData;
  46. }
  47. }
  48. public ProcessCmdKeyEventArgs(Keys keyData, bool handled)
  49. {
  50. this.keyData = keyData;
  51. this.handled = handled;
  52. }
  53. }
  54. public event EventHandler<ProcessCmdKeyEventArgs> ProcessCmdKeyRelay;
  55. public bool RelayProcessCmdKey(Keys keyData)
  56. {
  57. bool handled = false;
  58. if (ProcessCmdKeyRelay != null)
  59. {
  60. ProcessCmdKeyEventArgs e = new ProcessCmdKeyEventArgs(keyData, false);
  61. ProcessCmdKeyRelay(this, e);
  62. handled = e.Handled;
  63. }
  64. return handled;
  65. }
  66. internal static FormEx FindFormEx(Form host)
  67. {
  68. if (host != null)
  69. {
  70. Control.ControlCollection controls = host.Controls;
  71. for (int i = 0; i < controls.Count; ++i)
  72. {
  73. FormEx formEx = controls[i] as FormEx;
  74. if (formEx != null)
  75. {
  76. return formEx;
  77. }
  78. }
  79. }
  80. return null;
  81. }
  82. private int ignoreNcActivate = 0;
  83. /// <summary>
  84. /// Manages some special handling of window messages.
  85. /// </summary>
  86. /// <param name="mess"></param>
  87. /// <returns>true if the message was handled, false if the caller should handle the message.</returns>
  88. public bool HandleParentWndProc(ref Message mess)
  89. {
  90. bool returnVal = true;
  91. switch (mess.Msg)
  92. {
  93. case NativeConstants.WM_NCPAINT:
  94. goto default;
  95. case NativeConstants.WM_NCACTIVATE:
  96. if (this.forceActiveTitleBar && mess.WParam == IntPtr.Zero)
  97. {
  98. if (ignoreNcActivate > 0)
  99. {
  100. --ignoreNcActivate;
  101. goto default;
  102. }
  103. else if (Form.ActiveForm != this.host || // Gets rid of: if you have the form active, then click on the desktop --> desktop refreshes
  104. !this.host.Visible) // Gets rid of: desktop refresh on exit
  105. {
  106. goto default;
  107. }
  108. else
  109. {
  110. bool locked = false;
  111. if (this.host.Owner == null &&
  112. this.host.WindowState != FormWindowState.Minimized)
  113. {
  114. locked = true;
  115. }
  116. this.realParentWndProc(ref mess);
  117. SafeNativeMethods.SendMessageW(this.host.Handle, NativeConstants.WM_NCACTIVATE,
  118. new IntPtr(1), IntPtr.Zero);
  119. if (locked)
  120. {
  121. //UI.SetControlRedraw(this.host, true);
  122. //this.host.Invalidate(true);
  123. }
  124. break;
  125. }
  126. }
  127. else
  128. {
  129. goto default;
  130. }
  131. case NativeConstants.WM_ACTIVATE:
  132. goto default;
  133. case NativeConstants.WM_ACTIVATEAPP:
  134. this.realParentWndProc(ref mess);
  135. // Check if the app is being deactivated
  136. if (this.forceActiveTitleBar && mess.WParam == IntPtr.Zero)
  137. {
  138. // If so, put our titlebar in the inactive state
  139. SafeNativeMethods.PostMessageW(this.host.Handle, NativeConstants.WM_NCACTIVATE,
  140. IntPtr.Zero, IntPtr.Zero);
  141. ++ignoreNcActivate;
  142. }
  143. if (mess.WParam == new IntPtr(1))
  144. {
  145. foreach (Form childForm in this.host.OwnedForms)
  146. {
  147. FormEx childFormEx = FindFormEx(childForm);
  148. if (childFormEx != null)
  149. {
  150. if (childFormEx.ForceActiveTitleBar && childForm.IsHandleCreated)
  151. {
  152. SafeNativeMethods.PostMessageW(childForm.Handle, NativeConstants.WM_NCACTIVATE,
  153. new IntPtr(1), IntPtr.Zero);
  154. }
  155. }
  156. }
  157. FormEx ownerEx = FindFormEx(this.host.Owner);
  158. if (ownerEx != null)
  159. {
  160. if (ownerEx.ForceActiveTitleBar && this.host.Owner.IsHandleCreated)
  161. {
  162. SafeNativeMethods.PostMessageW(this.host.Owner.Handle, NativeConstants.WM_NCACTIVATE,
  163. new IntPtr(1), IntPtr.Zero);
  164. }
  165. }
  166. }
  167. break;
  168. default:
  169. returnVal = false;
  170. break;
  171. }
  172. GC.KeepAlive(this.host);
  173. return returnVal;
  174. }
  175. }
  176. }