WPF无边框窗口调整大小 [英] WPF Borderless window resize
问题描述
我在WPF中设计自己的自定义窗口,我一直在试图实现我以前在WinForms中使用的调整大小功能。因为某些原因,我的WndProc的返回值没有给我正确的结果。
I am designing my own custom window in WPF and I have been trying to implement the resizing functionality I have used previously in WinForms. For some reason the return value of my WndProc isn't giving me the proper result.
我有一个NativeMethods类的所有我的WndProc消息和结果:
I have a NativeMethods class for all my WndProc messages and results:
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;
}
这里是我的窗口的代码:
And here is the code behind for my window:
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;
}
}
我希望光标更改为resize down箭头和调整大小功能工作,因为它在我的WinForms项目。我设置了断点,当光标在期望的位置时,HTBOTTOM返回触发。在XAML我有ResizeMode设置为CanResize和WindowStyle设置为无。我做错了什么?
I expected the cursor to change to the "resize down arrow" and the resizing functionality to work as it did in my WinForms project. I have set breakpoints and the HTBOTTOM return is firing when the cursor is in the expected location. In XAML I have ResizeMode set to CanResize and the WindowStyle set to None. What am I doing wrong?
推荐答案
这是一个愚蠢的错误。我忘记在返回结果之前添加 handled = true;
。现在窗口正常工作。注意,如果您将ResizeMode设置为NoResize,此代码将不会工作。
Well this was a stupid mistake. I forgot to add handled = true;
before I returned the result. Now the window is functioning as normal. As a note if you set the ResizeMode to NoResize this code won't work at all.
这篇关于WPF无边框窗口调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!