你确实需要处理一个Windows消息来完成它,但它并不复杂。
您必须处理WM_WINDOWPOSCHANGING消息,在WPF中这样做需要一些样板代码,您可以在下面看到实际逻辑仅仅是两行代码。
internal enum WM
{
WINDOWPOSCHANGING = 0x0046,
}
[StructLayout(LayoutKind.Sequential)]
internal struct WINDOWPOS
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int x;
public int y;
public int cx;
public int cy;
public int flags;
}
private void Window_SourceInitialized(object sender, EventArgs ea)
{
HwndSource hwndSource = (HwndSource)HwndSource.FromVisual((Window)sender);
hwndSource.AddHook(DragHook);
}
private static IntPtr DragHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handeled)
{
switch ((WM)msg)
{
case WM.WINDOWPOSCHANGING:
{
WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS));
if ((pos.flags & (int)SWP.NOMOVE) != 0)
{
return IntPtr.Zero;
}
Window wnd = (Window)HwndSource.FromHwnd(hwnd).RootVisual;
if (wnd == null)
{
return IntPtr.Zero;
}
bool changedPos = false;
// ***********************
// Here you check the values inside the pos structure
// if you want to override them just change the pos
// structure and set changedPos to true
// ***********************
// this is a simplified version that doesn't work in high-dpi settings
// pos.cx and pos.cy are in "device pixels" and MinWidth and MinHeight
// are in "WPF pixels" (WPF pixels are always 1/96 of an inch - if your
// system is configured correctly).
if(pos.cx < MinWidth) { pos.cx = MinWidth; changedPos = true; }
if(pos.cy < MinHeight) { pos.cy = MinHeight; changedPos = true; }
// ***********************
// end of "logic"
// ***********************
if (!changedPos)
{
return IntPtr.Zero;
}
Marshal.StructureToPtr(pos, lParam, true);
handeled = true;
}
break;
}
return IntPtr.Zero;
}
来源
2009-11-12 08:42:57
Nir
谢谢!这正是我需要的。我已经连接了源代码,因为我正在监视0x0024(最大化事件),所以我不得不向我的交换机添加一个案例。再次感谢! – 2009-11-12 19:24:03
谢谢你。这是很好和适当的排序。 – Dennis 2011-06-10 17:49:26
当MinHeight或MinWidth尝试超过时,有什么办法可以阻止“黑色”屏幕闪烁?闪烁是不好的。 – 2013-02-26 21:17:37