我正在 WPF 中设计我自己的自定义窗口,并且我一直在尝试实现我之前在 WinForms 中使用的调整大小功能。出于某种原因,我的 WndProc 的返回值没有给我正确的结果。
我的所有 WndProc 消息和结果都有一个 NativeMethods 类:
public class NativeMethods
{
public const int WM_NCHITTEST = 0x84;
public const int HTCAPTION = 2;
public const int HTLEFT = 10;
public const int HTRIGHT = 11;
public const int HTTOP = 12;
public const int HTTOPLEFT = 13;
public const int HTTOPRIGHT = 14;
public const int HTBOTTOM = 15;
public const int HTBOTTOMLEFT = 16;
public const int HTBOTTOMRIGHT = 17;
}
这是我的窗口背后的代码:
public partial class MainWindow : Window
{
const int GripSize = 16;
const int BorderSize = 7;
public MainWindow()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
IntPtr windowHandle = new WindowInteropHelper(this).Handle;
HwndSource windowSource = HwndSource.FromHwnd(windowHandle);
windowSource.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg,
IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == NativeMethods.WM_NCHITTEST)
{
int x = lParam.ToInt32() << 16 >> 16, y = lParam.ToInt32() >> 16;
Point pos = PointFromScreen(new Point(x, y));
if (pos.X > GripSize &&
pos.X < ActualWidth - GripSize &&
pos.Y >= ActualHeight - BorderSize)
{
return (IntPtr)NativeMethods.HTBOTTOM; // This doesn't work?
}
// Top, Left, Right, Corners, Etc.
}
return IntPtr.Zero;
}
}
我希望光标更改为“调整大小向下箭头”,并且调整大小功能可以像在我的 WinForms 项目中那样工作。我设置了断点,当光标位于预期位置时,HTBOTTOM 返回正在触发。在 XAML 中,我将 ResizeMode 设置为 CanResize,并将 WindowStyle 设置为 None。我究竟做错了什么?