FormEx.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Windows.Forms;
  3. namespace PaintDotNet.SystemLayer
  4. {
  5. /// <summary>
  6. /// Provides special methods and properties that must be implemented in a
  7. /// system-specific manner. It is implemented as an object that is hosted
  8. /// by the PdnBaseForm class. This way there is no inheritance hierarchy
  9. /// extending into the SystemLayer assembly.
  10. /// </summary>
  11. public sealed class FormEx : Control
  12. {
  13. private Form host;
  14. private RealParentWndProcDelegate realParentWndProc;
  15. private bool forceActiveTitleBar = false;
  16. /// <summary>
  17. /// Gets or sets the titlebar rendering behavior for when the form is deactivated.
  18. /// </summary>
  19. /// <remarks>
  20. /// If this property is false, the titlebar will be rendered in a different color when the form
  21. /// is inactive as opposed to active. If this property is true, it will always render with the
  22. /// active style. If the whole application is deactivated, the title bar will still be drawn in
  23. /// an inactive state.
  24. /// </remarks>
  25. public bool ForceActiveTitleBar
  26. {
  27. get
  28. {
  29. return this.forceActiveTitleBar;
  30. }
  31. set
  32. {
  33. this.forceActiveTitleBar = value;
  34. }
  35. }
  36. public FormEx(Form host, RealParentWndProcDelegate realParentWndProc)
  37. {
  38. this.host = host;
  39. this.realParentWndProc = realParentWndProc;
  40. }
  41. public class ProcessCmdKeyEventArgs
  42. : EventArgs
  43. {
  44. private bool handled;
  45. public bool Handled
  46. {
  47. get
  48. {
  49. return this.handled;
  50. }
  51. set
  52. {
  53. this.handled = value;
  54. }
  55. }
  56. private Keys keyData;
  57. public Keys KeyData
  58. {
  59. get
  60. {
  61. return this.keyData;
  62. }
  63. }
  64. public ProcessCmdKeyEventArgs(Keys keyData, bool handled)
  65. {
  66. this.keyData = keyData;
  67. this.handled = handled;
  68. }
  69. }
  70. public event EventHandler<ProcessCmdKeyEventArgs> ProcessCmdKeyRelay;
  71. public bool RelayProcessCmdKey(Keys keyData)
  72. {
  73. bool handled = false;
  74. if (ProcessCmdKeyRelay != null)
  75. {
  76. ProcessCmdKeyEventArgs e = new ProcessCmdKeyEventArgs(keyData, false);
  77. ProcessCmdKeyRelay(this, e);
  78. handled = e.Handled;
  79. }
  80. return handled;
  81. }
  82. internal static FormEx FindFormEx(Form host)
  83. {
  84. if (host != null)
  85. {
  86. Control.ControlCollection controls = host.Controls;
  87. for (int i = 0; i < controls.Count; ++i)
  88. {
  89. FormEx formEx = controls[i] as FormEx;
  90. if (formEx != null)
  91. {
  92. return formEx;
  93. }
  94. }
  95. }
  96. return null;
  97. }
  98. private int ignoreNcActivate = 0;
  99. /// <summary>
  100. /// Manages some special handling of window messages.
  101. /// </summary>
  102. /// <param name="m"></param>
  103. /// <returns>true if the message was handled, false if the caller should handle the message.</returns>
  104. public bool HandleParentWndProc(ref Message m)
  105. {
  106. bool returnVal = true;
  107. switch (m.Msg)
  108. {
  109. case NativeConstants.WM_NCPAINT:
  110. goto default;
  111. case NativeConstants.WM_NCACTIVATE:
  112. if (this.forceActiveTitleBar && m.WParam == IntPtr.Zero)
  113. {
  114. if (ignoreNcActivate > 0)
  115. {
  116. --ignoreNcActivate;
  117. goto default;
  118. }
  119. else if (Form.ActiveForm != this.host || // Gets rid of: if you have the form active, then click on the desktop --> desktop refreshes
  120. !this.host.Visible) // Gets rid of: desktop refresh on exit
  121. {
  122. goto default;
  123. }
  124. else
  125. {
  126. // Only 'lock' for the topmost form in the application. Otherwise you get the whole system
  127. // refreshing (i.e. the dreaded "repaint the whole desktop 5 times" glitch) when you do things
  128. // like minimize the window
  129. // And only lock if we aren't minimized. Otherwise the desktop refreshes.
  130. bool locked = false;
  131. if (this.host.Owner == null &&
  132. this.host.WindowState != FormWindowState.Minimized)
  133. {
  134. //UI.SetControlRedraw(this.host, false);
  135. locked = true;
  136. }
  137. this.realParentWndProc(ref m);
  138. SafeNativeMethods.SendMessageW(this.host.Handle, NativeConstants.WM_NCACTIVATE,
  139. new IntPtr(1), IntPtr.Zero);
  140. if (locked)
  141. {
  142. //UI.SetControlRedraw(this.host, true);
  143. //this.host.Invalidate(true);
  144. }
  145. break;
  146. }
  147. }
  148. else
  149. {
  150. goto default;
  151. }
  152. case NativeConstants.WM_ACTIVATE:
  153. goto default;
  154. case NativeConstants.WM_ACTIVATEAPP:
  155. this.realParentWndProc(ref m);
  156. // Check if the app is being deactivated
  157. if (this.forceActiveTitleBar && m.WParam == IntPtr.Zero)
  158. {
  159. // If so, put our titlebar in the inactive state
  160. SafeNativeMethods.PostMessageW(this.host.Handle, NativeConstants.WM_NCACTIVATE,
  161. IntPtr.Zero, IntPtr.Zero);
  162. ++ignoreNcActivate;
  163. }
  164. if (m.WParam == new IntPtr(1))
  165. {
  166. foreach (Form childForm in this.host.OwnedForms)
  167. {
  168. FormEx childFormEx = FindFormEx(childForm);
  169. if (childFormEx != null)
  170. {
  171. if (childFormEx.ForceActiveTitleBar && childForm.IsHandleCreated)
  172. {
  173. SafeNativeMethods.PostMessageW(childForm.Handle, NativeConstants.WM_NCACTIVATE,
  174. new IntPtr(1), IntPtr.Zero);
  175. }
  176. }
  177. }
  178. FormEx ownerEx = FindFormEx(this.host.Owner);
  179. if (ownerEx != null)
  180. {
  181. if (ownerEx.ForceActiveTitleBar && this.host.Owner.IsHandleCreated)
  182. {
  183. SafeNativeMethods.PostMessageW(this.host.Owner.Handle, NativeConstants.WM_NCACTIVATE,
  184. new IntPtr(1), IntPtr.Zero);
  185. }
  186. }
  187. }
  188. break;
  189. default:
  190. returnVal = false;
  191. break;
  192. }
  193. GC.KeepAlive(this.host);
  194. return returnVal;
  195. }
  196. }
  197. }