原文:wpf 自定义窗口,最大化时覆盖任务栏解决方案

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

在网上找答案,排名靠前的都是提供用hook钩子,篇幅很长,如:http://www.cnblogs.com/zhouyinhui/archive/2008/11/04/1326188.html

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

Rect rcnormal;//定义一个全局rect记录还原状态下窗口的位置和大小。
/// <summary>
/// 最大化
/// </summary>
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;
}
/// <summary>
/// 还原
/// </summary>
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();
          }
       };

wpf 自定义窗口,最大化时覆盖任务栏解决方案的更多相关文章

  1. [WPF疑难]避免窗口最大化时遮盖任务栏

    原文 [WPF疑难]避免窗口最大化时遮盖任务栏 [WPF疑难]避免窗口最大化时遮盖任务栏 周银辉 WPF窗口最大化时有个很不好的现象是:如果窗口的WindowStyle被直接或间接地设置为None后( ...

  2. WPF自定义窗口最大化显示任务栏

    原文:WPF自定义窗口最大化显示任务栏 当我们要自定义WPF窗口样式时,通常是采用设计窗口的属性 WindowStyle="None" ,然后为窗口自定义放大,缩小,关闭按钮的样式 ...

  3. WPF最大化避免覆盖任务栏

    原文:WPF最大化避免覆盖任务栏 WPF当窗体WindowStyle=”None”时,最大化会覆盖掉任务栏.如何解决这个问题呢? 我在Google里面搜到一篇文章,要用到Win32 API,通过让WP ...

  4. WPF自学入门(九)WPF自定义窗口基类

    今天简单记录一个知识点:WPF自定义窗口基类,常用winform的人知道,winform的窗体继承是很好用的,写一个基础窗体,直接在后台代码改写继承窗体名.但如果是WPF要继承窗体,我个人感觉没有理解 ...

  5. WPF自定义窗口基类

    WPF自定义窗口基类时,窗口基类只定义.cs文件,xaml文件不定义.继承自定义窗口的类xaml文件的根节点就不再是<Window>,而是自定义窗口类名(若自定义窗口与继承者不在同一个命名 ...

  6. 对话框窗口最大化盖住任务栏问题!OnGetMinMaxInfo,WM_GETMINMAXINFO

    http://hi.baidu.com/csacer/item/37cd6ac2dec18d360831c6a7 在写程序时,如果包含了标题栏,但是没有包含最大化按钮或者最小话按钮,那么人工用Show ...

  7. 【转】【WPF】WPF 登录窗口关闭时打开主窗口

    在WPF中设计登录窗口关闭时打开主窗口,自动生成的App.xaml不能满足要求, 1.把App.xaml的属性窗口中的生成操作设定为 无 2.添加Program类 static class Progr ...

  8. wpf 自定义窗口,最大化时不覆盖任务栏

    相信很多人使用wpf时会选择自定义美观的窗口,因此会设置WindowStyle="None" 取消自带的标题栏.但这样使用 WindowState="Maximized& ...

  9. WPF 自定义窗口

    在WPF中,经常需要对窗口进行设置,下面讲讲常用的几个设置. 1.无边框窗口 WindowStyle="None" 窗口样式无 AllowsTransparency="T ...

随机推荐

  1. 【例题5-3 UVA - 10815】Andy's First Dictionary

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用stringstream来处理中间的标点. ->直接把他变成一个空格. 然后重新输入进去. set默认的字典序就是升序的了. ...

  2. 【BZOJ 1597】 [Usaco2008 Mar]土地购买

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把这n个土地按照x为第一关键字.y为第二关键字.都升序排. 然后考虑一个土地xi,yi 若有一个土地的x<=xi且y<= ...

  3. Uploadify404无效链接

    Uploadify404无效链接 在使用Jquery Uploadify插件的時候.会发如今请求中有个返回值为404的请求. 假如如今的location为www.aa.com/bugs/more. h ...

  4. php数组全排列,元素所有组合

    <?php $source = array('pll','我','爱','你','嘿'); sort($source); //保证初始数组是有序的 $last = count($source) ...

  5. STL map 按key值和按value值排序

    map是用来存放<key, value>键值对的数据结构,能够非常方便高速的依据key查到对应的value. 假如存储水果和其单位价格.我们用map来进行存储就是个不错的选择. 我们这样定 ...

  6. [Ramda] Filter an Array Based on Multiple Predicates with Ramda's allPass Function

    In this lesson, we'll filter a list of objects based on multiple conditions and we'll use Ramda's al ...

  7. js进阶 10-9 -of-type型子元素伪类选择器

    js进阶 10-9 -of-type型子元素伪类选择器 一.总结 一句话总结:三种和first.last等有关的选择器. 1.:first和:first-child和:first-of-type的区别 ...

  8. Notes on OpenSSL and Qt(ssl.pri,qsslocket_openssl_symbols.cpp)

    Libraries name of openssl? The "library" portion of OpenSSL consists of two libraries. On ...

  9. Docker for Windows 安装

    原文:Docker for Windows 安装 前言: 环境:windows10专业版 64位 正文: 官方下载地址:https://hub.docker.com/editions/communit ...

  10. 求 1~n 之间素数的个数

    求1到n之间素数的个数 1. 筛选法 筛选掉偶数,然后比如对于 3,而言,筛选掉其整数倍数:(也即合数一定是某数的整数倍,比如 27 = 3*9) int n = 100000000; bool fl ...