public static extern bool SetMenuItemInfo(IntPtr hMenu, uint uItem, bool fByPos, ref ShellAPI.MENUITEMINFO lpmii);
private void CreateFolderMenu(Point ptInvoke, MouseEventArgs e) { #region Fields ShellItem currentItem = br.SelectedItem; foreach (ListViewItem item in br.FileView.SelectedItems) item.Selected = false; IntPtr contextMenu = IntPtr.Zero, viewSubMenu = IntPtr.Zero; IntPtr newContextMenuPtr = IntPtr.Zero, newContextMenuPtr2 = IntPtr.Zero, newContextMenuPtr3 = IntPtr.Zero; newSubmenuPtr = IntPtr.Zero; #endregion try { #region Make Menu contextMenu = ShellAPI.CreatePopupMenu(); viewSubMenu = ShellAPI.CreatePopupMenu(); #region View Submenu ShellAPI.MENUITEMINFO itemInfo = new ShellAPI.MENUITEMINFO("View"); itemInfo.cbSize = ShellAPI.cbMenuItemInfo; itemInfo.fMask = ShellAPI.MIIM.SUBMENU | ShellAPI.MIIM.STRING; itemInfo.hSubMenu = viewSubMenu; ShellAPI.InsertMenuItem(contextMenu, 0, true, ref itemInfo); ShellAPI.MFT rCheck = ShellAPI.MFT.RADIOCHECK | ShellAPI.MFT.CHECKED; ShellAPI.AppendMenu(viewSubMenu, (br.CurrentViewPlugin == null && br.FileView.View == View.Tile ? rCheck : 0), (uint)CMD_CUSTOM.Tiles, "Tiles"); ShellAPI.AppendMenu(viewSubMenu, (br.CurrentViewPlugin == null && br.FileView.View == View.LargeIcon ? rCheck : 0), (uint)CMD_CUSTOM.Icons, "Icons"); ShellAPI.AppendMenu(viewSubMenu, (br.CurrentViewPlugin == null && br.FileView.View == View.List ? rCheck : 0), (uint)CMD_CUSTOM.List, "List"); ShellAPI.AppendMenu(viewSubMenu, (br.CurrentViewPlugin == null && br.FileView.View == View.Details ? rCheck : 0), (uint)CMD_CUSTOM.Details, "Details"); for (int i = 0; i < pluginWrapper.ViewPlugins.Count; i++) { ShellAPI.AppendMenu(viewSubMenu, (IViewPlugin.Equals(br.CurrentViewPlugin, pluginWrapper.ViewPlugins[i]) ? rCheck : 0), (uint)CMD_CUSTOM.SpecialView + (uint)i, ((IViewPlugin)pluginWrapper.ViewPlugins[i]).ViewName); } #endregion #region Paste Menu DragDropEffects effects = ShellHelper.CanDropClipboard(currentItem); bool canPaste = (effects & DragDropEffects.Copy) != 0 || (effects & DragDropEffects.Move) != 0; bool canPasteShortCut = (effects & DragDropEffects.Link) != 0; ShellAPI.AppendMenu(contextMenu, ShellAPI.MFT.SEPARATOR, 0, string.Empty); ShellAPI.AppendMenu(contextMenu, canPaste ? 0 : ShellAPI.MFT.GRAYED, (int)CMD_CUSTOM.Paste, "Paste"); ShellAPI.AppendMenu(contextMenu, canPasteShortCut ? 0 : ShellAPI.MFT.GRAYED, (int)CMD_CUSTOM.Paste_ShortCut, "Paste Shortcut"); #endregion #region New Submenu if (br.SelectedItem.IsFileSystem && ContextMenuHelper.GetNewContextMenu(br.SelectedItem, out newContextMenuPtr, out newContextMenu)) { ShellAPI.AppendMenu(contextMenu, ShellAPI.MFT.SEPARATOR, 0, string.Empty); newContextMenu.QueryContextMenu( contextMenu, 5, ShellAPI.CMD_FIRST, ShellAPI.CMD_LAST, ShellAPI.CMF.NORMAL); newSubmenuPtr = ShellAPI.GetSubMenu(contextMenu, 5); Marshal.QueryInterface(newContextMenuPtr, ref ShellAPI.IID_IContextMenu2, out newContextMenuPtr2); Marshal.QueryInterface(newContextMenuPtr, ref ShellAPI.IID_IContextMenu3, out newContextMenuPtr3); try { newContextMenu2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(newContextMenuPtr2, typeof(IContextMenu2)); newContextMenu3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown(newContextMenuPtr3, typeof(IContextMenu3)); } catch (Exception) { } } #endregion #region Properties Menu if (!br.SelectedItem.Equals(br.ShellBrowser.DesktopItem)) { ShellAPI.AppendMenu(contextMenu, ShellAPI.MFT.SEPARATOR, 0, string.Empty); ShellAPI.AppendMenu(contextMenu, 0, (int)CMD_CUSTOM.Properties, "Properties"); } #endregion #endregion CMD_CUSTOM selected = (CMD_CUSTOM)ShellAPI.TrackPopupMenuEx( contextMenu, ShellAPI.TPM.RETURNCMD, ptInvoke.X, ptInvoke.Y, this.Handle, IntPtr.Zero); #region Invoke if ((int)selected >= ShellAPI.CMD_FIRST) { switch (selected) { #region Normal Views case CMD_CUSTOM.Tiles: br.FileView.View = View.Tile; br.ResetSpecialView(); provider.ReleaseStorage(); provider.ReleaseStream(); break; case CMD_CUSTOM.Icons: br.FileView.View = View.LargeIcon; br.ResetSpecialView(); provider.ReleaseStorage(); provider.ReleaseStream(); break; case CMD_CUSTOM.List: br.FileView.View = View.List; br.ResetSpecialView(); provider.ReleaseStorage(); provider.ReleaseStream(); break; case CMD_CUSTOM.Details: br.FileView.SuspendHeaderContextMenu = true; br.FileView.View = View.Details; br.ResetSpecialView(); provider.ReleaseStorage(); provider.ReleaseStream(); break; #endregion #region Folder Items case CMD_CUSTOM.Properties: ContextMenuHelper.InvokeCommand( br.SelectedItem.ParentItem, new IntPtr[] { br.SelectedItem.PIDLRel.Ptr }, "properties", ptInvoke); break; case CMD_CUSTOM.Paste: ContextMenuHelper.InvokeCommand( br.SelectedItem.ParentItem, new IntPtr[] { br.SelectedItem.PIDLRel.Ptr }, "paste", ptInvoke); break; case CMD_CUSTOM.Paste_ShortCut: ContextMenuHelper.InvokeCommand( br.SelectedItem.ParentItem, new IntPtr[] { br.SelectedItem.PIDLRel.Ptr }, "pastelink", ptInvoke); break; #endregion default: #region New if ((uint)selected <= ShellAPI.CMD_LAST) { lock (br.ShellBrowser) { br.NewItemCreated = true; } ContextMenuHelper.InvokeCommand( newContextMenu, (uint)selected - ShellAPI.CMD_FIRST, ShellItem.GetRealPath(br.SelectedItem), ptInvoke); } #endregion #region Special Views else { int index = (int)selected - (int)CMD_CUSTOM.SpecialView; if (br.FileView.Alignment != ListViewAlignment.Left) br.FileView.Alignment = ListViewAlignment.Left; br.FileView.View = View.LargeIcon; br.CurrentViewPlugin = pluginWrapper.ViewPlugins[index] as IViewPlugin; br.SpecialViewPanelVisible = true; br.SpecialViewPanel.Controls.Add(br.CurrentViewPlugin.ViewControl); br.CurrentViewPlugin.ViewControl.Dock = DockStyle.Fill; } #endregion break; } } #endregion } catch (Exception) { } #region Finally finally { if (newContextMenu != null) { Marshal.ReleaseComObject(newContextMenu); newContextMenu = null; } if (newContextMenu2 != null) { Marshal.ReleaseComObject(newContextMenu2); newContextMenu2 = null; } if (newContextMenu3 != null) { Marshal.ReleaseComObject(newContextMenu3); newContextMenu3 = null; } if (contextMenu != null) ShellAPI.DestroyMenu(contextMenu); if (viewSubMenu != null) ShellAPI.DestroyMenu(viewSubMenu); if (newContextMenuPtr != IntPtr.Zero) Marshal.Release(newContextMenuPtr); if (newContextMenuPtr2 != IntPtr.Zero) Marshal.Release(newContextMenuPtr2); if (newContextMenuPtr3 != IntPtr.Zero) Marshal.Release(newContextMenuPtr3); newSubmenuPtr = IntPtr.Zero; } #endregion }
public static extern bool InsertMenuItem(IntPtr hMenu, uint uItem, bool fByPosition, ref ShellAPI.MENUITEMINFO lpmii);
private static ShellAPI.MENUITEMINFO generateMenuItemInfo(CustomMenuStructure cms, uint idCmdFirst) { ShellAPI.MENUITEMINFO mii = new ShellAPI.MENUITEMINFO(); mii.cbSize = Marshal.SizeOf(mii); mii.wID = idCmdFirst + cms.ID; mii.fType = ShellAPI.MFT.MFT_STRING; mii.fMask = ShellAPI.MIIM.ID | ShellAPI.MIIM.TYPE; mii.dwTypeData = cms.Text; mii.fState = ShellAPI.MFS.ENABLED; if (cms.Checked) mii.fState |= ShellAPI.MFS.CHECKED; if (cms.IsFolder) mii.fMask |= ShellAPI.MIIM.SUBMENU; //Not working, dont know why... //if (cms.Icon != null) //{ // IntPtr ptrBitmap = cms.Icon.GetHbitmap(); // imgPtrConstructed.Add(ptrBitmap); // mii.hbmpItem = ptrBitmap; // mii.fMask |= ShellAPI.MIIM.BITMAP; // mii.fType |= ShellAPI.MFT.BITMAP; //} return mii; }