我正在WPF中设计自己的定制窗口,一直在尝试实现之前WinForms中使用过的调整大小功能。但是出于某种原因,我的WndProc返回值没有给我正确的结果。
我有一个NativeMethods类来处理所有的WndProc消息和结果:
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。我做错了什么?
ResizeMode = "CanResizeWithGrip"
?您想要实现这种效果(只是在窗口周围调整大小而没有Grip
)? - Anatoliy Nikolaev