MenuStripEx.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Windows.Forms;
  3. namespace PaintDotNet.SystemLayer
  4. {
  5. /// <summary>
  6. /// This class adds on to the functionality provided in System.Windows.Forms.MenuStrip.
  7. /// </summary>
  8. public class MenuStripEx
  9. : MenuStrip
  10. {
  11. private bool clickThrough = true;
  12. private static int openCount = 0;
  13. public MenuStripEx()
  14. {
  15. this.ImageScalingSize = new System.Drawing.Size(UI.ScaleWidth(16), UI.ScaleHeight(16));
  16. }
  17. /// <summary>
  18. /// Gets or sets whether the ToolStripEx honors item clicks when its containing form does
  19. /// not have input focus.
  20. /// </summary>
  21. /// <remarks>
  22. /// Default value is true, which is the opposite of the behavior provided by the base
  23. /// ToolStrip class.
  24. /// </remarks>
  25. public bool ClickThrough
  26. {
  27. get
  28. {
  29. return this.clickThrough;
  30. }
  31. set
  32. {
  33. this.clickThrough = value;
  34. }
  35. }
  36. protected override void WndProc(ref Message m)
  37. {
  38. base.WndProc(ref m);
  39. if (this.clickThrough)
  40. {
  41. UI.ClickThroughWndProc(ref m);
  42. }
  43. }
  44. /// <summary>
  45. /// Gets a value indicating whether any menu is currently open.
  46. /// </summary>
  47. /// <remarks>
  48. /// To be precise, this will return true if any menu has raised its MenuActivate event
  49. /// but has yet to raise its MenuDeactivate event.</remarks>
  50. public static bool IsAnyMenuActive
  51. {
  52. get
  53. {
  54. return openCount > 0;
  55. }
  56. }
  57. public static void PushMenuActivate()
  58. {
  59. ++openCount;
  60. }
  61. public static void PopMenuActivate()
  62. {
  63. --openCount;
  64. }
  65. protected override void OnMenuActivate(EventArgs e)
  66. {
  67. ++openCount;
  68. base.OnMenuActivate(e);
  69. }
  70. protected override void OnMenuDeactivate(EventArgs e)
  71. {
  72. --openCount;
  73. base.OnMenuDeactivate(e);
  74. }
  75. }
  76. }