我正在构建一个 WPF 4.5应用程序,它具有控件,可以让用户“锁定”并“解锁”应用程序的高度。
为了锁定高度,我正在关注StackOverflow answer regarding setting the MinHeight and MaxHeight to the same value。
为了解锁高度,我设置了MinHeight=0
和MaxHeight=double.PositiveInfinity
这一切似乎都运转正常。
我遇到的问题是我无法解决的问题是,当高度为“已锁定”时,当鼠标悬停在应用程序窗口的右边缘时,光标将变为水平调整大小光标。
有没有办法可以禁用它,以便光标保持为WPF中的常规指针?
我在WPF 4.5上。
我看到这篇文章的答案显示了如何在Win32中执行此操作:WPF: Make window unresizeable, but keep the frame?。
这篇文章已经超过3年了,我只是想知道(希望)自那以后WPF可能已经发展了。
非常感谢你!
菲利普
答案 0 :(得分:1)
在启动窗口(MainWindow.xaml)上,尝试对Window的ResizeMode属性进行绑定,然后在不希望它可调整大小时将其修改为“NoResize”。要使其可调整大小,请将其更改为“CanResize”。
希望有所帮助!
答案 1 :(得分:1)
您需要设置MinWidth = MaxWidth = Width =您想要的宽度,如StackOverflow answer regarding setting the MinHeight and MaxHeight to the same value中所述。
此外,您需要为窗口挂钩winproc并处理 WM_NCHITTEST 消息。
#region Vertical Resize Only
// ReSharper disable InconsistentNaming
private const int WM_NCHITTEST = 0x0084;
private const int HTBORDER = 18;
private const int HTBOTTOM = 15;
private const int HTBOTTOMLEFT = 16;
private const int HTBOTTOMRIGHT = 17;
private const int HTLEFT = 10;
private const int HTRIGHT = 11;
private const int HTTOP = 12;
private const int HTTOPLEFT = 13;
private const int HTTOPRIGHT = 14;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr DefWindowProc(
IntPtr hWnd,
int msg,
IntPtr wParam,
IntPtr lParam);
// ReSharper restore InconsistentNaming
#endregion Vertical Resize Only
public CanConfigurationDialog()
{
InitializeComponent();
Loaded += MainWindowLoaded;
}
#region Vertical Resize Only
private void MainWindowLoaded(object sender, RoutedEventArgs e)
{
try
{
// Obtain the window handle for WPF application
var mainWindowPtr = new WindowInteropHelper(this).Handle;
var mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
mainWindowSrc?.AddHook(WndProc);
}
catch (Exception)
{
;
}
}
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Override the window hit test
// and if the cursor is over a resize border,
// return a standard border result instead.
if (msg == WM_NCHITTEST)
{
handled = true;
var htLocation = DefWindowProc(hwnd, msg, wParam, lParam).ToInt32();
switch (htLocation)
{
case HTTOP:
case HTTOPLEFT:
case HTTOPRIGHT:
htLocation = HTTOP;
break;
case HTBOTTOM:
case HTBOTTOMLEFT:
case HTBOTTOMRIGHT:
htLocation = HTBOTTOM;
break;
case HTLEFT:
case HTRIGHT:
htLocation = HTBORDER;
break;
}
return new IntPtr(htLocation);
}
return IntPtr.Zero;
}
#endregion Vertical Resize Only
这将阻止显示水平调整大小光标! Q.E.D。