C# ContextMenu 删除默认选择

发布于 2024-11-30 10:21:08 字数 208 浏览 0 评论 0原文

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

Okay this may seem too simple of a question but I've wasted enough time looking for how to do this. I am using CE 6.5 on a mobile device and I have a ContextMenu with six MenuItems. When the Menu pops up the first Item in the list is automatically highlighted. I want to remove this highlight as it is confusing some of my users in thinking that it is the current state. I looked into ContextMenu and all its variables and MenuItem and haven't found out how to remove automatic highlight of first item. Same goes for a MainMenu's too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓 2024-12-07 10:21:08

不幸的是,我认为答案是,你不能。今天下午我付出了很大的努力来获取该菜单,但我只是无法获得操作系统允许我使用的有效 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。该控件的销量很小,因此几年前我们将其从我们的产品线中撤下。我现在已将其作为开源在 Codeplex 上发布

I think the answer is, unfortunately, you can't. I put in a strong effort this afternoon to get hold of that menu and I just couldn't get a valid HMENU that the OS would allow me to use. If you want to continue trying to chase the path I was on the code is below, but I really think it's a dead-end. At this point I'd consider P/Invoking everything for the menu (creation, population, etc) if you really need that feature.

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);
    }
}

EDIT

I knew I had code somewhere for doing all of this. We used to sell a commercial PopupMenu control that wrapped up all of the P/Invokes for menu creation. Sales for the control were small, so we pulled it from our product line a few years ago. I've now released it as open source over on Codeplex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文