|
3 | 3 | using System.Windows.Interop; |
4 | 4 | using System.Runtime.InteropServices; |
5 | 5 | using System.Windows.Input; |
| 6 | +using System.Windows.Shapes; |
6 | 7 |
|
7 | 8 | namespace SimpleStateMachineNodeEditor.Styles |
8 | 9 | { |
9 | 10 | public partial class CustomWindowTemplate |
10 | 11 | { |
11 | | - #region sizing event handlers |
12 | | - |
13 | | - void OnSizeSouth(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.South); } |
14 | | - |
15 | | - void OnSizeNorth(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.North); } |
16 | | - |
17 | | - void OnSizeEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.East); } |
18 | | - |
19 | | - void OnSizeWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.West); } |
20 | | - |
21 | | - void OnSizeNorthWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.NorthWest); } |
22 | | - |
23 | | - void OnSizeNorthEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.NorthEast); } |
24 | | - |
25 | | - void OnSizeSouthEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.SouthEast); } |
26 | | - |
27 | | - void OnSizeSouthWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.SouthWest); } |
28 | | - |
29 | | - void OnSize(object sender, SizingAction action) |
| 12 | + bool ResizeInProcess = false; |
| 13 | + int shift = 0; |
| 14 | + private void ResizeStart(object sender, MouseButtonEventArgs e) |
30 | 15 | { |
31 | | - if (((FrameworkElement)sender).TemplatedParent is Window wnd) |
| 16 | + Rectangle senderRect = sender as Rectangle; |
| 17 | + if (senderRect != null) |
32 | 18 | { |
33 | | - WindowInteropHelper helper = new WindowInteropHelper(wnd); |
34 | | - DragSize(helper.Handle, action); |
| 19 | + ResizeInProcess = true; |
| 20 | + senderRect.CaptureMouse(); |
35 | 21 | } |
36 | 22 | } |
37 | 23 |
|
38 | | - #endregion |
39 | | - |
40 | | - #region P/Invoke and helper method |
41 | | - |
42 | | - const int WM_SYSCOMMAND = 0x112; |
43 | | - const int SC_SIZE = 0xF000; |
44 | | - |
45 | | - |
46 | | - [DllImport("user32.dll", CharSet = CharSet.Auto)] |
47 | | - static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); |
48 | | - |
49 | | - void DragSize(IntPtr handle, SizingAction sizingAction) |
| 24 | + private void ResizeEnd(object sender, MouseButtonEventArgs e) |
50 | 25 | { |
51 | | - if (Mouse.LeftButton == MouseButtonState.Pressed) |
| 26 | + Rectangle senderRect = sender as Rectangle; |
| 27 | + if (senderRect != null) |
52 | 28 | { |
53 | | - SendMessage(handle, WM_SYSCOMMAND, (IntPtr)(SC_SIZE + sizingAction), IntPtr.Zero); |
54 | | - SendMessage(handle, 514, IntPtr.Zero, IntPtr.Zero); |
| 29 | + ResizeInProcess = false; ; |
| 30 | + senderRect.ReleaseMouseCapture(); |
55 | 31 | } |
56 | 32 | } |
57 | 33 |
|
58 | | - #endregion |
59 | | - |
60 | | - #region helper enum |
61 | | - |
62 | | - public enum SizingAction |
| 34 | + private void Resizeing_Form(object sender, MouseEventArgs e) |
63 | 35 | { |
64 | | - North = 3, |
65 | | - South = 6, |
66 | | - East = 2, |
67 | | - West = 1, |
68 | | - NorthEast = 5, |
69 | | - NorthWest = 4, |
70 | | - SouthEast = 8, |
71 | | - SouthWest = 7 |
| 36 | + if (ResizeInProcess) |
| 37 | + { |
| 38 | + double temp = 0; |
| 39 | + Rectangle senderRect = sender as Rectangle; |
| 40 | + Window mainWindow = senderRect.Tag as Window; |
| 41 | + if (senderRect != null) |
| 42 | + { |
| 43 | + double width = e.GetPosition(mainWindow).X; |
| 44 | + double height = e.GetPosition(mainWindow).Y; |
| 45 | + senderRect.CaptureMouse(); |
| 46 | + if (senderRect.Name.Contains("right", StringComparison.OrdinalIgnoreCase)) |
| 47 | + { |
| 48 | + width += shift; |
| 49 | + if (width > 0) |
| 50 | + mainWindow.Width = width; |
| 51 | + } |
| 52 | + if (senderRect.Name.Contains("left", StringComparison.OrdinalIgnoreCase)) |
| 53 | + { |
| 54 | + width -= shift; |
| 55 | + temp = mainWindow.Width - width; |
| 56 | + if ((temp > mainWindow.MinWidth) && (temp < mainWindow.MaxWidth)) |
| 57 | + { |
| 58 | + mainWindow.Width = temp; |
| 59 | + mainWindow.Left += width; |
| 60 | + } |
| 61 | + } |
| 62 | + if (senderRect.Name.Contains("bottom", StringComparison.OrdinalIgnoreCase)) |
| 63 | + { |
| 64 | + height += shift; |
| 65 | + if (height > 0) |
| 66 | + mainWindow.Height = height; |
| 67 | + } |
| 68 | + if (senderRect.Name.ToLower().Contains("top", StringComparison.OrdinalIgnoreCase)) |
| 69 | + { |
| 70 | + height -= shift; |
| 71 | + temp = mainWindow.Height - height; |
| 72 | + if ((temp > mainWindow.MinHeight) && (temp < mainWindow.MaxHeight)) |
| 73 | + { |
| 74 | + mainWindow.Height = temp; |
| 75 | + mainWindow.Top += height; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
72 | 80 | } |
73 | 81 |
|
74 | | - #endregion |
75 | 82 | } |
76 | 83 | } |
0 commit comments