【发布时间】:2013-07-12 18:59:56
【问题描述】:
我正在 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。我做错了什么?
【问题讨论】:
-
为什么不使用
ResizeMode = "CanResizeWithGrip"?您正在尝试实现这种效果(只是在没有Grip的情况下围绕窗口调整大小)? -
因为我仍然希望能够像正常功能一样从任何角落或侧面调整窗口大小。我发现了这个问题,我现在发布答案。
标签: c# .net wpf window-resize mouse-cursor