Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

internal static partial class Interop
{
internal static partial class User32
{
[DllImport(Libraries.User32, ExactSpelling = true)]
public static extern BOOL GetMenuItemInfoW(IntPtr hmenu, int item, BOOL fByPosition, ref MENUITEMINFOW lpmii);

public static BOOL GetMenuItemInfoW(HandleRef hmenu, int item, BOOL fByPosition, ref MENUITEMINFOW lpmii)
{
BOOL result = GetMenuItemInfoW(hmenu.Handle, item, fByPosition, ref lpmii);
GC.KeepAlive(hmenu.Wrapper);
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class User32
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public unsafe struct MENUITEMINFOW
{
public uint cbSize;
public MIIM fMask;
public MFT fType;
public MFS fState;
public int wID;
public IntPtr hSubMenu;
public IntPtr hbmpChecked;
public IntPtr hbmpUnchecked;
public IntPtr dwItemData;
public char* dwTypeData;
public int cch;
public IntPtr hbmpItem;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

internal static partial class Interop
{
internal static partial class User32
{
[Flags]
public enum MFS : uint
{
GRAYED = 0x00000003,
DISABLED = GRAYED,
CHECKED = MF.CHECKED,
HILITE = MF.HILITE,
ENABLED = MF.ENABLED,
UNCHECKED = MF.UNCHECKED,
UNHILITE = MF.UNHILITE,
DEFAULT = MF.DEFAULT,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

internal static partial class Interop
{
internal static partial class User32
{
[Flags]
public enum MFT : uint
{
STRING = MF.STRING,
BITMAP = MF.BITMAP,
MENUBARBREAK = MF.MENUBARBREAK,
MENUBREAK = MF.MENUBREAK,
OWNERDRAW = MF.OWNERDRAW,
RADIOCHECK = 0x00000200,
SEPARATOR = MF.SEPARATOR,
RIGHTORDER = 0x00002000,
RIGHTJUSTIFY = MF.RIGHTJUSTIFY,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

internal static partial class Interop
{
internal static partial class User32
{
[Flags]
public enum MIIM : uint
{
STATE = 0x00000001,
ID = 0x00000002,
SUBMENU = 0x00000004,
CHECKMARKS = 0x00000008,
TYPE = 0x00000010,
DATA = 0x00000020,
STRING = 0x00000040,
BITMAP = 0x00000080,
FTYPE = 0x00000100,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,6 @@ public static uint MAKELCID(uint lgid, uint sort)

public const int MEMBERID_NIL = (-1),
ERROR_INSUFFICIENT_BUFFER = 122, //https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
MIIM_STATE = 0x00000001,
MIIM_ID = 0x00000002,
MIIM_SUBMENU = 0x00000004,
MIIM_TYPE = 0x00000010,
MIIM_DATA = 0x00000020,
MIIM_STRING = 0x00000040,
MIIM_BITMAP = 0x00000080,
MIIM_FTYPE = 0x00000100,
MFS_DISABLED = 0x00000003,
MFT_MENUBREAK = 0x00000040,
MFT_SEPARATOR = 0x00000800,
MFT_RIGHTORDER = 0x00002000,
MFT_RIGHTJUSTIFY = 0x00004000,
MDIS_ALLCHILDSTYLES = 0x0001,
MDITILE_VERTICAL = 0x0000,
MDITILE_HORIZONTAL = 0x0001,
Expand Down Expand Up @@ -599,42 +586,6 @@ public class NOTIFYICONDATA
public int dwInfoFlags;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MENUITEMINFO_T
{
public int cbSize = Marshal.SizeOf<MENUITEMINFO_T>();
public int fMask;
public int fType;
public int fState;
public int wID;
public IntPtr hSubMenu;
public IntPtr hbmpChecked;
public IntPtr hbmpUnchecked;
public IntPtr dwItemData;
public string dwTypeData;
public int cch;
}

// This version allows you to read the string that's stuffed
// in the native menu item. You have to do the marshaling on
// your own though.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MENUITEMINFO_T_RW
{
public int cbSize = Marshal.SizeOf<MENUITEMINFO_T_RW>();
public int fMask = 0;
public int fType = 0;
public int fState = 0;
public int wID = 0;
public IntPtr hSubMenu = IntPtr.Zero;
public IntPtr hbmpChecked = IntPtr.Zero;
public IntPtr hbmpUnchecked = IntPtr.Zero;
public IntPtr dwItemData = IntPtr.Zero;
public IntPtr dwTypeData = IntPtr.Zero;
public int cch = 0;
public IntPtr hbmpItem = IntPtr.Zero; // requires WINVER > 5
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MSAAMENUINFO
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,6 @@ public static int DragQueryFileLongPath(HandleRef hDrop, int iFile, StringBuilde
[DllImport(ExternDll.Shell32, CharSet = CharSet.Auto)]
public static extern int Shell_NotifyIcon(int message, NativeMethods.NOTIFYICONDATA pnid);

[DllImport(Libraries.User32, CharSet = CharSet.Auto)]
public static extern BOOL GetMenuItemInfo(IntPtr hMenu, int uItem, bool fByPosition, [In, Out] NativeMethods.MENUITEMINFO_T lpmii);

public static BOOL GetMenuItemInfo(HandleRef hMenu, int uItem, bool fByPosition, NativeMethods.MENUITEMINFO_T lpmii)
{
BOOL result = GetMenuItemInfo(hMenu.Handle, uItem, fByPosition, lpmii);
GC.KeepAlive(hMenu.Wrapper);
return result;
}

[DllImport(Libraries.User32, CharSet = CharSet.Auto)]
public static extern BOOL GetMenuItemInfo(IntPtr hMenu, int uItem, bool fByPosition, [In, Out] NativeMethods.MENUITEMINFO_T_RW lpmii);

public static BOOL GetMenuItemInfo(HandleRef hMenu, int uItem, bool fByPosition, NativeMethods.MENUITEMINFO_T_RW lpmii)
{
BOOL result = GetMenuItemInfo(hMenu.Handle, uItem, fByPosition, lpmii);
GC.KeepAlive(hMenu.Wrapper);
return result;
}

[DllImport(ExternDll.Comdlg32, SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] NativeMethods.OPENFILENAME_I ofn);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private ToolStripScrollButton UpScrollButton
/// targetWindow is the window to send WM_COMMAND, WM_SYSCOMMAND to
/// hmenu is a handle to the native menu.
/// </summary>
internal static ToolStripDropDownMenu FromHMenu(IntPtr hmenu, IWin32Window targetWindow)
internal unsafe static ToolStripDropDownMenu FromHMenu(IntPtr hmenu, IWin32Window targetWindow)
{
ToolStripDropDownMenu managedDropDown = new ToolStripDropDownMenu();
managedDropDown.SuspendLayout();
Expand All @@ -271,43 +271,40 @@ internal static ToolStripDropDownMenu FromHMenu(IntPtr hmenu, IWin32Window targe
for (int i = 0; i < count; i++)
{
// peek at the i'th item.
NativeMethods.MENUITEMINFO_T_RW info = new NativeMethods.MENUITEMINFO_T_RW
var info = new User32.MENUITEMINFOW
{
cbSize = Marshal.SizeOf<NativeMethods.MENUITEMINFO_T_RW>(),
fMask = NativeMethods.MIIM_FTYPE,
fType = NativeMethods.MIIM_FTYPE
cbSize = (uint)sizeof(User32.MENUITEMINFOW),
fMask = User32.MIIM.FTYPE
};
UnsafeNativeMethods.GetMenuItemInfo(hmenu, i, /*fByPosition=*/ true, info);
User32.GetMenuItemInfoW(hmenu, i, /*fByPosition=*/ BOOL.TRUE, ref info);

if (info.fType == NativeMethods.MFT_SEPARATOR)
if (info.fType == User32.MFT.SEPARATOR)
{
// its a separator.
itemToAdd = new ToolStripSeparator();
}
else
{
// its a menu item... lets fish out the command id
info = new NativeMethods.MENUITEMINFO_T_RW
info = new User32.MENUITEMINFOW
{
cbSize = Marshal.SizeOf<NativeMethods.MENUITEMINFO_T_RW>(),
fMask = NativeMethods.MIIM_ID,
fType = NativeMethods.MIIM_ID
cbSize = (uint)sizeof(User32.MENUITEMINFOW),
fMask = User32.MIIM.ID
};

UnsafeNativeMethods.GetMenuItemInfo(hmenu, i, /*fByPosition=*/ true, info);
User32.GetMenuItemInfoW(hmenu, i, /*fByPosition=*/ BOOL.TRUE, ref info);

// create the managed object - toolstripmenu item knows how to grok hmenu for information.
itemToAdd = new ToolStripMenuItem(hmenu, info.wID, targetWindow);

// if there is a submenu fetch it.
info = new NativeMethods.MENUITEMINFO_T_RW
info = new User32.MENUITEMINFOW
{
cbSize = Marshal.SizeOf<NativeMethods.MENUITEMINFO_T_RW>(),
fMask = NativeMethods.MIIM_SUBMENU,
fType = NativeMethods.MIIM_SUBMENU
cbSize = (uint)sizeof(User32.MENUITEMINFOW),
fMask = User32.MIIM.SUBMENU
};

UnsafeNativeMethods.GetMenuItemInfo(hmenu, i, /*fByPosition=*/ true, info);
User32.GetMenuItemInfoW(hmenu, i, /*fByPosition=*/ BOOL.TRUE, ref info);

if (info.hSubMenu != IntPtr.Zero)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,27 +736,26 @@ protected override void Dispose(bool disposing) {
base.Dispose(disposing);
}

private bool GetNativeMenuItemEnabled()
private unsafe bool GetNativeMenuItemEnabled()
{
if (nativeMenuCommandID == -1 || nativeMenuHandle == IntPtr.Zero)
{
Debug.Fail("why were we called to fetch native menu item info with nothing assigned?");
return false;
}
NativeMethods.MENUITEMINFO_T_RW info = new NativeMethods.MENUITEMINFO_T_RW

var info = new User32.MENUITEMINFOW
{
cbSize = Marshal.SizeOf<NativeMethods.MENUITEMINFO_T_RW>(),
fMask = NativeMethods.MIIM_STATE,
fType = NativeMethods.MIIM_STATE,
cbSize = (uint)sizeof(User32.MENUITEMINFOW),
fMask = User32.MIIM.STATE,
wID = nativeMenuCommandID
};
UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, nativeMenuHandle), nativeMenuCommandID, /*fByPosition instead of ID=*/ false, info);

return ((info.fState & NativeMethods.MFS_DISABLED) == 0);
User32.GetMenuItemInfoW(new HandleRef(this, nativeMenuHandle), nativeMenuCommandID, /*fByPosition instead of ID=*/ BOOL.FALSE, ref info);
return (info.fState & User32.MFS.DISABLED) == 0;
}

// returns text and shortcut separated by tab.
private string GetNativeMenuItemTextAndShortcut()
private unsafe string GetNativeMenuItemTextAndShortcut()
{
if (nativeMenuCommandID == -1 || nativeMenuHandle == IntPtr.Zero)
{
Expand All @@ -766,33 +765,30 @@ private string GetNativeMenuItemTextAndShortcut()
string text = null;

// fetch the string length
NativeMethods.MENUITEMINFO_T_RW info = new NativeMethods.MENUITEMINFO_T_RW
var info = new User32.MENUITEMINFOW
{
fMask = NativeMethods.MIIM_STRING,
fType = NativeMethods.MIIM_STRING,
wID = nativeMenuCommandID,
dwTypeData = IntPtr.Zero
cbSize = (uint)sizeof(User32.MENUITEMINFOW),
fMask = User32.MIIM.STRING,
wID = nativeMenuCommandID
};
UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, nativeMenuHandle), nativeMenuCommandID, /*fByPosition instead of ID=*/ false, info);
User32.GetMenuItemInfoW(new HandleRef(this, nativeMenuHandle), nativeMenuCommandID, /*fByPosition instead of ID=*/ BOOL.FALSE, ref info);

if (info.cch > 0)
{
// fetch the string
info.cch += 1; // according to MSDN we need to increment the count we receive by 1.
info.wID = nativeMenuCommandID;
IntPtr allocatedStringBuffer = Marshal.AllocCoTaskMem(info.cch * sizeof(char));
info.dwTypeData = allocatedStringBuffer;
info.dwTypeData = (char*)allocatedStringBuffer;

try
{
UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, nativeMenuHandle), nativeMenuCommandID, /*fByPosition instead of ID=*/ false, info);
User32.GetMenuItemInfoW(new HandleRef(this, nativeMenuHandle), nativeMenuCommandID, /*fByPosition instead of ID=*/ BOOL.FALSE, ref info);

// convert the string into managed data.
if (info.dwTypeData != IntPtr.Zero)
if (info.dwTypeData != null)
{
// we have to use PtrToStringAuto as we can't use Marshal.SizeOf to determine
// the size of the struct with a StringBuilder member.
text = Marshal.PtrToStringAuto(info.dwTypeData, info.cch);
text = new string(info.dwTypeData, 0, info.cch);
}
}
finally
Expand All @@ -816,13 +812,12 @@ private Image GetNativeMenuItemImage()
return null;
}

NativeMethods.MENUITEMINFO_T_RW info = new NativeMethods.MENUITEMINFO_T_RW
var info = new User32.MENUITEMINFOW
{
fMask = NativeMethods.MIIM_BITMAP,
fType = NativeMethods.MIIM_BITMAP,
fMask = User32.MIIM.BITMAP,
wID = nativeMenuCommandID
};
UnsafeNativeMethods.GetMenuItemInfo(new HandleRef(this, nativeMenuHandle), nativeMenuCommandID, /*fByPosition instead of ID=*/ false, info);
User32.GetMenuItemInfoW(new HandleRef(this, nativeMenuHandle), nativeMenuCommandID, /*fByPosition instead of ID=*/ BOOL.FALSE, ref info);

if (info.hbmpItem != IntPtr.Zero && info.hbmpItem.ToInt32() > (int)User32.HBMMENU.POPUP_MINIMIZE)
{
Expand Down