code_junkie

How to show a Context Menu when you right click a Menu Item

Nov 12th, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. In:
  2. protected override void OnClick(EventArgs e)
  3. {
  4. if (SecondaryContextMenu == null || MouseButtons != MouseButtons.Right)
  5. {
  6. base.OnClick(e);
  7. }
  8. }
  9.  
  10. MouseButtons != MouseButtons.Right
  11.  
  12. public partial class Form1 : Form
  13. {
  14. class CustomToolStripMenuItem : ToolStripMenuItem
  15. {
  16. private ContextMenuStrip secondaryContextMenu;
  17.  
  18. public ContextMenuStrip SecondaryContextMenu
  19. {
  20. get
  21. {
  22. return secondaryContextMenu;
  23. }
  24. set
  25. {
  26. secondaryContextMenu = value;
  27. }
  28. }
  29.  
  30. public CustomToolStripMenuItem(string text)
  31. : base(text)
  32. { }
  33.  
  34. protected override void Dispose(bool disposing)
  35. {
  36. if (disposing)
  37. {
  38. if (secondaryContextMenu != null)
  39. {
  40. secondaryContextMenu.Dispose();
  41. secondaryContextMenu = null;
  42. }
  43. }
  44.  
  45. base.Dispose(disposing);
  46. }
  47.  
  48. protected override void OnClick(EventArgs e)
  49. {
  50. if (SecondaryContextMenu == null || MouseButtons != MouseButtons.Right)
  51. {
  52. base.OnClick(e);
  53. }
  54. }
  55. }
  56.  
  57. class CustomContextMenuStrip : ContextMenuStrip
  58. {
  59. private bool secondaryContextMenuActive = false;
  60. private ContextMenuStrip lastShownSecondaryContextMenu = null;
  61.  
  62. protected override void Dispose(bool disposing)
  63. {
  64. if (disposing)
  65. {
  66. if (lastShownSecondaryContextMenu != null)
  67. {
  68. lastShownSecondaryContextMenu.Close();
  69. lastShownSecondaryContextMenu = null;
  70. }
  71. }
  72. base.Dispose(disposing);
  73. }
  74.  
  75. protected override void OnControlAdded(ControlEventArgs e)
  76. {
  77. e.Control.MouseClick += new MouseEventHandler(Control_MouseClick);
  78. base.OnControlAdded(e);
  79. }
  80.  
  81. protected override void OnControlRemoved(ControlEventArgs e)
  82. {
  83. e.Control.MouseClick -= new MouseEventHandler(Control_MouseClick);
  84. base.OnControlRemoved(e);
  85. }
  86.  
  87. private void Control_MouseClick(object sender, MouseEventArgs e)
  88. {
  89. ShowSecondaryContextMenu(e);
  90. }
  91.  
  92. protected override void OnMouseClick(MouseEventArgs e)
  93. {
  94. ShowSecondaryContextMenu(e);
  95. base.OnMouseClick(e);
  96. }
  97.  
  98. private bool ShowSecondaryContextMenu(MouseEventArgs e)
  99. {
  100. CustomToolStripMenuItem ctsm = this.GetItemAt(e.Location) as CustomToolStripMenuItem;
  101.  
  102. if (ctsm == null || ctsm.SecondaryContextMenu == null || e.Button != MouseButtons.Right)
  103. {
  104. return false;
  105. }
  106.  
  107. lastShownSecondaryContextMenu = ctsm.SecondaryContextMenu;
  108. secondaryContextMenuActive = true;
  109. ctsm.SecondaryContextMenu.Closed += new ToolStripDropDownClosedEventHandler(SecondaryContextMenu_Closed);
  110. ctsm.SecondaryContextMenu.Show(Cursor.Position);
  111. return true;
  112. }
  113.  
  114. void SecondaryContextMenu_Closed(object sender, ToolStripDropDownClosedEventArgs e)
  115. {
  116. ((ContextMenuStrip)sender).Closed -= new ToolStripDropDownClosedEventHandler(SecondaryContextMenu_Closed);
  117. lastShownSecondaryContextMenu = null;
  118. secondaryContextMenuActive = false;
  119. Focus();
  120. }
  121.  
  122. protected override void OnClosing(ToolStripDropDownClosingEventArgs e)
  123. {
  124. if (secondaryContextMenuActive)
  125. {
  126. e.Cancel = true;
  127. }
  128.  
  129. base.OnClosing(e);
  130. }
  131. }
  132.  
  133. public Form1()
  134. {
  135. InitializeComponent();
  136.  
  137.  
  138. CustomToolStripMenuItem itemPrimary1 = new CustomToolStripMenuItem("item primary 1");
  139. itemPrimary1.SecondaryContextMenu = new ContextMenuStrip();
  140. itemPrimary1.SecondaryContextMenu.Items.AddRange(new ToolStripMenuItem[] {
  141. new ToolStripMenuItem("item primary 1.1"),
  142. new ToolStripMenuItem("item primary 1.2"),
  143. });
  144.  
  145. CustomToolStripMenuItem itemPrimary2 = new CustomToolStripMenuItem("item primary 2");
  146. itemPrimary2.DropDownItems.Add("item primary 2, sub 1");
  147. itemPrimary2.DropDownItems.Add("item primary 2, sub 2");
  148. itemPrimary2.SecondaryContextMenu = new ContextMenuStrip();
  149. itemPrimary2.SecondaryContextMenu.Items.AddRange(new ToolStripMenuItem[] {
  150. new ToolStripMenuItem("item primary 2.1"),
  151. new ToolStripMenuItem("item primary 2.2"),
  152. });
  153.  
  154. CustomContextMenuStrip primaryContextMenu = new CustomContextMenuStrip();
  155. primaryContextMenu.Items.AddRange(new ToolStripItem[]{
  156. itemPrimary1,
  157. itemPrimary2
  158. });
  159.  
  160. this.ContextMenuStrip = primaryContextMenu;
  161. }
  162. }
  163.  
  164. [DllImport("user32.dll")]
  165. static extern bool TrackPopupMenuEx(IntPtr hmenu, uint fuFlags, int x, int y,
  166. IntPtr hwnd, IntPtr lptpm);
  167.  
  168. const int TPM_RECURSE = 0x0001;
Add Comment
Please, Sign In to add comment