ToolStripEx.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Threading;
  3. using System.Windows.Forms;
  4. namespace SmartCoalApplication.SystemLayer
  5. {
  6. /// <summary>
  7. /// This class adds on to the functionality provided in System.Windows.Forms.ToolStrip.
  8. /// </summary>
  9. public class ToolStripEx : ToolStrip
  10. {
  11. private bool clickThrough = true;
  12. private bool managedFocus = true;
  13. private static int lockFocusCount = 0;
  14. public ToolStripEx()
  15. {
  16. ToolStripProfessionalRenderer tspr = this.Renderer as ToolStripProfessionalRenderer;
  17. if (tspr != null)
  18. {
  19. tspr.ColorTable.UseSystemColors = true;
  20. tspr.RoundedEdges = false;
  21. }
  22. this.ImageScalingSize = new System.Drawing.Size(UI.ScaleWidth(16), UI.ScaleHeight(16));
  23. }
  24. protected override bool ProcessCmdKey(ref Message m, Keys keyData)
  25. {
  26. bool processed = false;
  27. Form form = this.FindForm();
  28. FormEx formEx = FormEx.FindFormEx(form);
  29. if (formEx != null)
  30. {
  31. processed = formEx.RelayProcessCmdKey(keyData);
  32. }
  33. return processed;
  34. }
  35. public bool ClickThrough
  36. {
  37. get
  38. {
  39. return this.clickThrough;
  40. }
  41. set
  42. {
  43. this.clickThrough = value;
  44. }
  45. }
  46. protected override void WndProc(ref Message m)
  47. {
  48. base.WndProc(ref m);
  49. if (this.clickThrough)
  50. {
  51. UI.ClickThroughWndProc(ref m);
  52. }
  53. }
  54. /// <summary>
  55. /// This event is raised when this toolstrip instance wishes to relinquish focus.
  56. /// </summary>
  57. public event EventHandler RelinquishFocus;
  58. private void OnRelinquishFocus()
  59. {
  60. if (RelinquishFocus != null)
  61. {
  62. RelinquishFocus(this, EventArgs.Empty);
  63. }
  64. }
  65. /// <summary>
  66. /// Gets or sets whether the toolstrip manages focus.
  67. /// </summary>
  68. /// <remarks>
  69. /// If this is true, the toolstrip will capture focus when the mouse enters its client area. It will then
  70. /// relinquish focus (via the RelinquishFocus event) when the mouse leaves. It will not capture or
  71. /// attempt to relinquish focus if MenuStripEx.IsAnyMenuActive returns true.
  72. /// </remarks>
  73. public bool ManagedFocus
  74. {
  75. get
  76. {
  77. return this.managedFocus;
  78. }
  79. set
  80. {
  81. this.managedFocus = value;
  82. }
  83. }
  84. protected override void OnItemAdded(ToolStripItemEventArgs e)
  85. {
  86. ToolStripComboBox tscb = e.Item as ToolStripComboBox;
  87. ToolStripTextBox tstb = e.Item as ToolStripTextBox;
  88. if (tscb != null)
  89. {
  90. tscb.DropDownClosed += ComboBox_DropDownClosed;
  91. tscb.Enter += ComboBox_Enter;
  92. tscb.Leave += ComboBox_Leave;
  93. }
  94. else if (tstb != null)
  95. {
  96. tstb.Enter += TextBox_Enter;
  97. tstb.Leave += TextBox_Enter;
  98. }
  99. else
  100. {
  101. e.Item.MouseEnter += OnItemMouseEnter;
  102. }
  103. base.OnItemAdded(e);
  104. }
  105. private static void PushLockFocus()
  106. {
  107. Interlocked.Increment(ref lockFocusCount);
  108. }
  109. private static void PopLockFocus()
  110. {
  111. Interlocked.Decrement(ref lockFocusCount);
  112. }
  113. private static bool IsFocusLocked
  114. {
  115. get
  116. {
  117. return lockFocusCount > 0;
  118. }
  119. }
  120. private void ComboBox_Enter(object sender, EventArgs e)
  121. {
  122. PushLockFocus();
  123. }
  124. private void ComboBox_Leave(object sender, EventArgs e)
  125. {
  126. PopLockFocus();
  127. }
  128. private void TextBox_Enter(object sender, EventArgs e)
  129. {
  130. PushLockFocus();
  131. }
  132. private void TextBox_Leave(object sender, EventArgs e)
  133. {
  134. PopLockFocus();
  135. }
  136. private void ComboBox_DropDownClosed(object sender, EventArgs e)
  137. {
  138. OnRelinquishFocus();
  139. }
  140. protected override void OnItemRemoved(ToolStripItemEventArgs e)
  141. {
  142. ToolStripComboBox tscb = e.Item as ToolStripComboBox;
  143. ToolStripTextBox tstb = e.Item as ToolStripTextBox;
  144. if (tscb != null)
  145. {
  146. tscb.DropDownClosed -= ComboBox_DropDownClosed;
  147. tscb.Enter -= ComboBox_Enter;
  148. tscb.Leave -= ComboBox_Leave;
  149. }
  150. else if (tstb != null)
  151. {
  152. tstb.Enter -= TextBox_Enter;
  153. tstb.Leave -= TextBox_Enter;
  154. }
  155. else
  156. {
  157. e.Item.MouseEnter -= OnItemMouseEnter;
  158. }
  159. base.OnItemRemoved(e);
  160. }
  161. private void OnItemMouseEnter(object sender, EventArgs e)
  162. {
  163. if (this.managedFocus && !MenuStripEx.IsAnyMenuActive && UI.IsOurAppActive && !IsFocusLocked)
  164. {
  165. this.Focus();
  166. }
  167. }
  168. protected override void OnMouseLeave(EventArgs e)
  169. {
  170. if (this.managedFocus && !MenuStripEx.IsAnyMenuActive && UI.IsOurAppActive && !IsFocusLocked)
  171. {
  172. OnRelinquishFocus();
  173. }
  174. base.OnMouseLeave(e);
  175. }
  176. }
  177. }