zoukankan      html  css  js  c++  java
  • 禁用,移除 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
  • 相关阅读:
    sql增删改查-转载
    委托和事件 链接
    三层架构-转载
    ToList()方法
    Invoke--转载
    C# 6.0新特性---语法糖
    索引器
    HBase学习总结(1)
    教程-关于Owner和Parent的区别
    问题-在TreeView使用时,发现选中的树节点会闪烁或消失
  • 原文地址:https://www.cnblogs.com/ilison/p/10875051.html
Copyright © 2011-2022 走看看