Shortcut menus (ContextMenuStrip/ContextMenu) and the 3D Mouse

A 3D mouse is an input device used by some CAD users. After a 3D mouse is detected for the first time, whenever the user right-clicks on an Eyeshot control with their (standard) mouse the normal shortcut menu is repopulated with navigation options specific for 3D mice. Please be aware that Eyeshot controls do not react when the "MENU" button of your 3D mouse is pressed. That input is handled by the 3D mouse driver (if it is installed). If you think your users may want to use a 3D mouse with your application, you may want to customize how its shortcut menu will behave.

Unless otherwise specified, this article applies to both WinForms and WPF. Thus, we need to introduce some common vocabulary between the two platforms:

  • shortcut menu: the menu that pops up when the user right-clicks on an Eyeshot control with their standard mouse. In WinForms, it is represented by an instance of ContextMenuStrip (that has no children if we do not want any shortcut menus to appear at all). In WPF, it is represented by an instance of ContextMenu (that is null if we do not want any shortcut menus to appear at all);
  • menu item: an item of the shortcut menu. In WinForms, it is represented by a ToolStripItem; in WPF, it is represented by a MenuItem.

A list of common scenarios and solutions follows.

Let your custom menu items be overwritten (naïve implementation)

If you just add your menu items once at initialization time as you would normally do, they will be overridden by the Mouse3D shortcut menu. This is probably not your desired behavior.

Removing the shortcut menu altogether

If you do not want any menu to appear on a right-click, you may just remove the shortcut menu altogether.

WinForms

(design1.ContextMenuStrip = new ContextMenuStrip()).Opening +=
        (_, __) => design1.ContextMenuStrip.Items.Clear();

WPF

Unlike the ContextMenuStrip, the ContextMenu shows up as an empty list when it has no items, so we set it to null altogether.

design1.ContextMenuOpening += (_, __) => design1.ContextMenu = null;

Replacing the 3D mouse menu items with your own custom menu items

If you want your custom menu items to appear instead of the standard 3D Mouse options, you can clear the item lists first, and then add whatever you like next.

WinForms

(design1.ContextMenuStrip = new ContextMenuStrip()).Opening += (_, __) =>
{
    var items = design1.ContextMenuStrip.Items;
    items.Clear();
    items.Add("Custom item 1"); };

WPF

design1.ContextMenu = new ContextMenu();
design1.ContextMenuOpening += (_, cmea) =>
        design1.ContextMenu.Items.Add(new MenuItem {Header = "Custom item 1"});

Adding your custom menu items to the 3D mouse menu items

If you want to keep both the standard 3D mouse and custom menu items, you can merge them into the same menu.

WinForms

private readonly ToolStripMenuItem _customContextMenuItem = new ToolStripMenuItem("Custom item 1");
(design1.ContextMenuStrip = new ContextMenuStrip()).Opening += (_, __) =>
{
    var items = design1.ContextMenuStrip.Items;
    if (!items.Contains(_customContextMenuItem))
    items.Add(_customContextMenuItem);
};

WPF

private readonly MenuItem _customContextMenuItem => new MenuItem { Header = "Custom item 1"};
design1.ContextMenu = new ContextMenu();
design1.ContextMenuOpening += (_, __) =>
{
    var items = design1.ContextMenu.Items;
    if (!items.Contains(_customContextMenuItem))
        items.Add(_customContextMenuItems);
};
Was this article helpful?
1 out of 1 found this helpful

Comments

0 comments

Please sign in to leave a comment.

Articles in this section