Ben*_*lan 9 wpf height xaml window width
我有一个WPF应用程序,主窗口的装饰是自定义的,通过WindowStyle ="None".我绘制自己的标题栏和最小/最大/关闭按钮.不幸的是,Windows在调整窗口大小时不会强制执行MinWidth和MinHeight属性,从而允许窗口一直调整到3x3(appx - 刚好足以显示窗口增长的句柄).
我已经不得不拦截窗口事件(sp.0x0024)来修复由WindowStyle = none引起的最大化错误(它将在Windows任务栏上最大化).我并不害怕拦截更多事件来实现我的需要.
有没有人知道如何让我的窗口不在我的MinWidth和MinHeight属性下调整大小,如果有可能的话?谢谢!!
小智 13
我能够通过设置来解决这个问题handled
(最后一个参数WindowProc()
),以false
的情况下为0x0024(其中OP提到他已经钩住修复最大化),然后设置MinHeight
,并MinWidth
在窗口XAML.这使得此窗口消息的处理可以进入默认的WPF机制.
这样,Window
管理最小大小的Min*属性和自定义GetMinMaxInfo代码管理最大大小.
Nir*_*Nir 11
你需要处理一个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;
}
Run Code Online (Sandbox Code Playgroud)