gpt4 book ai didi

c# - Win32 API GetMenuItemInfo 仅返回项目文本的第一个字符

转载 作者:行者123 更新时间:2023-11-30 15:29:59 27 4
gpt4 key购买 nike

我正在尝试使用 GetMenuItemInfo API 收集菜单项的文本。

这是我正在使用的代码:

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool GetMenuItemInfo(IntPtr hMenu, uint uItem, bool fByPosition, ref MENUITEMINFO lpmii);

[StructLayout(LayoutKind.Sequential)]
public struct MENUITEMINFO
{
public uint cbSize;
public uint fMask;
public uint fType;
public uint fState;
public uint wID;
public IntPtr hSubMenu;
public IntPtr hbmpChecked;
public IntPtr hbmpUnchecked;
public IntPtr dwItemData;
public String dwTypeData;
public uint cch;
public IntPtr hbmpItem;

// Return the size of the structure
public static uint sizeOf
{
get { return (uint)Marshal.SizeOf(typeof(MENUITEMINFO)); }
}
}

MENUITEMINFO mif = new MENUITEMINFO();
mif.cbSize = MENUITEMINFO.sizeOf;
mif.fMask = MIIM_STRING;
mif.fType = MFT_STRING;
mif.dwTypeData = null;
bool res = Win32.GetMenuItemInfo(hMenu, (uint)0, true, ref mif); //To load cch into memory

mif.cch += 1;
mif.fMask = MIIM_STRING;
mif.fType = MFT_STRING;
mif.dwTypeData = new String(' ', (Int32)(mif.cch));

res = Win32.GetMenuItemInfo(hMenu, (uint)i, true, ref mif); //To fill dwTypeData

不幸的是,在这些行之后,dwTypeData 结果是一个只有 1 个字符而不是 mif.cch 字符的字符串。注意:这个字符是菜单项的第一个字符!

返回 MENUITEMINFO 结构时,数据编码似乎有一些错位,不是吗?

请告诉我如何解决此类问题!

最佳

格格西

最佳答案

您的 header 翻译非常好,但您的dwTypeData 不太正确。不能使用 string 从调用者到被调用者编码。您需要像这样通过手动编码来做到这一点。

[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool GetMenuItemInfo(IntPtr hMenu, int uItem, bool fByPosition, MENUITEMINFO lpmii);

[StructLayout(LayoutKind.Sequential)]
public class MENUITEMINFO
{
public int cbSize;
public uint fMask;
public uint fType;
public uint fState;
public uint wID;
public IntPtr hSubMenu;
public IntPtr hbmpChecked;
public IntPtr hbmpUnchecked;
public IntPtr dwItemData;
public IntPtr dwTypeData;
public uint cch;
public IntPtr hbmpItem;

public MENUITEMINFO()
{
cbSize = Marshal.SizeOf(typeof(MENUITEMINFO));
}
}

....

MENUITEMINFO mif = new MENUITEMINFO();
mif.fMask = MIIM_STRING;
mif.fType = MFT_STRING;

mif.dwTypeData = IntPtr.Zero;
bool res = GetMenuItemInfo(hMenu, 0, true, mif);
if (!res)
throw new Win32Exception();
mif.cch++;
mif.dwTypeData = Marshal.AllocHGlobal((IntPtr)(mif.cch*2));
try
{
res = GetMenuItemInfo(hMenu, 0, true, mif);
if (!res)
throw new Win32Exception();
string caption = Marshal.PtrToStringUni(mif.dwTypeData);
}
finally
{
Marshal.FreeHGlobal(mif.dwTypeData);
}

关于c# - Win32 API GetMenuItemInfo 仅返回项目文本的第一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22852461/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com