WPF最大化避免覆盖任务栏
WPF当窗体WindowStyle=”None”时,最大化会覆盖掉任务栏。如何解决这个问题呢?
我在Google里面搜到一篇文章,要用到Win32 API,通过让WPF窗体WM_GETMINMAXINFO消息挂接一个钩子来处理。
public static void RepairWindowBehavior(Window wpfWindow)
{ if (wpfWindow == null) return; wpfWindow.SourceInitialized += delegate { IntPtr handle = (new WindowInteropHelper(wpfWindow)).Handle; HwndSource source = HwndSource.FromHwnd(handle); if (source != null) { source.AddHook(WindowProc); } }; } private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { switch (msg) { case 0x0024: WmGetMinMaxInfo(hwnd, lParam); handled = true; break; } return (IntPtr)0; } private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam) { MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO)); int MONITOR_DEFAULTTONEAREST = 0x00000002; IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); if (monitor != 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); } [DllImport("user32")] internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi); [DllImport("User32")] internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags); #region Nested type: MINMAXINFO [StructLayout(LayoutKind.Sequential)] internal struct MINMAXINFO { public POINT ptReserved; public POINT ptMaxSize; public POINT ptMaxPosition; public POINT ptMinTrackSize; public POINT ptMaxTrackSize; } #endregion #region Nested type: MONITORINFO [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] internal class MONITORINFO { public int cbSize = Marshal.SizeOf(typeof(MONITORINFO)); public RECT rcMonitor; public RECT rcWork; public int dwFlags; } #endregion #region Nested type: POINT [StructLayout(LayoutKind.Sequential)] internal struct POINT { public int x; public int y; public POINT(int x, int y) { this.x = x; this.y = y; } } #endregion #region Nested type: RECT [StructLayout(LayoutKind.Sequential, Pack = 0)] internal struct RECT { public int left; public int top; public int right; public int bottom; public static readonly RECT Empty; public int Width { get { return Math.Abs(right - left); } } public int Height { get { return bottom - top; } } public RECT(int left, int top, int right, int bottom) { this.left = left; this.top = top; this.right = right; this.bottom = bottom; } public RECT(RECT rcSrc) { left = rcSrc.left; top = rcSrc.top; right = rcSrc.right; bottom = rcSrc.bottom; } public bool IsEmpty { get { return left >= right || top >= bottom; } } public override string ToString() { if (this == Empty) { return "RECT {Empty}"; } return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }"; } public override bool Equals(object obj) { if (!(obj is Rect)) { return false; } return (this == (RECT)obj); } public override int GetHashCode() { return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode(); } 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); } public static bool operator !=(RECT rect1, RECT rect2) { return !(rect1 == rect2); } } #endregion }通过调用WindowHelper.RepairWindowBehavior(Window)方法注册Win32消息事件,以后调用this.WindowState = WindowState.Maximized;就可以解决最大化避免覆盖任务栏的问题了。
WPF最大化避免覆盖任务栏的更多相关文章
- wpf 自定义窗口,最大化时覆盖任务栏解决方案
原文:wpf 自定义窗口,最大化时覆盖任务栏解决方案 相信很多人使用wpf时会选择自定义美观的窗口,因此会设置WindowStyle="None" 取消自带的标题栏.但这样使用 W ...
- 2句代码轻松实现WPF最大化不遮挡任务栏并且具有边框调节效果
原文:2句代码轻松实现WPF最大化不遮挡任务栏并且具有边框调节效果 相信刚入门的菜鸟跟我一样找遍了百度谷歌解决最大化遮挡任务栏的方法大多方法都是HOOK一大堆API声明 最近在敲代码的时候无意中发现有 ...
- [WPF疑难]避免窗口最大化时遮盖任务栏
原文 [WPF疑难]避免窗口最大化时遮盖任务栏 [WPF疑难]避免窗口最大化时遮盖任务栏 周银辉 WPF窗口最大化时有个很不好的现象是:如果窗口的WindowStyle被直接或间接地设置为None后( ...
- wpf 自定义窗口,最大化时不覆盖任务栏
相信很多人使用wpf时会选择自定义美观的窗口,因此会设置WindowStyle="None" 取消自带的标题栏.但这样使用 WindowState="Maximized& ...
- C# Winform WPF DeskBand 窗体嵌入任务栏,在任务栏显示文字
最近写了个小程序,用于将固态硬盘的写入量等信息显示在任务栏,最开始使用Windows API也可以实现,但是当任务栏托盘增加的时候,会被遮盖,最终采用了DeskBand来实现,填了很多坑. 参考的Gi ...
- 对话框窗口最大化盖住任务栏问题!OnGetMinMaxInfo,WM_GETMINMAXINFO
http://hi.baidu.com/csacer/item/37cd6ac2dec18d360831c6a7 在写程序时,如果包含了标题栏,但是没有包含最大化按钮或者最小话按钮,那么人工用Show ...
- 让view 覆盖导航栏
当我们想做一个弹出式菜单时,想将导航栏也一起盖住不显示的话,可以用如下语句实现: UIView* myView = /* 你自定义的view */; UIWindow* currentWindow = ...
- wpf textblock 会覆盖 button里面字体样式的解决方法 还有button的style覆盖。。datepicker里面的按钮的style
.(button使用contont写的时候) 当.button使用 <button.content><textBlock/></button.content>依然会 ...
- WPF 最大化最小化窗口
public static void FullOrMin(this Window window) { //如果是全屏,则最小化 if (win ...
随机推荐
- 【54.38%】【BZOJ 4300】绝世好题
Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1120 Solved: 609 [Submit][Status][Discuss] Descript ...
- PatentTips - Integrated circuit well bias circuitry
1. Field of the Invention This invention relates in general to an integrated circuit and more specif ...
- SimpleDateFormat.format的简单使用小结
format的用法 是将当前时间格式转换为指定格式 场景一:给定毫秒数或者当前系统时间,返回指定时间格式 输入 Date date=new Date();//获得系统当前的时间 // ...
- VS关于 _CRT_SECURE_NO_WARNINGS 警告说明
在VS中调用 strcpy.strcat 等函数时会提示 _CRT_SECURE_NO_WARNINGS 警告.原因是这些函数不安全.可能会造成内存泄露等. 所以建议採用带_s的函数,如strcpy_ ...
- php课程 6-24 字符串函数有哪些(复习)
php课程 6-24 字符串函数有哪些(复习) 一.总结 一句话总结: 二.php课程 6-24 字符串函数有哪些(复习) 上次复习:--------------------------------- ...
- [GeekBand] C++ 基础知识一 ——通过引用传递数组
本文参考 : C++ Primer (第四版) 7.2.4及 16.1.5 相关章节 GeekBand 侯捷老师,学习笔记 开发环境采用:VS2013版本 关键问题一.传递引用与传指针.传值的区别? ...
- 一个封装比较完整的FTP类——clsFTP
前几天,看见园子里面的博友写了一个支持断点续传的FTP类,一时技痒,干脆写了个更完整的clsFtp类.只是我写这个clsFtp不是支持断点续传的目的,而是为了封装FTP几个基本常用的操作接口. 功能 ...
- 【codeforces 785A】Anton and Polyhedrons
[题目链接]:http://codeforces.com/contest/785 [题意] 给你各种形状的物体; 然后让你计算总的面数; [题解] 用map来记录各种物体本该有的面数; 读入各种物体; ...
- 【25.47%】【codeforces 733D】Kostya the Sculptor
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 在navicat上设置定时计划执行存储过程
原文 应用情景: 有一个存储过程,需要每天定时执行一次.所以在navicat上使用事件处理,当然还有其他的方法,这只是一种.作为参考 1.事件定义填写 2.事件计划设置 3.保存 点击上方保存即可 常 ...