[WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu)

点击窗口左上角图标时弹出来的菜单也就是这里所说的系统菜单(SystemMenu),有时需要禁用(移除)其中的某些或全部菜单项。

要全部禁用(移除)菜单项请调用SystemMenuManager.RemoveWindowSystemMenu(Window window)方法,想部分禁用(移除)菜单项则调用SystemMenuManager.RemoveWindowSystemMenuItem(Window window, int itemIndex)方法。
值得注意的是禁用了其中的菜单项那么与之相关联的功能也会被禁用,比如将“关闭”从其中移除,那么窗口的右上角的关闭按钮也会被禁用,在任务栏的窗口图标上右击也不会出现相应的项目

public static class SystemMenuManager
    {
        [DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
        private static extern IntPtr GetSystemMenu(IntPtr hwnd, int revert);

        [DllImport("user32.dll", EntryPoint = "GetMenuItemCount")]
        private static extern int GetMenuItemCount(IntPtr hmenu);

        [DllImport("user32.dll", EntryPoint = "RemoveMenu")]
        private static extern int RemoveMenu(IntPtr hmenu, int npos, int wflags);

        [DllImport("user32.dll", EntryPoint = "DrawMenuBar")]
        private static extern int DrawMenuBar(IntPtr hwnd);

        private const int MF_BYPOSITION = 0x0400;
        private const int MF_DISABLED = 0x0002;

        public static void RemoveWindowSystemMenu(Window window)
        {
            if(window == null)
            {
                return;
            }

            window.SourceInitialized += window_SourceInitialized;

        }

        static void window_SourceInitialized(object sender, EventArgs e)
        {
            var window = (Window) sender;

            var helper = new WindowInteropHelper(window);
            IntPtr windowHandle = helper.Handle; //Get the handle of this window
            IntPtr hmenu = GetSystemMenu(windowHandle, 0);
            int cnt = GetMenuItemCount(hmenu);

            for (int i = cnt - 1; i >= 0; i--)
            {
                RemoveMenu(hmenu, i, MF_DISABLED | MF_BYPOSITION);
            }
        }

        public static void RemoveWindowSystemMenuItem(Window window, int itemIndex)
        {
            if (window == null)
            {
                return;
            }

            window.SourceInitialized += delegate
                                            {
                                                var helper = new WindowInteropHelper(window);
                                                IntPtr windowHandle = helper.Handle; //Get the handle of this window
                                                IntPtr hmenu = GetSystemMenu(windowHandle, 0);

                                                //remove the menu item
                                                RemoveMenu(hmenu, itemIndex, MF_DISABLED | MF_BYPOSITION);

                                                DrawMenuBar(windowHandle); //Redraw the menu bar
                                            };

        }
    }

原文链接:https://www.cnblogs.com/zhouyinhui/archive/2008/11/04/1326514.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值