MenuStrip.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // MenuStrip.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Copyright (c) 2006 Jonathan Pobst
  24. //
  25. // Authors:
  26. // Jonathan Pobst ([email protected])
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Drawing;
  31. using System.ComponentModel;
  32. using System.Runtime.InteropServices;
  33. namespace System.Windows.Forms
  34. {
  35. [ClassInterface (ClassInterfaceType.AutoDispatch)]
  36. [ComVisible (true)]
  37. public class MenuStrip : ToolStrip
  38. {
  39. private bool can_overflow;
  40. private bool stretch;
  41. public MenuStrip () : base ()
  42. {
  43. this.GripStyle = ToolStripGripStyle.Hidden;
  44. this.stretch = true;
  45. }
  46. #region Public Properties
  47. [DefaultValue (false)]
  48. public bool CanOverflow
  49. {
  50. get { return this.can_overflow; }
  51. set { this.can_overflow = value; }
  52. }
  53. [DefaultValue (ToolStripGripStyle.Hidden)]
  54. public ToolStripGripStyle GripStyle
  55. {
  56. get { return base.GripStyle; }
  57. set { base.GripStyle = value; }
  58. }
  59. [DefaultValue (false)]
  60. public bool ShowItemToolTips
  61. {
  62. get { return base.ShowItemToolTips; }
  63. set { base.ShowItemToolTips = value; }
  64. }
  65. [MonoTODO ()]
  66. [DefaultValue (true)]
  67. public bool Stretch
  68. {
  69. get { return this.stretch; }
  70. set { this.stretch = value; }
  71. }
  72. #endregion
  73. #region Protected Properties
  74. protected override Padding DefaultGripMargin { get { return new Padding (2, 2, 0, 2); } }
  75. protected override Padding DefaultPadding { get { return new Padding (6, 2, 0, 2); } }
  76. protected override bool DefaultShowItemToolTips { get { return false; } }
  77. protected override Size DefaultSize { get { return new Size (200, 24); } }
  78. #endregion
  79. #region Protected Methods
  80. protected internal override ToolStripItem CreateDefaultItem (string text, Image image, EventHandler onClick)
  81. {
  82. return new ToolStripMenuItem (text, image, onClick);
  83. }
  84. protected virtual void OnMenuActivate (EventArgs e)
  85. {
  86. if (MenuActivate != null) MenuActivate (this, e);
  87. }
  88. protected virtual void OnMenuDeactivate (EventArgs e)
  89. {
  90. if (MenuDeactivate != null) MenuDeactivate (this, e);
  91. }
  92. protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
  93. {
  94. return base.ProcessCmdKey (ref msg, keyData);
  95. }
  96. protected override void WndProc (ref Message m)
  97. {
  98. base.WndProc (ref m);
  99. }
  100. #endregion
  101. #region Public Events
  102. public event EventHandler MenuActivate;
  103. public event EventHandler MenuDeactivate;
  104. #endregion
  105. #region Internal Methods
  106. internal void FireMenuActivate ()
  107. {
  108. // The tracker lets us know when the form is clicked or loses focus
  109. ToolStripMenuTracker.AppClicked += new EventHandler (ToolStripMenuTracker_AppClicked);
  110. ToolStripMenuTracker.AppFocusChange += new EventHandler (ToolStripMenuTracker_AppFocusChange);
  111. this.OnMenuActivate (EventArgs.Empty);
  112. }
  113. internal void FireMenuDeactivate ()
  114. {
  115. // Detach from the tracker
  116. ToolStripMenuTracker.AppClicked -= new EventHandler (ToolStripMenuTracker_AppClicked); ;
  117. ToolStripMenuTracker.AppFocusChange -= new EventHandler (ToolStripMenuTracker_AppFocusChange);
  118. this.OnMenuDeactivate (EventArgs.Empty);
  119. }
  120. private void ToolStripMenuTracker_AppFocusChange (object sender, EventArgs e)
  121. {
  122. this.HideMenus (true, ToolStripDropDownCloseReason.AppFocusChange);
  123. }
  124. private void ToolStripMenuTracker_AppClicked (object sender, EventArgs e)
  125. {
  126. this.HideMenus (true, ToolStripDropDownCloseReason.AppClicked);
  127. }
  128. #endregion
  129. }
  130. }
  131. #endif