• 禁用,移除 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
  • 相关阅读:
    C#实现京东登录密码加密POST
    查询SQL Server数据库所有表字段备注
    DataGridView数值列和日期列
    (转)Android 系统 root 破解原理分析
    Dynamics AX 中重点数据源方法
    .NET中Debug模式与Release模式
    DotNetBar的初步使用
    省市区联动小功能
    多余的Using Namespaces或引用会影响程序的执行效率么?
    MSIL指令集
  • 原文地址:https://www.cnblogs.com/ilison/p/10875051.html
Copyright © 2020-2023  润新知