I create my own custom window in WPF, and I am trying to implement the resize function that I used earlier in WinForms. For some reason, the return value of my WndProc does not give me the correct result.
I have a NativeMethods class for all 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 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;
I expected the cursor to change to "resize down arrow" and resize functionality to work the same way as in my WinForms project. I set breakpoints and the HTBOTTOM return fires when the cursor is in the expected location. In XAML, my ResizeMode is set to CanResize, and WindowStyle is None. What am I doing wrong?
Cris McLaughlin Jul 15 '13 at 0:57 2013-07-15 00:57
source share