禁用,移除 WPF window窗体系统操作SystemMenu

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

        [DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        public static IntPtr GetSysMenu(IntPtr hWnd, bool bRevert)
        {
            return GetSystemMenu(hWnd, bRevert);
        }

        [DllImport("user32.dll")]
        private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
        public static bool EnableMenu(IntPtr hMenu, uint uIDEnableItem, uint uEnable)
        {
            return EnableMenuItem(hMenu, uIDEnableItem, uEnable);
        }
    }
View Code
  public class Win32ApiWrapper
    {
        [StructLayout(LayoutKind.Sequential)]
        private struct Win32Point
        {
            public Int32 X;
            public Int32 Y;
        };

        [DllImport("user32.dll")]
        private static extern bool GetCursorPos(ref Win32Point pt);

        /// <summary>
        /// 儅僂僗僇乕僜儖埵抲傪曉偡.<br/>
        /// </summary>
        /// <param name="relativeTo">The Visual to which the mouse coordinates will be relative.</param>
        public static Point GetMousePosition(Visual relativeTo)
        {
            Win32Point mouse = new Win32Point();
            GetCursorPos(ref mouse);

            return relativeTo.PointFromScreen(new Point((double)mouse.X, (double)mouse.Y));
        }

        #region OCR Wrapper
        [DllImport("SshSvcOCRHandle.dll")]
        public static extern int CheckOCREnable();
        #endregion // OCR Wrapper

        [DllImport("user32.dll")]
        private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
        public static bool EnableMenu(IntPtr hMenu, uint uIDEnableItem, uint uEnable)
        {
            return EnableMenuItem(hMenu, uIDEnableItem, uEnable);
        }

        [DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        public static IntPtr GetSysMenu(IntPtr hWnd, bool bRevert)
        {
            return GetSystemMenu(hWnd, bRevert);
        }

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
        public static bool CheckWindow(string strWindowName)
        {
            IntPtr hwnd = FindWindow(strWindowName, null);
            if (hwnd == IntPtr.Zero)
            {
                return false;
            }
            return true;
        }

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr OpenFileMapping(uint dwDesiredAccess, bool bInheritHandle, string lpName);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll", SetLastError = true)]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        [SuppressUnmanagedCodeSecurity]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool CloseHandle(IntPtr hObject);
    }
View Code

 使用:

  

private const uint MF_BYCOMMAND = 0x00000000;
private const uint MF_GRAYED = 0x00000001;
private const uint SC_RESTORE = 0xF120;
private const uint SC_MOVE = 0xF010;
private const uint SC_SIZE = 0xF000;
private const uint SC_MINIMIZE = 0xF020;
private const uint SC_MAXIMIZE = 0xF030;
private const uint SC_CLOSE = 0xF060;

IntPtr handle = new WindowInteropHelper(metroWindow).Handle;
            //xamlで、ResizeMode="NoResize"としても、メニューにマスクされないため明示的に行う。
            var sysMenu = SystemMenuManager.GetSysMenu(handle, false);
            SystemMenuManager.EnableMenu(sysMenu, SC_RESTORE, MF_BYCOMMAND | MF_GRAYED);
            SystemMenuManager.EnableMenu(sysMenu, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);
            SystemMenuManager.EnableMenu(sysMenu, SC_MINIMIZE, MF_BYCOMMAND | MF_GRAYED);
            SystemMenuManager.EnableMenu(sysMenu, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
View Code

 

转载于:https://www.cnblogs.com/ilison/p/10875051.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在WPF中,可以使用UserControl来创建自定义的可重用控件,而操作Window的关键是在UserControl中获取对Window的访问权。 要在UserControl中操作Window,首先要在UserControl的代码文件中定义一个依赖属性,该依赖属性类型应为Window,用于保存对Window的引用。例如,可以定义一个名为"ParentWindow"的依赖属性。 然后,在UserControl的XAML文件中,可以将UserControl的父级控件设置为Window,并将该Window与依赖属性进行绑定。可以使用RelativeSource指定绑定的目标为父级口。例如,可以在UserControl的根元素上定义"ParentWindow"属性的绑定,绑定源可以为RelativeSource的FindAncestor指令,并指定AncestorType为Window。 接下来,在UserControl的代码文件中,可以使用"ParentWindow"属性来获取对Window的引用,从而可以对Window进行操作。例如,可以在UserControl的某个事件处理程序中使用"ParentWindow"属性来关闭Window,可以使用"ParentWindow.Close()"方法来关闭Window。 总之,要在WPF的UserControl中操作Window,需要先定义一个依赖属性来保存对Window的引用,然后在XAML文件中将UserControl与Window进行绑定,最后在代码中通过该属性来实现对Window操作。 ### 回答2: 在WPF中,一个UserControl是一种重用的可视化元素,它可以包含其他控件和逻辑代码。可以通过各种方式操作UserControl来实现对Window操作。 首先,可以通过在UserControl中使用事件来与Window进行通信。在UserControl中定义一个公开的事件,然后在Window中订阅这个事件。当UserControl中触发该事件时,Window中的事件处理程序将被调用,从而实现对Window操作。 其次,可以通过依赖属性来实现UserControl与Window之间的数据绑定。在UserControl中定义一个依赖属性,然后在Window中将UserControl的某个属性与Window的属性进行绑定。当UserControl中的属性发生变化时,Window中的属性也会自动更新,从而实现对Window操作。 另外,可以在UserControl中使用VisualTreeHelper类来查找Window的实例,并对其进行操作。通过递归遍历UserControl的子元素,可以在其中找到Window的实例,并进行相应的操作。 还可以通过在UserControl中使用Command来操作Window。定义一个Command,并在UserControl中的某个控件上绑定这个Command。当该控件被触发时,Command会被执行,然后可以在Command的执行方法中对Window进行操作。 最后,还可以通过在UserControl中使用基于消息的通信来操作Window。使用Messenger或EventAggregator等消息传递机制,UserControl可以发送一个消息,然后Window中的订阅者可以接收到这个消息并对Window进行操作。 综上所述,通过事件、数据绑定、遍历可视化树、命令和消息传递等方式,我们可以在UserControl中操作Window,实现丰富的交互和功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值