public static extern HWND CreateWindowExW( DWORD dwExStyle, [MarshalAs(UnmanagedType.LPWStr)] string lpClassName, [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName, Win32WindowStyle dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam );
public static MenuItemInfo[] GetMenuItems(HMENU hMenu) { if (hMenu.IsNull) { return(new MenuItemInfo[0]); } var SubMenus = new MenuItemInfo[GetMenuItemCount(hMenu)]; for (uint i = 0; i < SubMenus.Length; i++) { SubMenus[i] = new MenuItemInfo(hMenu, i); } return(SubMenus); }
internal MenuItemInfo(HMENU hMenu, uint idx) { using var strmem = new SafeHGlobalHandle(512); var mii = new MENUITEMINFO { cbSize = (uint)Marshal.SizeOf(typeof(MENUITEMINFO)), fMask = MenuItemInfoMask.MIIM_ID | MenuItemInfoMask.MIIM_SUBMENU | MenuItemInfoMask.MIIM_FTYPE | MenuItemInfoMask.MIIM_STRING | MenuItemInfoMask.MIIM_STATE | MenuItemInfoMask.MIIM_BITMAP, fType = MenuItemType.MFT_STRING, dwTypeData = (IntPtr)strmem, cch = strmem.Size / (uint)StringHelper.GetCharSize() }; Win32Error.ThrowLastErrorIfFalse(GetMenuItemInfo(hMenu, idx, true, ref mii)); Id = mii.wID; Text = mii.fType.IsFlagSet(MenuItemType.MFT_SEPARATOR) ? "-" : mii.fType.IsFlagSet(MenuItemType.MFT_STRING) ? strmem.ToString(-1, CharSet.Auto) : ""; Type = mii.fType; State = mii.fState; BitmapHandle = mii.hbmpItem; SubMenus = GetMenuItems(mii.hSubMenu); }
HRESULT IContextMenu.QueryContextMenu(HMENU hmenu, uint indexMenu, uint idCmdFirst, uint idCmdLast, CMF uFlags) { throw new NotImplementedException(); //if (!uFlags.IsFlagSet(CMF.CMF_DEFAULTONLY) && base.MenuItems.Count > 0) //{ // var cmdId = idCmdFirst; // var idx = indexMenu; // using var str = new SafeCoTaskMemString(512, CharSet.Auto); // var info = new MENUITEMINFO(0) { dwTypeData = (IntPtr)str }; // foreach (var menuItem in base.MenuItems.OfType<MenuItem>().Where(i => i.Visible)) // { // info.cch = (uint)str.Capacity; // info.fMask = (MenuItemInfoMask)0x1ffU; // GetMenuItemInfo(Handle, (uint)menuItem.Index, true, ref info); // info.wID = cmdId++; // InsertMenuItem(hmenu, idx++, true, ref info); // } // return HRESULT.Make(false, HRESULT.FacilityCode.FACILITY_NULL, cmdId - idCmdFirst + 1); //} //return HRESULT.Make(false, HRESULT.FacilityCode.FACILITY_NULL, 0); }
public HRESULT SetMenu(HMENU hmenu, HWND hwnd, [NativeTypeName("DWORD")] uint dwFlags) { return(((delegate * unmanaged <IShellMenu *, HMENU, HWND, uint, int>)(lpVtbl[7]))((IShellMenu *)Unsafe.AsPointer(ref this), hmenu, hwnd, dwFlags)); }
public HWND Create(HWND hwndParent, LPCTSTR strName, DWORD dwStyle, DWORD dwExStyle, int x, int y, int cx, int cy, HMENU hMenu) { if (GetSuperClassName() != null && !RegisterSuperclass()) { return(IntPtr.Zero); } if (GetSuperClassName() == null && !RegisterWindowClass()) { return(IntPtr.Zero); } m_hWnd = NativeMethods.CreateWindowEx(dwExStyle, GetWindowClassName(), strName, dwStyle, x, y, cx, cy, hwndParent, hMenu, DuiUIManager.GetInstance(), m_pointer); Debug.Assert(m_hWnd != IntPtr.Zero); return(m_hWnd); }
public static extern int GetMenuItemCount(HMENU hmenu);
public HRESULT InsertMenuEntries(IUnknown *punk, HMENU hmenu, int nPos, int idFirst, int idLast, [NativeTypeName("DWORD")] uint dwFlags) { return(((delegate * unmanaged <ITravelLog *, IUnknown *, HMENU, int, int, int, uint, int>)(lpVtbl[10]))((ITravelLog *)Unsafe.AsPointer(ref this), punk, hmenu, nPos, idFirst, idLast, dwFlags)); }
public static HWND CreateWindowW([NativeTypeName("LPCWSTR")] ushort *lpClassName, [NativeTypeName("LPCWSTR")] ushort *lpWindowName, [NativeTypeName("DWORD")] uint dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, [NativeTypeName("LPVOID")] void *lpParam) => CreateWindowExW(0, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
public static extern bool EnableMenuItem(HMENU hMenu, uint uIDEnableItem, uint uEnable);
public HRESULT SetMenuSB(HMENU hmenuShared, [NativeTypeName("HOLEMENU")] HGLOBAL holemenuRes, HWND hwndActiveObject) { return(((delegate * unmanaged <IShellBrowser *, HMENU, HGLOBAL, HWND, int>)(lpVtbl[6]))((IShellBrowser *)Unsafe.AsPointer(ref this), hmenuShared, holemenuRes, hwndActiveObject)); }
public HRESULT RemoveMenusSB(HMENU hmenuShared) { return(((delegate * unmanaged <IShellBrowser *, HMENU, int>)(lpVtbl[7]))((IShellBrowser *)Unsafe.AsPointer(ref this), hmenuShared)); }
public HRESULT InsertMenusSB(HMENU hmenuShared, [NativeTypeName("LPOLEMENUGROUPWIDTHS")] OLEMENUGROUPWIDTHS *lpMenuWidths) { return(((delegate * unmanaged <IShellBrowser *, HMENU, OLEMENUGROUPWIDTHS *, int>)(lpVtbl[5]))((IShellBrowser *)Unsafe.AsPointer(ref this), hmenuShared, lpMenuWidths)); }
/// <summary>Gets the menu information set by calling IShellMenu::SetMenu.</summary> /// <param name="phmenu"> /// <para>Type: <c>HMENU*</c></para> /// <para> /// When this method returns, contains a pointer to an <c>HMENU</c> value that receives the hmenu value that you specified when /// you called IShellMenu::SetMenu. This value can be <c>NULL</c>. /// </para> /// </param> /// <param name="phwnd"> /// <para>Type: <c>HWND*</c></para> /// <para> /// When this method returns, contains a pointer to an <c>HWND</c> value that receives the hwnd value that you specified when /// you called IShellMenu::SetMenu. This value can be <c>NULL</c>. /// </para> /// </param> /// <param name="pdwFlags"> /// <para>Type: <c>DWORD*</c></para> /// <para> /// When this method returns, contains a pointer to a <c>DWORD</c> value that receives the dwFlags value that you specified when /// you called IShellMenu::SetMenu. This value can be <c>NULL</c>. /// </para> /// </param> /// <returns> /// <para>Type: <c>HRESULT</c></para> /// <para>If this method succeeds, it returns <c>S_OK</c>. Otherwise, it returns an <c>HRESULT</c> error code.</para> /// </returns> // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellmenu-getmenu HRESULT GetMenu( HMENU // *phmenu, HWND *phwnd, DWORD *pdwFlags ); void GetMenu(out HMENU phmenu, out HWND phwnd, out SMSET pdwFlags);
/// <summary>Appends a static menu to the menu band.</summary> /// <param name="hmenu"> /// <para>Type: <c>HMENU</c></para> /// <para>The handle of the static menu that is to be appended. This value can be <c>NULL</c>.</para> /// </param> /// <param name="hwnd"> /// <para>Type: <c>HWND</c></para> /// <para>The <c>HWND</c> of the owner window. This value can be <c>NULL</c>.</para> /// </param> /// <param name="dwFlags"> /// <para>Type: <c>DWORD</c></para> /// <para>Flags that specify how the menu operates.</para> /// <para>SMSET_BOTTOM</para> /// <para>Attach the menu to the bottom of the parent menu.</para> /// <para>SMSET_TOP</para> /// <para>Attach the menu to the top of the parent menu.</para> /// <para>SMSET_DONTOWN</para> /// <para>The menu band does not own the menu named in hwnd, so should that menu eventually be replaced, it should not be destroyed.</para> /// </param> // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellmenu-setmenu HRESULT SetMenu( HMENU // hmenu, HWND hwnd, DWORD dwFlags ); void SetMenu([In, Optional] HMENU hmenu, [In, Optional] HWND hwnd, SMSET dwFlags);
public HRESULT RemoveMenus(HMENU hmenuShared) { return(((delegate * unmanaged <IOleInPlaceFrame *, HMENU, int>)(lpVtbl[11]))((IOleInPlaceFrame *)Unsafe.AsPointer(ref this), hmenuShared)); }
private static ContextMenuPackage[] FetchContextMenuCore(Shell32.IContextMenu Context, HMENU Menu) { int MenuItemNum = User32.GetMenuItemCount(Menu); List <ContextMenuPackage> MenuItems = new List <ContextMenuPackage>(MenuItemNum); for (uint i = 0; i < MenuItemNum; i++) { IntPtr DataHandle = Marshal.AllocHGlobal(BufferSize); try { User32.MENUITEMINFO Info = new User32.MENUITEMINFO { cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(User32.MENUITEMINFO))), fMask = User32.MenuItemInfoMask.MIIM_ID | User32.MenuItemInfoMask.MIIM_SUBMENU | User32.MenuItemInfoMask.MIIM_FTYPE | User32.MenuItemInfoMask.MIIM_STRING | User32.MenuItemInfoMask.MIIM_STATE | User32.MenuItemInfoMask.MIIM_BITMAP, dwTypeData = DataHandle, cch = BufferSize }; if (User32.GetMenuItemInfo(Menu, i, true, ref Info)) { if (Info.fType.IsFlagSet(User32.MenuItemType.MFT_STRING) && !Info.fState.IsFlagSet(User32.MenuItemState.MFS_DISABLED)) { IntPtr VerbHandle = Marshal.AllocHGlobal(BufferSize); try { string Verb = Context.GetCommandString(new IntPtr(Info.wID), Shell32.GCS.GCS_VERBW, IntPtr.Zero, VerbHandle, Convert.ToUInt32(BufferSize)).Succeeded ? Marshal.PtrToStringUni(VerbHandle) : string.Empty; if (!VerbFilterHashSet.Contains(Verb.ToLower())) { try { string Name = Marshal.PtrToStringUni(DataHandle); if (!string.IsNullOrEmpty(Name) && !NameFilterHashSet.Contains(Name)) { ContextMenuPackage Package = new ContextMenuPackage { Name = Regex.Replace(Name, @"\(&\S*\)|&", string.Empty), Id = Convert.ToInt32(Info.wID), Verb = Verb }; if (Info.hbmpItem != HBITMAP.NULL && ((IntPtr)Info.hbmpItem).ToInt64() != -1) { using (Bitmap OriginBitmap = Info.hbmpItem.ToBitmap()) { BitmapData OriginData = OriginBitmap.LockBits(new Rectangle(0, 0, OriginBitmap.Width, OriginBitmap.Height), ImageLockMode.ReadOnly, OriginBitmap.PixelFormat); try { using (Bitmap ArgbBitmap = new Bitmap(OriginBitmap.Width, OriginBitmap.Height, OriginData.Stride, PixelFormat.Format32bppArgb, OriginData.Scan0)) using (MemoryStream Stream = new MemoryStream()) { ArgbBitmap.Save(Stream, ImageFormat.Png); Package.IconData = Stream.ToArray(); } } finally { OriginBitmap.UnlockBits(OriginData); } } } else { Package.IconData = Array.Empty <byte>(); } if (Info.hSubMenu != HMENU.NULL) { Package.SubMenus = FetchContextMenuCore(Context, Info.hSubMenu); } else { Package.SubMenus = Array.Empty <ContextMenuPackage>(); } MenuItems.Add(Package); } } catch { continue; } } } finally { Marshal.FreeHGlobal(VerbHandle); } } } } finally { Marshal.FreeHGlobal(DataHandle); } } return(MenuItems.ToArray()); }
public static extern bool DestroyMenu(HMENU hmenu);
public static extern bool ModifyMenu(HMENU hmenu, uint uPosition, uint uFlags, UIntPtr uIDNewItem, string lpNewItem);
public static extern bool GetMenuInfo(HMENU hmenu, [In, Out] ref MENUINFO mi);
public static extern bool SetMenu(HWND hwnd, HMENU hmenu);
public static extern HMENU GetSubMenu(HMENU hmenu, int nPos);
public HRESULT InsertMenus(HMENU hmenuShared, [NativeTypeName("LPOLEMENUGROUPWIDTHS")] OLEMENUGROUPWIDTHS *lpMenuWidths) { return(((delegate * unmanaged <IOleInPlaceFrame *, HMENU, OLEMENUGROUPWIDTHS *, int>)(lpVtbl[9]))((IOleInPlaceFrame *)Unsafe.AsPointer(ref this), hmenuShared, lpMenuWidths)); }
public static extern bool RemoveMenu(HMENU hmenu, uint uPosition, uint uFlags);
private static extern HWND CreateWindowEx( DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, Int32 x, Int32 y, Int32 nWidth, Int32 nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);
public static extern bool SetMenuInfo(HMENU hmenu, [In] ref MENUINFO mi);
public static extern HWND CreateWindowEx( DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, Int32 x, Int32 y, Int32 nWidth, Int32 nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, [MarshalAs(UnmanagedType.AsAny)] object pvParam);
private static void EnumMenuItems( Shell32.IContextMenu cMenu, HMENU hMenu, List <Win32ContextMenuItem> menuItemsResult, Func <string, bool> itemFilter = null) { var itemCount = User32.GetMenuItemCount(hMenu); var mii = new User32.MENUITEMINFO(); mii.cbSize = (uint)Marshal.SizeOf(mii); mii.fMask = User32.MenuItemInfoMask.MIIM_BITMAP | User32.MenuItemInfoMask.MIIM_FTYPE | User32.MenuItemInfoMask.MIIM_STRING | User32.MenuItemInfoMask.MIIM_ID | User32.MenuItemInfoMask.MIIM_SUBMENU; for (uint ii = 0; ii < itemCount; ii++) { var menuItem = new ContextMenuItem(); var container = new SafeCoTaskMemString(512); mii.dwTypeData = (IntPtr)container; mii.cch = (uint)container.Capacity - 1; // https://devblogs.microsoft.com/oldnewthing/20040928-00/?p=37723 var retval = User32.GetMenuItemInfo(hMenu, ii, true, ref mii); if (!retval) { container.Dispose(); continue; } menuItem.Type = (MenuItemType)mii.fType; menuItem.ID = (int)(mii.wID - 1); // wID - idCmdFirst if (menuItem.Type == MenuItemType.MFT_STRING) { Debug.WriteLine("Item {0} ({1}): {2}", ii, mii.wID, mii.dwTypeData); menuItem.Label = mii.dwTypeData; menuItem.CommandString = GetCommandString(cMenu, mii.wID - 1); if (itemFilter != null && (itemFilter(menuItem.CommandString) || itemFilter(menuItem.Label))) { // Skip items implemented in UWP container.Dispose(); continue; } if (mii.hbmpItem != HBITMAP.NULL && !Enum.IsDefined(typeof(HBITMAP_HMENU), ((IntPtr)mii.hbmpItem).ToInt64())) { var bitmap = GetBitmapFromHBitmap(mii.hbmpItem); if (bitmap != null) { menuItem.Icon = bitmap; } } if (mii.hSubMenu != HMENU.NULL) { Debug.WriteLine("Item {0}: has submenu", ii); var subItems = new List <Win32ContextMenuItem>(); try { (cMenu as Shell32.IContextMenu2)?.HandleMenuMsg((uint)User32.WindowMessage.WM_INITMENUPOPUP, (IntPtr)mii.hSubMenu, new IntPtr(ii)); } catch (NotImplementedException) { // Only for dynamic/owner drawn? (open with, etc) } EnumMenuItems(cMenu, mii.hSubMenu, subItems, itemFilter); menuItem.SubItems = subItems; Debug.WriteLine("Item {0}: done submenu", ii); } } else { Debug.WriteLine("Item {0}: {1}", ii, mii.fType.ToString()); } container.Dispose(); menuItemsResult.Add(menuItem); } }
private static ContextMenuPackage[] FetchContextMenuCore(Shell32.IContextMenu Context, HMENU Menu, string[] RelatedPath, bool IncludeExtensionItem) { int MenuItemNum = User32.GetMenuItemCount(Menu); List <ContextMenuPackage> MenuItems = new List <ContextMenuPackage>(MenuItemNum); for (uint i = 0; i < MenuItemNum; i++) { IntPtr DataHandle = Marshal.AllocCoTaskMem(BufferSize); try { User32.MENUITEMINFO Info = new User32.MENUITEMINFO { cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(User32.MENUITEMINFO))), fMask = User32.MenuItemInfoMask.MIIM_ID | User32.MenuItemInfoMask.MIIM_SUBMENU | User32.MenuItemInfoMask.MIIM_FTYPE | User32.MenuItemInfoMask.MIIM_STRING | User32.MenuItemInfoMask.MIIM_STATE | User32.MenuItemInfoMask.MIIM_BITMAP, dwTypeData = DataHandle, cch = CchMax }; if (User32.GetMenuItemInfo(Menu, i, true, ref Info)) { if (Info.fType.IsFlagSet(User32.MenuItemType.MFT_STRING) && !Info.fState.IsFlagSet(User32.MenuItemState.MFS_DISABLED)) { IntPtr VerbWHandle = IntPtr.Zero; IntPtr VerbAHandle = IntPtr.Zero; string Verb = null; try { VerbWHandle = Marshal.AllocCoTaskMem(BufferSize); if (Context.GetCommandString(new IntPtr(Info.wID), Shell32.GCS.GCS_VERBW, IntPtr.Zero, VerbWHandle, CchMax).Succeeded) { Verb = Marshal.PtrToStringUni(VerbWHandle); } if (string.IsNullOrEmpty(Verb)) { VerbAHandle = Marshal.AllocCoTaskMem(BufferSize); if (Context.GetCommandString(new IntPtr(Info.wID), Shell32.GCS.GCS_VERBA, IntPtr.Zero, VerbAHandle, CchMax).Succeeded) { Verb = Marshal.PtrToStringAnsi(VerbAHandle); } } } catch (AccessViolationException AVE) { Verb = null; LogTracer.Log(AVE, "Could not get verb from context menu item"); } finally { if (VerbAHandle != IntPtr.Zero) { Marshal.FreeCoTaskMem(VerbAHandle); } if (VerbWHandle != IntPtr.Zero) { Marshal.FreeCoTaskMem(VerbWHandle); } } Verb ??= string.Empty; if (!VerbFilterHashSet.Contains(Verb.ToLower())) { try { string Name = Marshal.PtrToStringUni(DataHandle); if (!string.IsNullOrEmpty(Name) && !NameFilterHashSet.Contains(Name)) { ContextMenuPackage Package = new ContextMenuPackage { Name = Regex.Replace(Name, @"\(&\S*\)|&", string.Empty), Id = Convert.ToInt32(Info.wID), Verb = Verb, IncludeExtensionItem = IncludeExtensionItem, RelatedPath = RelatedPath }; if (Info.hbmpItem != HBITMAP.NULL && ((IntPtr)Info.hbmpItem).ToInt64() != -1) { using (Bitmap OriginBitmap = Info.hbmpItem.ToBitmap()) { BitmapData OriginData = OriginBitmap.LockBits(new Rectangle(0, 0, OriginBitmap.Width, OriginBitmap.Height), ImageLockMode.ReadOnly, OriginBitmap.PixelFormat); try { using (Bitmap ArgbBitmap = new Bitmap(OriginBitmap.Width, OriginBitmap.Height, OriginData.Stride, PixelFormat.Format32bppArgb, OriginData.Scan0)) using (MemoryStream Stream = new MemoryStream()) { ArgbBitmap.Save(Stream, ImageFormat.Png); Package.IconData = Stream.ToArray(); } } finally { OriginBitmap.UnlockBits(OriginData); } } } else { Package.IconData = Array.Empty <byte>(); } if (Info.hSubMenu != HMENU.NULL) { Package.SubMenus = FetchContextMenuCore(Context, Info.hSubMenu, RelatedPath, IncludeExtensionItem); } else { Package.SubMenus = Array.Empty <ContextMenuPackage>(); } MenuItems.Add(Package); } } catch { continue; } } } } } catch (Exception ex) { LogTracer.Log(ex, "Exception was threw when fetching the context menu item"); } finally { Marshal.FreeCoTaskMem(DataHandle); } } return(MenuItems.ToArray()); }
public HRESULT QueryContextMenu(HMENU hmenu, uint indexMenu, uint idCmdFirst, uint idCmdLast, uint uFlags) { return(((delegate * unmanaged <IContextMenu3 *, HMENU, uint, uint, uint, uint, int>)(lpVtbl[3]))((IContextMenu3 *)Unsafe.AsPointer(ref this), hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags)); }
public static extern bool AppendMenu(HMENU hmenu, uint uFlags, UIntPtr uIDNewItem, string lpNewItem);
public MenuHandle(HMENU handle, bool ownsHandle = true) { HMENU = handle; _ownsHandle = ownsHandle; }
public static extern bool CheckMenuItem(HMENU hMenu, uint uIDEnableItem, uint uCheck);
public static extern HWND CreateWindowEx(uint dwExStyle, string stClassName, string stTitle, uint dwStyle, int x, int y, int w, int h, HWND hwndParent, HMENU hmenu, HINSTANCE hinst, [MarshalAs(UnmanagedType.AsAny)] object pvParam);
public HRESULT SetMenu(HMENU hmenuShared, [NativeTypeName("HOLEMENU")] HGLOBAL holemenu, HWND hwndActiveObject) { return(((delegate * unmanaged <IOleInPlaceFrame *, HMENU, HGLOBAL, HWND, int>)(lpVtbl[10]))((IOleInPlaceFrame *)Unsafe.AsPointer(ref this), hmenuShared, holemenu, hwndActiveObject)); }