From c5f65c93708df972d68e14c069c0fb18bbfa3099 Mon Sep 17 00:00:00 2001 From: Dave Kerr Date: Thu, 7 Nov 2019 13:10:05 +0800 Subject: [PATCH] fix: context menu positioning (#316) --- .../NativeContextMenuWrapper.cs | 18 ++++++++++-------- .../SharpContextMenu/SharpContextMenu.cs | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/SharpShell/SharpShell/SharpContextMenu/NativeContextMenuWrapper.cs b/SharpShell/SharpShell/SharpContextMenu/NativeContextMenuWrapper.cs index 24cd3c2a..79106ed7 100644 --- a/SharpShell/SharpShell/SharpContextMenu/NativeContextMenuWrapper.cs +++ b/SharpShell/SharpShell/SharpContextMenu/NativeContextMenuWrapper.cs @@ -27,14 +27,17 @@ public void ResetNativeContextMenu() /// Builds a native context menu, on to the provided HMENU. /// /// The handle to the menu. + /// The zero-based position at which to insert the first new menu item. /// The first item id. /// The tool strip menu items. /// The index of the last item created. - public uint BuildNativeContextMenu(IntPtr hMenu, uint firstItemId, ToolStripItemCollection toolStripItems) + public uint BuildNativeContextMenu(IntPtr hMenu, uint itemIndex, uint firstItemId, ToolStripItemCollection toolStripItems) { - // Create an ID counter and position counter. + // Create an ID counter and position counter. The position is provided by the caller. If this is a top level menu item (i.e. + // top level in the shell context menu) then 'position' will be provided by the Shell via an earlier call to IContextMenu::QueryContextMenu. + // When we create submenus, we simply start at position '0'. var idCounter = firstItemId; - uint positionCounter = 0; + var positionCounter = itemIndex; // Go through every tool strip item. foreach (ToolStripItem item in toolStripItems) @@ -58,17 +61,16 @@ public uint BuildNativeContextMenu(IntPtr hMenu, uint firstItemId, ToolStripItem continue; } - // We successfully created the menu item, so increment the counters. + // We successfully created the menu item, so increment the position and ID counters. indexedCommands.Add(item); idCounter++; positionCounter++; // Have we just built a menu item? If so, does it have child items? - var toolStripMenuItem = item as ToolStripMenuItem; - if (toolStripMenuItem != null && toolStripMenuItem.HasDropDownItems) + if (item is ToolStripMenuItem toolStripMenuItem && toolStripMenuItem.HasDropDownItems) { - // Create each drop down item. - idCounter = BuildNativeContextMenu(menuItemInfo.hSubMenu, idCounter, toolStripMenuItem.DropDownItems); + // Create the drop down menu. As this is a submenu, we start at position zero and go from there. + idCounter = BuildNativeContextMenu(menuItemInfo.hSubMenu, 0, idCounter, toolStripMenuItem.DropDownItems); } } diff --git a/SharpShell/SharpShell/SharpContextMenu/SharpContextMenu.cs b/SharpShell/SharpShell/SharpContextMenu/SharpContextMenu.cs index 57593490..a96a2333 100644 --- a/SharpShell/SharpShell/SharpContextMenu/SharpContextMenu.cs +++ b/SharpShell/SharpShell/SharpContextMenu/SharpContextMenu.cs @@ -73,7 +73,7 @@ int IContextMenu.QueryContextMenu(IntPtr hMenu, uint indexMenu, int idCmdFirst, try { nativeContextMenuWrapper.ResetNativeContextMenu(); - lastItemId = nativeContextMenuWrapper.BuildNativeContextMenu(hMenu, firstItemId, contextMenuStrip.Value.Items); + lastItemId = nativeContextMenuWrapper.BuildNativeContextMenu(hMenu, indexMenu, firstItemId, contextMenuStrip.Value.Items); } catch (Exception exception) {