欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wpf窗体程序全屏显示,wpf退出限制,wpf窗口禁止移动

程序员文章站 2022-07-13 23:17:28
...

this.Topmost = true慎用,会遮挡对话框,造成无法操作,事件根据情况进行编写,或自动生成

 ((MainWindowsView)(target)).Closing += new System.ComponentModel.CancelEventHandler(this.ModernWindow_Closing);
 ((MainWindowsView)(target)).Loaded += new System.Windows.RoutedEventHandler(this.ModernWindow_Loaded);

第一种设置全屏的方式

                // 设置全屏
                this.WindowState = System.Windows.WindowState.Normal;
                this.WindowStyle = System.Windows.WindowStyle.None;
                this.ResizeMode = System.Windows.ResizeMode.CanMinimize;
                this.ResizeMode = ResizeMode.NoResize;
                //抓住焦点
                //this.Topmost = true;
                this.Left = 0.0;
                this.Top = 0.0;
                this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
                this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;

 第二种设置全屏的方式

private void ModernWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (SysSettings.Default.IsFullScreen)
            {
                 设置全屏
                //不显示最大化和最小化按钮
                this.ResizeMode = System.Windows.ResizeMode.NoResize;
                //窗口在最前
                //this.Topmost = true; 

                this.Left = 0.0;
                this.Top = 0.0;
                this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
                this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;

                //窗口初始化后指定钩子函数
                IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
                System.Windows.Interop.HwndSource.FromHwnd(hwnd).AddHook(new System.Windows.Interop.HwndSourceHook(WndProc));

                IntPtr handle = new System.Windows.Interop.WindowInteropHelper(this).Handle;
                IntPtr hmenu = GetSystemMenu(handle, 0);
                int cnt = GetMenuItemCount(hmenu);
                for (int i = cnt - 1; i >= 0; i--)
                {
                    StringBuilder tmpstr = new StringBuilder(100);
                    GetMenuString(hmenu, (uint)i, tmpstr, 255, MF_DISABLED | MF_BYPOSITION);
                    if (tmpstr.ToString().Contains("移动"))
                    {
                        RemoveMenu(hmenu, i, MF_DISABLED | MF_BYPOSITION);
                    }
                }
            }
            else
            {
                this.Width = 1024;
                this.Height = 768;
            }
        }

        //钩子函数的实现
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            //拦截标题栏双击和窗口移动事件
            if (msg == 0x00A3 || msg == 0x0003 || wParam == (IntPtr)0xF012)
            {
                handled = true;
                wParam = IntPtr.Zero;
            }
            return IntPtr.Zero;
        }

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

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

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

        [DllImport("user32.dll", EntryPoint = "GetMenuStringW", CharSet = CharSet.Unicode)]
        private static extern int GetMenuString(IntPtr hMenu, uint uIDItem, StringBuilder lpString, int cchMax, uint flags);

        //键值
        private const int MF_BYPOSITION = 0x0400;
        private const int MF_DISABLED = 0x0002;

关闭窗体密码效验功能

private void ModernWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            System.Windows.Style style = new System.Windows.Style();
            style.Setters.Add(new Setter(Xceed.Wpf.Toolkit.MessageBox.YesButtonContentProperty, "是!"));
            style.Setters.Add(new Setter(Xceed.Wpf.Toolkit.MessageBox.NoButtonContentProperty, "否!"));
            MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show("确认", "是否退出程序", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.Yes, style);
            if (result == MessageBoxResult.Yes)
            {
                //程序退出
                e.Cancel = false;
            }
            else
            {
                //终止退出进程
                e.Cancel = true;
            }
        }

 

相关标签: .Net