private void Desktop_ShowElementMenu(object sender, AcceptElementLocationEventArgs e) { NodeItem item = e.Element as NodeItem; if (item != null) { MyNodeView nodeView = item.Node as MyNodeView; if (nodeView != null) { MyNode node = nodeView.Node; openEditorToolStripMenuItem.Enabled = node is IScriptableNode; openGroupToolStripMenuItem.Enabled = node is MyNodeGroup; nodeContextMenuStrip.Tag = node; nodeContextMenuStrip.Show(e.Position); } } }
protected override void OnMouseClick(MouseEventArgs e) { try { ignoreDoubleClick = false; if (mouseMoved) return; var points = new Point[] { LastLocation }; inverse_transformation.TransformPoints(points); var transformed_location = points[0]; if (e.Button == MouseButtons.Right) { if (null != ShowElementMenu) { // See if we clicked on an element and give our owner the chance to show a menu var result = FindElementAt(transformed_location, delegate(IElement el) { // Fire the event and see if someone cancels it. var eventArgs = new AcceptElementLocationEventArgs(el, this.PointToScreen(LastLocation)); // Give our owner the chance to show a menu for this element ... ShowElementMenu(this, eventArgs); // If the owner declines (cancel == true) then we'll continue looking up the hierarchy .. return !eventArgs.Cancel; }); // If we haven't found anything to click on we'll just return the event with a null pointer .. // allowing our owner to show a generic menu if (result == null) { var eventArgs = new AcceptElementLocationEventArgs(null, this.PointToScreen(LastLocation)); ShowElementMenu(this, eventArgs); } return; } } var element = FindElementAt(transformed_location); if (element == null) { ignoreDoubleClick = true; // to avoid double-click from firing if (ModifierKeys == Keys.None) FocusElement = null; return; } switch (element.ElementType) { case ElementType.NodeItem: { if (ModifierKeys != Keys.None) return; var item = element as NodeItem; if (item.OnClick()) { ignoreDoubleClick = true; // to avoid double-click from firing this.Refresh(); return; } break; } } } finally { base.OnMouseClick(e); } }
void OnShowElementMenu(object sender, AcceptElementLocationEventArgs e) { if (e.Element == null) { // Show a test menu for when you click on nothing testMenuItem.Text = "(clicked on nothing)"; nodeMenu.Show(e.Position); e.Cancel = false; } else if (e.Element is Node) { // Show a test menu for a node testMenuItem.Text = ((Node)e.Element).Title; nodeMenu.Show(e.Position); e.Cancel = false; } else if (e.Element is NodeItem) { // Show a test menu for a nodeItem testMenuItem.Text = e.Element.GetType().Name; nodeMenu.Show(e.Position); e.Cancel = false; } else { // if you don't want to show a menu for this item (but perhaps show a menu for something more higher up) // then you can cancel the event e.Cancel = true; } }