wpf 最大最小化窗口详细

Posted pengde

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wpf 最大最小化窗口详细相关的知识,希望对你有一定的参考价值。

转自分享

public static void Win(this Window window)
        {
            //如果是全屏,则最小化
            if (window.WindowState == WindowState.Maximized)
            {
                window.Topmost = false;
                window.WindowState = WindowState.Normal;
                window.WindowStyle = WindowStyle.SingleBorderWindow;

                window.ResizeMode = ResizeMode.CanResizeWithGrip;//设置为可调整窗体大小

                //获取窗口句柄 
                var handle = new WindowInteropHelper(window).Handle;
                //获取当前显示器屏幕
                Screen screen = Screen.FromHandle(handle);

                window.Left = (screen.Bounds.Width - window.Width) / 2;
                window.Top = (screen.Bounds.Height - window.Height) / 2;

                window.WindowState = WindowState.Minimized;
                return;
            }

            //如果是窗口,则全屏
            if (window.WindowState == WindowState.Normal)
            {
                //变成无边窗体
                window.WindowState = WindowState.Normal;//假如已经是Maximized,就不能进入全屏,所以这里先调整状态
                window.WindowStyle = WindowStyle.None;
                window.ResizeMode = ResizeMode.NoResize;
                window.Topmost = true;//最大化后总是在最上面

                //获取窗口句柄 
                var handle = new WindowInteropHelper(window).Handle;

                //获取当前显示器屏幕
                Screen screen = Screen.FromHandle(handle);

                //调整窗口最大化,全屏的关键代码就是下面3句
                window.MaxWidth = screen.Bounds.Width;
                window.MaxHeight = screen.Bounds.Height;
                window.WindowState = WindowState.Maximized;


                //解决切换应用程序的问题
                window.Activated += new EventHandler(window_Activated);
                window.Deactivated += new EventHandler(window_Deactivated);
            }

        }

        static void window_Deactivated(object sender, EventArgs e)
        {
            var window = sender as Window;
            window.Topmost = false;
        }

        static void window_Activated(object sender, EventArgs e)
        {
            var window = sender as Window;
            window.Topmost = true;
        }

 








































以上是关于wpf 最大最小化窗口详细的主要内容,如果未能解决你的问题,请参考以下文章

wpf ShowDialog或者其他操作后 UI不动 最小化再次最大化后又恢复正常

WPF编程怎么激活指定窗口?

有关C# WPF里窗口尺寸的问题

这样的窗口如何做(WPF项目)

如何从可调整大小的窗口中删除最小化和最大化按钮?

更改最小化,关闭和最大化窗口的图标