/// <summary> /// Add commands to a shortcut menu. /// </summary> /// <param name="hMenu">A handle to the shortcut menu.</param> /// <param name="iMenu"> /// The zero-based position at which to insert the first new menu item. /// </param> /// <param name="idCmdFirst"> /// The minimum value that the handler can specify for a menu item ID. /// </param> /// <param name="idCmdLast"> /// The maximum value that the handler can specify for a menu item ID. /// </param> /// <param name="uFlags"> /// Optional flags that specify how the shortcut menu can be changed. /// </param> /// <returns> /// If successful, returns an HRESULT value that has its severity value set /// to SEVERITY_SUCCESS and its code value set to the offset of the largest /// command identifier that was assigned, plus one. /// </returns> public int QueryContextMenu( IntPtr hMenu, uint iMenu, uint idCmdFirst, uint idCmdLast, uint uFlags) { //System.Windows.Forms.MessageBox.Show("kero_query"); // If uFlags include CMF_DEFAULTONLY then we should not do anything. if (((uint)CMF.CMF_DEFAULTONLY & uFlags) != 0) { return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0)); } IntPtr hSubMenu = NativeMethods.CreatePopupMenu(); // PARENT!!! MENUITEMINFO mii2 = new MENUITEMINFO(); mii2.cbSize = (uint)Marshal.SizeOf(mii2); mii2.fMask = /*MIIM.MIIM_BITMAP | MIIM.MIIM_SUBMENU | */ MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE; //mii2.hSubMenu = hSubMenu; mii2.wID = idCmdFirst + itemCount++; mii2.fType = MFT.MFT_STRING; mii2.dwTypeData = this.menuText + " (" + this.selectedFiles.Count.ToString() + " Items)"; if (this.selectedFiles.Count == 1) { mii2.dwTypeData = this.menuText + " (1 Item)"; return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0));//there is no need to merge 1 folder... } if (this.selectedFiles.Count >= 16) { mii2.dwTypeData = this.menuText + " (16 or More Items)"; } mii2.fState = MFS.MFS_ENABLED; // Adding the POPUP Menu if (!NativeMethods.InsertMenuItem(hMenu, iMenu + 0, true, ref mii2)) { return(Marshal.GetHRForLastWin32Error()); } return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, IDM_DISPLAY + itemCount)); }
/// <summary> /// Add commands to a shortcut menu. /// </summary> /// <param name="hMenu">A handle to the shortcut menu.</param> /// <param name="iMenu"> /// The zero-based position at which to insert the first new menu item. /// </param> /// <param name="idCmdFirst"> /// The minimum value that the handler can specify for a menu item ID. /// </param> /// <param name="idCmdLast"> /// The maximum value that the handler can specify for a menu item ID. /// </param> /// <param name="uFlags"> /// Optional flags that specify how the shortcut menu can be changed. /// </param> /// <returns> /// If successful, returns an HRESULT value that has its severity value set /// to SEVERITY_SUCCESS and its code value set to the offset of the largest /// command identifier that was assigned, plus one. /// </returns> public int QueryContextMenu( IntPtr hMenu, uint iMenu, uint idCmdFirst, uint idCmdLast, uint uFlags) { // If uFlags include CMF_DEFAULTONLY then we should not do anything. if (((uint)CMF.CMF_DEFAULTONLY & uFlags) != 0) { return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0)); } // Use either InsertMenu or InsertMenuItem to add menu items. MENUITEMINFO mii = new MENUITEMINFO(); mii.cbSize = (uint)Marshal.SizeOf(mii); mii.fMask = MIIM.MIIM_BITMAP | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE; mii.wID = idCmdFirst + IDM_DISPLAY; mii.fType = MFT.MFT_STRING; mii.dwTypeData = this.menuText; mii.fState = MFS.MFS_ENABLED; mii.hbmpItem = this.menuBmp; if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii)) { return(Marshal.GetHRForLastWin32Error()); } // Add a separator. MENUITEMINFO sep = new MENUITEMINFO(); sep.cbSize = (uint)Marshal.SizeOf(sep); sep.fMask = MIIM.MIIM_TYPE; sep.fType = MFT.MFT_SEPARATOR; if (!NativeMethods.InsertMenuItem(hMenu, iMenu + 1, true, ref sep)) { return(Marshal.GetHRForLastWin32Error()); } // Return an HRESULT value with the severity set to SEVERITY_SUCCESS. // Set the code value to the offset of the largest command identifier // that was assigned, plus one (1). return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, IDM_DISPLAY + 1)); }
/// <summary> /// 将命令添加到快捷菜单. /// </summary> /// <param name="hMenu">快捷菜单的句柄.</param> /// <param name="iMenu"> /// 从零位置开始插入第一个新菜单项. /// </param> /// <param name="idCmdFirst"> /// 菜单项 ID 指定的最小值. /// </param> /// <param name="idCmdLast"> /// 菜单项 ID 指定的最大值. /// </param> /// <param name="uFlags"> /// 可选参数,用来标示快捷菜单如何改变. /// </param> /// <returns> // 如果成功返回一个HRESULT类型的值. // 将代码值设置为最大的命令标识符的偏移量将被分配并且加一. /// </returns> public int QueryContextMenu( IntPtr hMenu, uint iMenu, uint idCmdFirst, uint idCmdLast, uint uFlags) { // 如果uFlags参数包含CMF_DEFAULTONLY,我们就可以什么都不要做了. if (((uint)CMF.CMF_DEFAULTONLY & uFlags) != 0) { return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0)); } // 使用InsertMenu 或 InsertMenuItem添加菜单项. MENUITEMINFO mii = new MENUITEMINFO(); mii.cbSize = (uint)Marshal.SizeOf(mii); mii.fMask = MIIM.MIIM_ID | MIIM.MIIM_TYPE | MIIM.MIIM_STATE; mii.wID = idCmdFirst + IDM_DISPLAY; mii.fType = MFT.MFT_STRING; mii.dwTypeData = menuText; mii.fState = MFS.MFS_ENABLED; if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii)) { return(Marshal.GetHRForLastWin32Error()); } // 增加一个分隔符. MENUITEMINFO sep = new MENUITEMINFO(); sep.cbSize = (uint)Marshal.SizeOf(sep); sep.fMask = MIIM.MIIM_TYPE; sep.fType = MFT.MFT_SEPARATOR; if (!NativeMethods.InsertMenuItem(hMenu, iMenu + 1, true, ref sep)) { return(Marshal.GetHRForLastWin32Error()); } // 如果成功返回一个HRESULT类型的值. // 将代码值设置为最大的命令标识符的偏移量将被分配并且加一. return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, IDM_DISPLAY + 1)); }
public List <MENUITEMINFO> GetMenuItemInfo() { List <MENUITEMINFO> list = new List <MENUITEMINFO>(); if (_menus == null || _menus.Count == 0) { return(list); } foreach (var menu in _menus) { var menuInfo = new MENUITEMINFO(); switch (menu.MenuType) { case MenuType.Normal: menuInfo.cbSize = (uint)Marshal.SizeOf(menuInfo); menuInfo.fMask = MIIM.MIIM_BITMAP | MIIM.MIIM_SUBMENU | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE; menuInfo.wID = menu.CmdId; menuInfo.fType = MFT.MFT_STRING; menuInfo.dwTypeData = menu.Text; menuInfo.fState = MFS.MFS_ENABLED; break; case MenuType.Separator: menuInfo.cbSize = (uint)Marshal.SizeOf(menuInfo); menuInfo.fMask = MIIM.MIIM_TYPE; menuInfo.fType = MFT.MFT_SEPARATOR; break; } list.Add(menuInfo); } return(list); }
public List<MENUITEMINFO> GetMenuItemInfo() { List<MENUITEMINFO> list = new List<MENUITEMINFO>(); if (_menus == null || _menus.Count == 0) { return list; } foreach (var menu in _menus) { var menuInfo = new MENUITEMINFO(); switch (menu.MenuType) { case MenuType.Normal: menuInfo.cbSize = (uint)Marshal.SizeOf(menuInfo); menuInfo.fMask = MIIM.MIIM_BITMAP | MIIM.MIIM_SUBMENU | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE; menuInfo.wID = menu.CmdId; menuInfo.fType = MFT.MFT_STRING; menuInfo.dwTypeData = menu.Text; menuInfo.fState = MFS.MFS_ENABLED; break; case MenuType.Separator: menuInfo.cbSize = (uint)Marshal.SizeOf(menuInfo); menuInfo.fMask = MIIM.MIIM_TYPE; menuInfo.fType = MFT.MFT_SEPARATOR; break; } list.Add(menuInfo); } return list; }
/// <summary> /// Add commands to a shortcut menu. /// </summary> /// <param name="hMenu">A handle to the shortcut menu.</param> /// <param name="iMenu"> /// The zero-based position at which to insert the first new menu item. /// </param> /// <param name="idCmdFirst"> /// The minimum value that the handler can specify for a menu item ID. /// </param> /// <param name="idCmdLast"> /// The maximum value that the handler can specify for a menu item ID. /// </param> /// <param name="uFlags"> /// Optional flags that specify how the shortcut menu can be changed. /// </param> /// <returns> /// If successful, returns an HRESULT value that has its severity value set /// to SEVERITY_SUCCESS and its code value set to the offset of the largest /// command identifier that was assigned, plus one. /// </returns> public int QueryContextMenu( IntPtr hMenu, uint iMenu, uint idCmdFirst, uint idCmdLast, uint uFlags) { // If uFlags include CMF_DEFAULTONLY then we should not do anything. if (((uint)CMF.CMF_DEFAULTONLY & uFlags) != 0) { return WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0); } // ORIGINAL // Use either InsertMenu or InsertMenuItem to add menu items. /* MENUITEMINFO mii = new MENUITEMINFO(); mii.cbSize = (uint)Marshal.SizeOf(mii); mii.fMask = MIIM.MIIM_BITMAP | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE; mii.wID = idCmdFirst + itemCount++; mii.fType = MFT.MFT_STRING; mii.dwTypeData = this.menuText; mii.fState = MFS.MFS_ENABLED; mii.hbmpItem = this.menuBmp; if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii)) { return Marshal.GetHRForLastWin32Error(); } */ IntPtr hSubMenu = NativeMethods.CreatePopupMenu(); // PARENT!!! MENUITEMINFO mii2 = new MENUITEMINFO(); mii2.cbSize = (uint)Marshal.SizeOf(mii2); mii2.fMask = MIIM.MIIM_BITMAP | MIIM.MIIM_SUBMENU | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE; mii2.hSubMenu = hSubMenu; mii2.wID = idCmdFirst + itemCount; mii2.fType = MFT.MFT_STRING; mii2.dwTypeData = this.menuText; mii2.fState = MFS.MFS_ENABLED; mii2.hbmpItem = this.menuBmp; uint subItemCount = 0; SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows(); string filename; foreach (SHDocVw.InternetExplorer ie in shellWindows) { /* string[] paths = {"path1", "path2", "path3"}; foreach(string s in paths ) { */ filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); if (filename.Equals("explorer")) { string ExplorerWindowPath = ie.LocationURL; ExplorerWindowPath = ExplorerWindowPath.Replace("file:///", ""); // Filters out the current explorer window // Had a file in c:\ and it matched c:\alertus-dev... if (/*Path.GetDirectoryName(selectedFile) != Path.GetDirectoryName(ExplorerWindowPath) && */ !String.IsNullOrEmpty(ExplorerWindowPath) ) { // Subitem1 MENUITEMINFO mii3 = new MENUITEMINFO(); mii3.cbSize = (uint)Marshal.SizeOf(mii2); mii3.fMask = MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE; mii3.wID = idCmdFirst + itemCount; mii3.fType = MFT.MFT_STRING; mii3.dwTypeData = ExplorerWindowPath; mii3.fState = MFS.MFS_ENABLED; //mii3.hbmpItem = this.menuBmp; if (!NativeMethods.InsertMenuItem(hSubMenu, subItemCount++, true, ref mii3)) { return Marshal.GetHRForLastWin32Error(); } pathMap.Add((int)itemCount, ExplorerWindowPath); itemCount++; } } } // Adding the POPUP Menu if (!NativeMethods.InsertMenuItem(hMenu, iMenu + 0, true, ref mii2)) { return Marshal.GetHRForLastWin32Error(); } // Return an HRESULT value with the severity set to SEVERITY_SUCCESS. // Set the code value to the offset of the largest command identifier // that was assigned, plus one (1). return WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, IDM_DISPLAY + itemCount); }
/// <summary> /// 将命令添加到快捷菜单. /// </summary> /// <param name="hMenu">快捷菜单的句柄.</param> /// <param name="iMenu"> /// 从零位置开始插入第一个新菜单项. /// </param> /// <param name="idCmdFirst"> /// 菜单项 ID 指定的最小值. /// </param> /// <param name="idCmdLast"> /// 菜单项 ID 指定的最大值. /// </param> /// <param name="uFlags"> /// 可选参数,用来标示快捷菜单如何改变. /// </param> /// <returns> // 如果成功返回一个HRESULT类型的值. // 将代码值设置为最大的命令标识符的偏移量将被分配并且加一. /// </returns> public int QueryContextMenu( IntPtr hMenu, uint iMenu, uint idCmdFirst, uint idCmdLast, uint uFlags) { // 如果uFlags参数包含CMF_DEFAULTONLY,我们就可以什么都不要做了. if (((uint)CMF.CMF_DEFAULTONLY & uFlags) != 0) { return WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0); } // 使用InsertMenu 或 InsertMenuItem添加菜单项. MENUITEMINFO mii = new MENUITEMINFO(); mii.cbSize = (uint)Marshal.SizeOf(mii); mii.fMask = MIIM.MIIM_ID | MIIM.MIIM_TYPE | MIIM.MIIM_STATE; mii.wID = idCmdFirst + IDM_DISPLAY; mii.fType = MFT.MFT_STRING; mii.dwTypeData = menuText; mii.fState = MFS.MFS_ENABLED; if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii)) { return Marshal.GetHRForLastWin32Error(); } // 增加一个分隔符. MENUITEMINFO sep = new MENUITEMINFO(); sep.cbSize = (uint)Marshal.SizeOf(sep); sep.fMask = MIIM.MIIM_TYPE; sep.fType = MFT.MFT_SEPARATOR; if (!NativeMethods.InsertMenuItem(hMenu, iMenu + 1, true, ref sep)) { return Marshal.GetHRForLastWin32Error(); } // 如果成功返回一个HRESULT类型的值. // 将代码值设置为最大的命令标识符的偏移量将被分配并且加一. return WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, IDM_DISPLAY + 1); }
public static extern bool InsertMenuItem( IntPtr hMenu, uint uItem, [MarshalAs(UnmanagedType.Bool)] bool fByPosition, ref MENUITEMINFO mii);
public static extern bool InsertMenuItem( IntPtr hMenu, uint uItem, [MarshalAs(UnmanagedType.Bool)]bool fByPosition, ref MENUITEMINFO mii);
/// <summary> /// Add commands to a shortcut menu. /// </summary> /// <param name="hMenu">A handle to the shortcut menu.</param> /// <param name="iMenu"> /// The zero-based position at which to insert the first new menu item. /// </param> /// <param name="idCmdFirst"> /// The minimum value that the handler can specify for a menu item ID. /// </param> /// <param name="idCmdLast"> /// The maximum value that the handler can specify for a menu item ID. /// </param> /// <param name="uFlags"> /// Optional flags that specify how the shortcut menu can be changed. /// </param> /// <returns> /// If successful, returns an HRESULT value that has its severity value set /// to SEVERITY_SUCCESS and its code value set to the offset of the largest /// command identifier that was assigned, plus one. /// </returns> public int QueryContextMenu( IntPtr hMenu, uint iMenu, uint idCmdFirst, uint idCmdLast, uint uFlags) { // If uFlags include CMF_DEFAULTONLY then we should not do anything. if (((uint)CMF.CMF_DEFAULTONLY & uFlags) != 0) { return WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0); } // Use either InsertMenu or InsertMenuItem to add menu items. MENUITEMINFO mii = new MENUITEMINFO(); mii.cbSize = (uint)Marshal.SizeOf(mii); mii.fMask = MIIM.MIIM_BITMAP | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE; mii.wID = idCmdFirst + IDM_DISPLAY; mii.fType = MFT.MFT_STRING; mii.dwTypeData = this.menuText; mii.fState = MFS.MFS_ENABLED; if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii)) { return Marshal.GetHRForLastWin32Error(); } // Add a separator. MENUITEMINFO sep = new MENUITEMINFO(); sep.cbSize = (uint)Marshal.SizeOf(sep); sep.fMask = MIIM.MIIM_TYPE; sep.fType = MFT.MFT_SEPARATOR; if (!NativeMethods.InsertMenuItem(hMenu, iMenu + 1, true, ref sep)) { return Marshal.GetHRForLastWin32Error(); } // Return an HRESULT value with the severity set to SEVERITY_SUCCESS. // Set the code value to the offset of the largest command identifier // that was assigned, plus one (1). return WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, IDM_DISPLAY + 1); }
/// <summary> /// Add commands to a shortcut menu. /// </summary> /// <param name="hMenu">A handle to the shortcut menu.</param> /// <param name="iMenu"> /// The zero-based position at which to insert the first new menu item. /// </param> /// <param name="idCmdFirst"> /// The minimum value that the handler can specify for a menu item ID. /// </param> /// <param name="idCmdLast"> /// The maximum value that the handler can specify for a menu item ID. /// </param> /// <param name="uFlags"> /// Optional flags that specify how the shortcut menu can be changed. /// </param> /// <returns> /// If successful, returns an HRESULT value that has its severity value set /// to SEVERITY_SUCCESS and its code value set to the offset of the largest /// command identifier that was assigned, plus one. /// </returns> public int QueryContextMenu( IntPtr hMenu, uint iMenu, uint idCmdFirst, uint idCmdLast, uint uFlags) { // If uFlags include CMF_DEFAULTONLY then we should not do anything. if (((uint)CMF.CMF_DEFAULTONLY & uFlags) != 0) { return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0)); } // ORIGINAL // Use either InsertMenu or InsertMenuItem to add menu items. /* * MENUITEMINFO mii = new MENUITEMINFO(); * mii.cbSize = (uint)Marshal.SizeOf(mii); * mii.fMask = MIIM.MIIM_BITMAP | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE; * mii.wID = idCmdFirst + itemCount++; * mii.fType = MFT.MFT_STRING; * mii.dwTypeData = this.menuText; * mii.fState = MFS.MFS_ENABLED; * mii.hbmpItem = this.menuBmp; * if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii)) * { * return Marshal.GetHRForLastWin32Error(); * } */ IntPtr hSubMenu = NativeMethods.CreatePopupMenu(); // PARENT!!! MENUITEMINFO mii2 = new MENUITEMINFO(); mii2.cbSize = (uint)Marshal.SizeOf(mii2); mii2.fMask = MIIM.MIIM_BITMAP | MIIM.MIIM_SUBMENU | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE; mii2.hSubMenu = hSubMenu; mii2.wID = idCmdFirst + itemCount; mii2.fType = MFT.MFT_STRING; mii2.dwTypeData = this.menuText; mii2.fState = MFS.MFS_ENABLED; mii2.hbmpItem = this.menuBmp; uint subItemCount = 0; SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows(); string filename; foreach (SHDocVw.InternetExplorer ie in shellWindows) { /* * string[] paths = {"path1", "path2", "path3"}; * foreach(string s in paths ) { */ filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); if (filename.Equals("explorer")) { string ExplorerWindowPath = ie.LocationURL; ExplorerWindowPath = ExplorerWindowPath.Replace("file:///", ""); // Filters out the current explorer window // Had a file in c:\ and it matched c:\alertus-dev... if (/*Path.GetDirectoryName(selectedFile) != Path.GetDirectoryName(ExplorerWindowPath) && */ !String.IsNullOrEmpty(ExplorerWindowPath)) { // Subitem1 MENUITEMINFO mii3 = new MENUITEMINFO(); mii3.cbSize = (uint)Marshal.SizeOf(mii2); mii3.fMask = MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE; mii3.wID = idCmdFirst + itemCount; mii3.fType = MFT.MFT_STRING; mii3.dwTypeData = ExplorerWindowPath; mii3.fState = MFS.MFS_ENABLED; //mii3.hbmpItem = this.menuBmp; if (!NativeMethods.InsertMenuItem(hSubMenu, subItemCount++, true, ref mii3)) { return(Marshal.GetHRForLastWin32Error()); } pathMap.Add((int)itemCount, ExplorerWindowPath); itemCount++; } } } // Adding the POPUP Menu if (!NativeMethods.InsertMenuItem(hMenu, iMenu + 0, true, ref mii2)) { return(Marshal.GetHRForLastWin32Error()); } // Return an HRESULT value with the severity set to SEVERITY_SUCCESS. // Set the code value to the offset of the largest command identifier // that was assigned, plus one (1). return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, IDM_DISPLAY + itemCount)); }