1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| public enum ResizeDirection { Left = 1, Right = 2, Top = 3, TopLeft = 4, TopRight = 5, Bottom = 6, BottomLeft = 7, BottomRight = 8 } public partial class MainWindow : Window { private HwndSource _hwndSource; private const int WM_SYSCOMMAND = 0x112; [DllImport("user32.dll",CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); private Dictionary<ResizeDirection, Cursor> cursors = new Dictionary<ResizeDirection, Cursor> { { ResizeDirection.Top,Cursors.SizeNS }, { ResizeDirection.Bottom,Cursors.SizeNS }, { ResizeDirection.Left,Cursors.SizeWE }, { ResizeDirection.Right,Cursors.SizeWE }, { ResizeDirection.TopLeft,Cursors.SizeNWSE }, { ResizeDirection.BottomLeft,Cursors.SizeNWSE }, { ResizeDirection.TopRight,Cursors.SizeNESW }, { ResizeDirection.BottomRight,Cursors.SizeNESW }, }; public MainWindow() { InitializeComponent(); this.SourceInitialized += MainWindow_SourceInitialized; this.MouseMove += MainWindow_MouseMove; }
private void MainWindow_MouseMove(object sender, MouseEventArgs e) { if (e.OriginalSource is FrameworkElement element && !element.Name.Contains("Resize")) Cursor = Cursors.Arrow; }
private void MainWindow_SourceInitialized(object sender, EventArgs e) { _hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource; }
private void ResizePressed(object sender, MouseEventArgs e) { var element = sender as FrameworkElement; ResizeDirection direction = (ResizeDirection)Enum.Parse(typeof(ResizeDirection), element.Name.Replace("Resize", "")); this.Cursor = cursors[direction]; if (e.LeftButton == MouseButtonState.Pressed) ResizeWindow(direction); }
private void ResizeWindow(ResizeDirection direction) { SendMessage(_hwndSource.Handle, WM_SYSCOMMAND, (IntPtr)(61440 + direction), IntPtr.Zero); } }
|