Skip to content

Commit

Permalink
fix: context menu positioning (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwmkerr authored Nov 7, 2019
1 parent f95a732 commit c5f65c9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions SharpShell/SharpShell/SharpContextMenu/NativeContextMenuWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ public void ResetNativeContextMenu()
/// Builds a native context menu, on to the provided HMENU.
/// </summary>
/// <param name="hMenu">The handle to the menu.</param>
/// <param name="itemIndex">The zero-based position at which to insert the first new menu item.</param>
/// <param name="firstItemId">The first item id.</param>
/// <param name="toolStripItems">The tool strip menu items.</param>
/// <returns>The index of the last item created.</returns>
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)
Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion SharpShell/SharpShell/SharpContextMenu/SharpContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit c5f65c9

Please sign in to comment.