博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf 自定义窗口,最大化时覆盖任务栏解决方案
阅读量:4520 次
发布时间:2019-06-08

本文共 4246 字,大约阅读时间需要 14 分钟。

相信很多人使用wpf时会选择自定义美观的窗口,因此会设置WindowStyle="None" 取消自带的标题栏。但这样使用 WindowState="Maximized" 或者后台 this.WindowState = System.Windows.WindowState.Maximized; 最大化窗口会覆盖掉系统任务栏,即全屏了。这其实并不是个很好的体验。

在网上找答案,排名靠前的都是提供用hook钩子,篇幅很长,如:

个人感觉这么一个小功能添加那么多的代码是不人性的,于是继续寻找,终于看到黎明的曙光:

Rect rcnormal;//定义一个全局rect记录还原状态下窗口的位置和大小。        ///         /// 最大化        ///         private void btnMaximize_Click(object sender, RoutedEventArgs e)        {            this.btnMaximize.Visibility = Visibility.Collapsed;            this.btnNormal.Visibility = Visibility.Visible;            rcnormal = new Rect(this.Left, this.Top, this.Width, this.Height);//保存下当前位置与大小            this.Left = 0;//设置位置            this.Top = 0;                        Rect rc = SystemParameters.WorkArea;//获取工作区大小            this.Width = rc.Width;            this.Height = rc.Height;        }        ///         /// 还原        ///         private void btnNormal_Click(object sender, RoutedEventArgs e)        {            this.Left = rcnormal.Left;            this.Top = rcnormal.Top;            this.Width = rcnormal.Width;            this.Height = rcnormal.Height;            this.btnMaximize.Visibility = Visibility.Visible;            this.btnNormal.Visibility = Visibility.Collapsed;        }

好了,最大化和最小化事件自定义好了。那如果窗口拖动到顶端鼠标出界的话窗口将会最大化是不是?在wpf中 WindowStyle="None" 下也还是全屏效果,而且会覆盖掉我们自定义的效果,这个时候你的this.width和this.height都无用了。那该怎么办呢?看下边:

在前台添加:

SizeChanged="Window_SizeChanged"

后台:

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)        {            if (this.ActualHeight > SystemParameters.WorkArea.Height || this.ActualWidth > SystemParameters.WorkArea.Width)            {                this.WindowState = System.Windows.WindowState.Normal;                btnMaximize_Click(null, null);            }        }

或者在最大化后禁用拖动功能,待恢复原始窗口后在恢复拖动功能也可以。

ok,搞定! 这么简单的代码,相信大家看的懂吧~~

另附双击标题栏事件:

private void Grid_MouseDown(object sender, MouseButtonEventArgs e)        {            if (e.ClickCount == 2)            {                if (this.ActualWidth == SystemParameters.WorkArea.Width)                {                    btnNormal_Click(null, null);                }                else                {                    btnMaximize_Click(null, null);                }            }        }

最后总结代码:

Rect rcnormal;        protected virtual void InitializeEvent()        {            ControlTemplate baseWindowTemplate = (ControlTemplate)Application.Current.Resources["BaseWindowControlTemplate"];            TextBlock title = (TextBlock)baseWindowTemplate.FindName("lab_title", this);            title.Text = window_title;            title.Margin = mrg;            Button maxBtn = (Button)baseWindowTemplate.FindName("btnMax", this);            maxBtn.Visibility = btn_max;            bool tag = true;            maxBtn.Click += delegate            {                if (tag)                {                    tag = false;                    rcnormal = new Rect(this.Left, this.Top, this.Width, this.Height);                    this.Left = 0;                    this.Top = 0;                    Rect rc = SystemParameters.WorkArea;                    this.Width = rc.Width;                    this.Height = rc.Height;                    maxBtn.Style = (Style)Application.Current.Resources["btn_max2"];                }                else                {                    tag = true;                    this.Left = rcnormal.Left;                    this.Top = rcnormal.Top;                    this.Width = rcnormal.Width;                    this.Height = rcnormal.Height;                    maxBtn.Style = (Style)Application.Current.Resources["btn_max"];                }            };            Button closeBtn = (Button)baseWindowTemplate.FindName("btnClose", this);            closeBtn.Click += delegate            {                this.Close();            };            //拖动            Border borderTitle = (Border)baseWindowTemplate.FindName("borderTitle", this);            borderTitle.MouseMove += delegate(object sender, MouseEventArgs e)            {
//tag==true 只有窗口恢复到原始窗口后才可拖动           if (e.LeftButton == MouseButtonState.Pressed && tag==true) {             this.DragMove();           }        };

 

转载于:https://www.cnblogs.com/softwyy/p/9719036.html

你可能感兴趣的文章
门面模式
查看>>
Navicat Premium 10/12——破解激活
查看>>
【leetcode❤python】 290. Word Pattern
查看>>
Mysql备份还原数据库之mysqldump实例及参数详细说明 -转自http://www.cnblogs.com/xuejie/archive/2013/01/11/2856911.html...
查看>>
2013年10月13日学习:SQL通过命令语句来创建表
查看>>
剑指offer : 二维数组中的查找
查看>>
第三章 python基础
查看>>
java基础题
查看>>
[转]人人店短信插件开发
查看>>
[转]c# System.IO.Ports SerialPort Class
查看>>
14. 最长公共前缀
查看>>
Redis文档
查看>>
项目重构
查看>>
(笔试题)和一半的组合数
查看>>
leetcode--Algorithm--Array_Part 1 Easy- 566 Reshape the Matrix
查看>>
AC自动机算法详解 (转载)
查看>>
python3-day5(模块)
查看>>
Linux配置JDK
查看>>
qt 读取xml文件
查看>>
python3之正则表达式
查看>>