原文:WPF自定义窗口最大化显示任务栏

当我们要自定义WPF窗口样式时,通常是采用设计窗口的属性 WindowStyle="None" ,然后为窗口自定义放大,缩小,关闭按钮的样式。

然而这样的话,当通过代码设置窗口(代码如下)放大时,窗口会把任务栏给遮档住。

private void Max_Click(object sender, RoutedEventArgs e)
{
if (this.WindowState != WindowState.Maximized)
{
this.WindowState = WindowState.Maximized; }
else
{
this.WindowState = WindowState.Normal; }
}

这样的问题想必也同样困绕着你。下面可以通过采用win32编程的方式把任务栏显示出来。Idea源于网络上的资料,如果你在其他地方找到类似的,那祝贺你!

首先你的窗口代码文件引用两个命名空间:

using WinInterop = System.Windows.Interop;
using System.Runtime.InteropServices;

然后在窗口构造函数加入

this.SourceInitialized += new EventHandler(win_SourceInitialized);
win_SourceInitialized 函数及相关代码如下:
#region 最大化显示任务栏

        void win_SourceInitialized(object sender, EventArgs e)
{
System.IntPtr handle = (new WinInterop.WindowInteropHelper(this)).Handle;
WinInterop.HwndSource.FromHwnd(handle).AddHook(new WinInterop.HwndSourceHook(WindowProc));
} private static System.IntPtr WindowProc(
System.IntPtr hwnd,
int msg,
System.IntPtr wParam,
System.IntPtr lParam,
ref bool handled)
{
switch (msg)
{
case 0x0024:
WmGetMinMaxInfo(hwnd, lParam);
handled = true;
break;
} return (System.IntPtr);
} private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam)
{ MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO)); // Adjust the maximized size and position to fit the work area of the correct monitor
int MONITOR_DEFAULTTONEAREST = 0x00000002;
System.IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); if (monitor != System.IntPtr.Zero)
{ MONITORINFO monitorInfo = new MONITORINFO();
GetMonitorInfo(monitor, monitorInfo);
RECT rcWorkArea = monitorInfo.rcWork;
RECT rcMonitorArea = monitorInfo.rcMonitor;
mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
} Marshal.StructureToPtr(mmi, lParam, true);
} /// <summary>
/// POINT aka POINTAPI
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
/// <summary>
/// x coordinate of point.
/// </summary>
public int x;
/// <summary>
/// y coordinate of point.
/// </summary>
public int y; /// <summary>
/// Construct a point of coordinates (x,y).
/// </summary>
public POINT(int x, int y)
{
this.x = x;
this.y = y;
}
} [StructLayout(LayoutKind.Sequential)]
public struct MINMAXINFO
{
public POINT ptReserved;
public POINT ptMaxSize;
public POINT ptMaxPosition;
public POINT ptMinTrackSize;
public POINT ptMaxTrackSize;
}; /// <summary>
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MONITORINFO
{
/// <summary>
/// </summary>
public int cbSize = Marshal.SizeOf(typeof(MONITORINFO)); /// <summary>
/// </summary>
public RECT rcMonitor = new RECT(); /// <summary>
/// </summary>
public RECT rcWork = new RECT(); /// <summary>
/// </summary>
public int dwFlags = ;
} /// <summary> Win32 </summary>
[StructLayout(LayoutKind.Sequential, Pack = )]
public struct RECT
{
/// <summary> Win32 </summary>
public int left;
/// <summary> Win32 </summary>
public int top;
/// <summary> Win32 </summary>
public int right;
/// <summary> Win32 </summary>
public int bottom; /// <summary> Win32 </summary>
public static readonly RECT Empty = new RECT(); /// <summary> Win32 </summary>
public int Width
{
get { return Math.Abs(right - left); } // Abs needed for BIDI OS
}
/// <summary> Win32 </summary>
public int Height
{
get { return bottom - top; }
} /// <summary> Win32 </summary>
public RECT(int left, int top, int right, int bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
} /// <summary> Win32 </summary>
public RECT(RECT rcSrc)
{
this.left = rcSrc.left;
this.top = rcSrc.top;
this.right = rcSrc.right;
this.bottom = rcSrc.bottom;
} /// <summary> Win32 </summary>
public bool IsEmpty
{
get
{
// BUGBUG : On Bidi OS (hebrew arabic) left > right
return left >= right || top >= bottom;
}
}
/// <summary> Return a user friendly representation of this struct </summary>
public override string ToString()
{
if (this == RECT.Empty) { return "RECT {Empty}"; }
return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }";
} /// <summary> Determine if 2 RECT are equal (deep compare) </summary>
public override bool Equals(object obj)
{
if (!(obj is Rect)) { return false; }
return (this == (RECT)obj);
} /// <summary>Return the HashCode for this struct (not garanteed to be unique)</summary>
public override int GetHashCode()
{
return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
} /// <summary> Determine if 2 RECT are equal (deep compare)</summary>
public static bool operator ==(RECT rect1, RECT rect2)
{
return (rect1.left == rect2.left && rect1.top == rect2.top && rect1.right == rect2.right && rect1.bottom == rect2.bottom);
} /// <summary> Determine if 2 RECT are different(deep compare)</summary>
public static bool operator !=(RECT rect1, RECT rect2)
{
return !(rect1 == rect2);
} } [DllImport("user32")]
internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi); /// <summary>
///
/// </summary>
[DllImport("User32")]
internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags); #endregion

