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 ...
随机推荐
- VIM HML
D:\skill\Apps\Vim\vim80\defaults.vim "set scrolloff=5 设置为默认值0即可
- Opencv中integral计算积分图
Paul Viola和Michael Jones在2001年首次将积分图应用在图像特征提取上,在他们的论文"Rapid Object Detection using a Boosted Ca ...
- 【u031】租用游艇
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 长江游艇俱乐部在长江上设置了n 个游艇出租站1,2,-,n.游客可在这些游艇出租站租用游艇,并在下游的 ...
- 如何使用Name对象,包括WorkspaceNames和DatasetNames
转自chanyinhelv原文 如何使用Name对象,包括WorkspaceNames和DatasetNames 第一原文链接 该博主还有很多有关arcgis二次开发的不错的文章. 如何使用Name对 ...
- apt-get install安装软件时出现依赖错误解决方式
在使用apt-get install安装软件时,常常会遇到如上图所看到的错误.该错误的意思为缺少依赖软件.解决方式为: aptitude install golang-go
- Android下载文件提示文件不存在。。。 java.io.FileNotFoundException
遇到这个错误java.io.FileNotFoundException,事实上文件是存在的,把地址复制到手机浏览器都能够直接下载的,但为嘛不能下载呢. Error in downloadBitmap ...
- Linux非图形界面安装程序
安装Linux程序的时候一般不会调取图形界面,这样输错内容,返回上一步时需要使用命令 previous ,相应的exit 与 next..在中文操作的时候,不会提示,所以要记住 在安装过程中,我们可能 ...
- svn: is already a working copy for a different url 解决办法
svnX svn: E155000: '/Users/mac/Desktop/SHiosProject/SVNmangerfiles/wuye' is already a working c ...
- MOCHIWEB与COWBOY使用JSON
http://4096.info/2014/05/28/mochiweb%E4%B8%8Ecowboy%E4%BD%BF%E7%94%A8json/ 服务器原来的socket实现机制更改为ranch了 ...
- 汉高澳大利亚matrix矩阵计算器
我在梦中的超级计算机超级计算机锯,使用大量阵列的cpu记忆,完成并行计算.一个手机制造商由于使用普通机械提供的服务,往往造成停机.是铁道部列车网络售票的事实. 无法使用云服务.上万台计算机并行处理,因 ...