gpt4 book ai didi

C# ContextMenu 删除默认选择

转载 作者:行者123 更新时间:2023-11-30 22:36:14 24 4
gpt4 key购买 nike

好吧,这个问题似乎太简单了,但我已经浪费了足够多的时间来寻找如何做到这一点。我在移动设备上使用 CE 6.5,并且我有一个包含六个 MenuItem 的 ContextMenu。当菜单弹出时,列表中的第一个项目会自动突出显示。我想删除这个突出显示,因为它让我的一些用户感到困惑,认为它是当前状态。我查看了 ContextMenu 及其所有变量和 MenuItem,但没有找到如何删除第一项的自动突出显示。主菜单也是如此。

最佳答案

不幸的是,我认为答案是你做不到。今天下午我付出了很大的努力来获得该菜单,但我无法获得操作系统允许我使用的有效 HMENU。如果你想继续尝试追逐我在下面的代码上的路径,但我真的认为这是一个死胡同。在这一点上,如果您真的需要该功能,我会考虑 P/Invoking 菜单的所有内容(创建、填充等)。

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;

using UINT = System.UInt32;
using HMENU = System.IntPtr;
using HBITMAP = System.IntPtr;
using DWORD = System.UInt32;
using LPTSTR = System.IntPtr;

namespace MenuTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

contextMenu.MenuItems.Add(new MenuItem() { Text = "Item A" });
contextMenu.MenuItems.Add(new MenuItem() { Text = "Item B" });
contextMenu.MenuItems.Add(new MenuItem() { Text = "Item C" });
contextMenu.MenuItems.Add(new MenuItem() { Text = "Item D" });

this.MouseDown += new MouseEventHandler(Form1_MouseDown);
contextMenu.Popup += new EventHandler(contextMenu_Popup);
}

void contextMenu_Popup(object sender, EventArgs e)
{
var type = contextMenu.GetType();
var members = type.GetMembers(
BindingFlags.NonPublic | BindingFlags.Instance);
var menuMember = type.GetField("m_hmnu",
BindingFlags.NonPublic | BindingFlags.Instance);
var hMenu = (HMENU)menuMember.GetValue(contextMenu);

var info = new MENUITEMINFO();
info.cbSize = (uint)Marshal.SizeOf(info);
info.fMask = MIIM_STATE;
var result = GetMenuItemInfo(hMenu, 0, true, out info);
if (!result)
{
var err = Marshal.GetLastWin32Error();
if (err == 0x0579) MessageBox.Show("Invalid menu handle");
return;
}
info.fMask = MIIM_STATE;
info.fState &= (~MFS_HILITE);
result = SetMenuItemInfo(hMenu, 0, true, ref info);
}

void Form1_MouseDown(object sender, MouseEventArgs e)
{
contextMenu.Show(this, new Point(e.X, e.Y));
}

private const uint MIIM_STATE = 1;
private const uint MFS_UNHILITE = 0;
private const uint MFS_HILITE = 0x80;

//typedef struct tagMENUITEMINFO {
// UINT cbSize;
// UINT fMask;
// UINT fType;
// UINT fState;
// UINT wID;
// HMENU hSubMenu;
// HBITMAP hbmpChecked;
// HBITMAP hbmpUnchecked;
// DWORD dwItemData;
// LPTSTR dwTypeData;
// UINT cch;
//} MENUITEMINFO, FAR* LPMENUITEMINFO;
private struct MENUITEMINFO
{
public UINT cbSize;
public UINT fMask;
public UINT fType;
public UINT fState;
public UINT wID;
public HMENU hSubMenu;
public HBITMAP hbmpChecked;
public HBITMAP hbmpUnchecked;
public DWORD dwItemData;
public LPTSTR dwTypeData;
public UINT cch;
}

//BOOL SetMenuItemInfo(
// HMENU hMenu,
// UINT uItem,
// BOOL fByPosition,
// LPCMENUITEMINFO lpmii
//);
[DllImport("coredll", SetLastError = true)]
private static extern bool SetMenuItemInfo(HMENU hMenu, UINT uItem,
[MarshalAs(UnmanagedType.Bool)]bool fByPosition,
ref MENUITEMINFO lpmii);

//BOOL GetMenuItemInfo(
// HMENU hMenu,
// UINT uItem,
// BOOL fByPosition,
// LPMENUITEMINFO lpmii
//);
[DllImport("coredll", SetLastError = true)]
private static extern bool GetMenuItemInfo(HMENU hMenu, UINT uItem,
[MarshalAs(UnmanagedType.Bool)]bool fByPosition,
out MENUITEMINFO lpmii);

//HMENU GetSubMenu(
// HMENU hMenu,
// int nPos
//);
[DllImport("coredll", SetLastError = true)]
private static extern HMENU GetSubMenu(HMENU hMenu, int nPos);
}
}

编辑

我知道我在某个地方有代码可以完成所有这些。我们曾经销售一个商业 PopupMenu 控件,它封装了用于创建菜单的所有 P/Invoke。控件的销量很小,因此我们在几年前将其从我们的产品线中撤出。我现在已将其作为开源发布 over on Codeplex .

关于C# ContextMenu 删除默认选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7111506/

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