最后F5,你会看到你所期望效果。

Thank you!

WPF自定义窗口最大化显示任务栏的更多相关文章

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

    原文:wpf 自定义窗口,最大化时覆盖任务栏解决方案 相信很多人使用wpf时会选择自定义美观的窗口,因此会设置WindowStyle="None" 取消自带的标题栏.但这样使用 W ...

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

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

  3. WPF自定义窗口基类

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

  4. winform,wpf全屏 还显示任务栏的解决方法

    原文:winform,wpf全屏 还显示任务栏的解决方法 以wpf为例: 全屏代码: this.Topmost = true; this.WindowStyle = System.Windows.Wi ...

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

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

  6. WPF 自定义窗口

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

  7. MFC最大化显示任务栏

    今天2016-07-23   13:26:24又来处理最大化时,窗口任务栏隐藏的bug. 前面已经用了 MINMAXINFO的结构体: typedef struct { POINT ptReserve ...

  8. WPF 自定义窗口标题栏

    1.建一个WPF资源词典,在其中定义窗口样式,并在App.xaml中指定其为程序资源 2.写一个继续自windows的类,并指定这个类的Style为第一步资源里的样式 3.新建窗口时,分别把xaml文 ...

  9. WPF自定义窗口(Windows Server 2012 Style)

    先上图 新建CustomControl,名:HeaderedWindow Themes\Generic.aml文件夹下加入 笔刷,转换器 <SolidColorBrush x:Key=" ...

随机推荐

  1. word-vba-microsoft(中英文)

    中文 https://msdn.microsoft.com/zh-cn/vba/word-vba/articles/view-displaypageboundaries-property-word 英 ...

  2. 百度消息推送SDK探究(并附上最简推送Demo)

    上一篇<百度消息推送REST API探究>中了解了如何使用REST API推送消息,这一篇我们来看一下百度消息推送为我们提供的SDK. 帮助文档:http://developer.baid ...

  3. Jquery前端分页插件pagination使用

    插件描述:JqueryPagination是一个轻量级的jquery分页插件.只需几个简单的配置就可以生成分页控件.并且支持ajax获取数据,自定义请求参数,提供多种方法,事件和回调函数,功能全面的分 ...

  4. C#生成二维码,把二维码图片放入Excel中

    /// <summary> /// 把图片保存到excel中 /// </summary> /// <param name="excelFilePath&quo ...

  5. 手动安装huson插件的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 首先到jenkins-ci.org下载插件,地址为:http://updates.jenkins-ci.org/dow ...

  6. noip刷题记录 20170823

    独木桥 怎么说呢 #include<iostream> #include<cstdio> #include<algorithm> using namespace s ...

  7. AsyncTask下载图片

    最近在看一个非常早期曾经写过代码,装上去召回.本文首先召回AsyncTask的基本使用.   AsyncTask说简单点就是 开启一个线程.而且把结果提交给ui线程. Thread+Handler,只 ...

  8. linux 时间同步ntp

    配置前准备:关闭防火墙,配置好hosts,ssh免密登录 1.选定同步的标准,我是以hadoop002(设置为当前时间)作为同步标准,hadoop003(时间是2018年3月21,使用date -s进 ...

  9. 检测鼠标指针的改变(使用GetCursorInfo API函数)

    第一步:定义全局变量用于状态改变时的对比 var Form1: TForm1; OldCI:HICON; 第二步:添加Timer组件,Interval设置随意一般50就可以了. 增加Memo组件用于记 ...

  10. 组态档(configuration file)与建构档

    组态档,或者叫 configuration file,配置文件.组态档是用一种建构软件专用的特殊编程语言写的 CMake 脚本. 使用组态档能改变程序的设置,而不用重新编译程序. CMake 的组态档 ...