参数定义
1 class NativeMethods 2 { 3 public const int WM_NCHITTEST = 0x84; 4 public const int HTCAPTION = 2; 5 public const int HTLEFT = 10; 6 public const int HTRIGHT = 11; 7 public const int HTTOP = 12; 8 public const int HTTOPLEFT = 13; 9 public const int HTTOPRIGHT = 14; 10 public const int HTBOTTOM = 15; 11 public const int HTBOTTOMLEFT = 16; 12 public const int HTBOTTOMRIGHT = 17; 13 }
加hook
1 IntPtr windowHandle = new WindowInteropHelper(win).Handle; 2 HwndSource windowSource = HwndSource.FromHwnd(windowHandle); 3 windowSource.RemoveHook(WndProc);
1 private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) 2 { 3 int GripSize = 16; 4 int BorderSize = 7; 5 Window win = (Window)System.Windows.Interop.HwndSource.FromHwnd(hwnd).RootVisual; 6 if (msg == NativeMethods.WM_NCHITTEST) 7 { 8 int x = lParam.ToInt32() << 16 >> 16, y = lParam.ToInt32() >> 16; 9 Point pos = win.PointFromScreen(new Point(x, y)); 10 11 //bottom 12 if (pos.X > GripSize && 13 pos.X < win.ActualWidth - GripSize && 14 pos.Y >= win.ActualHeight - BorderSize) 15 { 16 handled = true; 17 return (IntPtr)NativeMethods.HTBOTTOM; 18 } 19 20 //Right 21 if (pos.Y > GripSize && 22 pos.X > win.ActualWidth - BorderSize && 23 pos.Y < win.ActualHeight - GripSize) 24 { 25 handled = true; 26 return (IntPtr)NativeMethods.HTRIGHT; 27 } 28 29 // Top, Left, Right, Corners, Etc. 30 //HTBOTTOMRIGHT 31 if (pos.X > win.ActualWidth - GripSize && 32 pos.Y >= win.ActualHeight - GripSize) 33 { 34 handled = true; 35 return (IntPtr)NativeMethods.HTBOTTOMRIGHT; 36 } 37 } 38 39 return IntPtr.Zero; 40 }