C#ContextMenu删除默认选择

时间:2011-08-18 17:23:13

标签: c# mobile compact-framework contextmenu

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

1 个答案:

答案 0 :(得分:3)

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

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 / Invokes以创建菜单。对照的销售额很小,所以我们几年前从我们的产品线中撤出了它。我现在已将其作为开源over on Codeplex发布。