markus / KCOM / Controls / CustomWindow.cs @ ab7fe8c0
이력 | 보기 | 이력해설 | 다운로드 (9.7 KB)
1 | d33ef543 | taeseongkim | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Text; |
||
4 | using System.Windows.Input; |
||
5 | using System.Windows; |
||
6 | using System.Windows.Controls; |
||
7 | using System.Windows.Shapes; |
||
8 | using System.Windows.Interop; |
||
9 | using System.Runtime.InteropServices; |
||
10 | using System.Windows.Media; |
||
11 | using System.Collections; |
||
12 | 40a810fc | taeseongkim | using System.Linq; |
13 | d33ef543 | taeseongkim | |
14 | namespace KCOM.Controls.CustomizedWindow |
||
15 | { |
||
16 | internal static class LocalExtensions |
||
17 | { |
||
18 | public static void ForWindowFromChild(this object childDependencyObject, Action<Window> action) |
||
19 | { |
||
20 | var element = childDependencyObject as DependencyObject; |
||
21 | while (element != null) |
||
22 | { |
||
23 | element = VisualTreeHelper.GetParent(element); |
||
24 | if (element is Window) { action(element as Window); break; } |
||
25 | } |
||
26 | } |
||
27 | |||
28 | public static void ForWindowFromTemplate(this object templateFrameworkElement, Action<Window> action) |
||
29 | { |
||
30 | Window window = ((FrameworkElement)templateFrameworkElement).TemplatedParent as Window; |
||
31 | if (window != null) action(window); |
||
32 | } |
||
33 | |||
34 | public static IntPtr GetWindowHandle(this Window window) |
||
35 | { |
||
36 | WindowInteropHelper helper = new WindowInteropHelper(window); |
||
37 | return helper.Handle; |
||
38 | } |
||
39 | } |
||
40 | |||
41 | 68302e9d | taeseongkim | public class CustomWindow : Window |
42 | d33ef543 | taeseongkim | { |
43 | 68302e9d | taeseongkim | public Rect NomalWindowArea |
44 | { |
||
45 | get { return (Rect)GetValue(NomalWindowAreaProperty); } |
||
46 | set { SetValue(NomalWindowAreaProperty, value); } |
||
47 | } |
||
48 | |||
49 | public static readonly DependencyProperty NomalWindowAreaProperty = DependencyProperty.Register("NomalWindowArea", typeof(Rect),typeof(CustomWindow), null); |
||
50 | |||
51 | |||
52 | public WindowState CustomState |
||
53 | { |
||
54 | get { return (WindowState)GetValue(CustomStateProperty); } |
||
55 | set { SetValue(CustomStateProperty, value); } |
||
56 | } |
||
57 | |||
58 | public static readonly DependencyProperty CustomStateProperty = DependencyProperty.Register("CustomState", typeof(WindowState), |
||
59 | typeof(CustomWindow), new FrameworkPropertyMetadata(WindowState.Normal, new PropertyChangedCallback(CustomStatePropertyChanged))); |
||
60 | 40a810fc | taeseongkim | |
61 | 68302e9d | taeseongkim | private static void CustomStatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
62 | { |
||
63 | |||
64 | } |
||
65 | d33ef543 | taeseongkim | |
66 | 68302e9d | taeseongkim | protected override void OnStateChanged(EventArgs e) |
67 | { |
||
68 | base.OnStateChanged(e); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | public partial class VS2012WindowStyle |
||
73 | { |
||
74 | d33ef543 | taeseongkim | void OnSizeSouth(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.South); } |
75 | void OnSizeNorth(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.North); } |
||
76 | void OnSizeEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.East); } |
||
77 | void OnSizeWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.West); } |
||
78 | void OnSizeNorthWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.NorthWest); } |
||
79 | void OnSizeNorthEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.NorthEast); } |
||
80 | void OnSizeSouthEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.SouthEast); } |
||
81 | void OnSizeSouthWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.SouthWest); } |
||
82 | |||
83 | void OnSize(object sender, SizingAction action) |
||
84 | { |
||
85 | if (Mouse.LeftButton == MouseButtonState.Pressed) |
||
86 | { |
||
87 | sender.ForWindowFromTemplate(w => |
||
88 | { |
||
89 | 68302e9d | taeseongkim | var win = w as CustomWindow; |
90 | |||
91 | if (win.CustomState == WindowState.Normal) |
||
92 | d33ef543 | taeseongkim | DragSize(w.GetWindowHandle(), action); |
93 | }); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | void IconMouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
||
98 | { |
||
99 | if (e.ClickCount > 1) |
||
100 | { |
||
101 | sender.ForWindowFromTemplate(w => w.Close()); |
||
102 | } |
||
103 | else |
||
104 | { |
||
105 | sender.ForWindowFromTemplate(w => |
||
106 | SendMessage(w.GetWindowHandle(), WM_SYSCOMMAND, (IntPtr)SC_KEYMENU, (IntPtr)' ')); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | void CloseButtonClick(object sender, RoutedEventArgs e) |
||
111 | { |
||
112 | sender.ForWindowFromTemplate(w => w.Close()); |
||
113 | } |
||
114 | |||
115 | void MinButtonClick(object sender, RoutedEventArgs e) |
||
116 | { |
||
117 | sender.ForWindowFromTemplate(w => w.WindowState = WindowState.Minimized); |
||
118 | } |
||
119 | |||
120 | void MaxButtonClick(object sender, RoutedEventArgs e) |
||
121 | { |
||
122 | 40a810fc | taeseongkim | sender.ForWindowFromTemplate(w => |
123 | { |
||
124 | 68302e9d | taeseongkim | var win = w as CustomizedWindow.CustomWindow; |
125 | |||
126 | if (win.CustomState == WindowState.Maximized) |
||
127 | 40a810fc | taeseongkim | { |
128 | 68302e9d | taeseongkim | win.WindowState = WindowState.Normal; |
129 | win.CustomState = WindowState.Normal; |
||
130 | |||
131 | win.Dispatcher.Invoke(() => |
||
132 | { |
||
133 | win.Left = win.NomalWindowArea.X; |
||
134 | win.Top = win.NomalWindowArea.Y; |
||
135 | win.Width = win.NomalWindowArea.Width; |
||
136 | win.Height = win.NomalWindowArea.Height; |
||
137 | }); |
||
138 | 40a810fc | taeseongkim | } |
139 | else |
||
140 | { |
||
141 | 68302e9d | taeseongkim | win.WindowState = WindowState.Normal; |
142 | win.CustomState = WindowState.Maximized; |
||
143 | |||
144 | var screen = GetOnScreen(w); |
||
145 | |||
146 | win.NomalWindowArea = new Rect(win.Left, win.Top, win.Width, win.Height); |
||
147 | |||
148 | win.Dispatcher.Invoke(() => |
||
149 | { |
||
150 | win.Left = screen.WorkingArea.X; |
||
151 | win.Width = screen.WorkingArea.Width; |
||
152 | c362d2a5 | taeseongkim | win.Top = 0 + screen.Bounds.Y; |
153 | 68302e9d | taeseongkim | win.Height = screen.WorkingArea.Height; |
154 | }); |
||
155 | 40a810fc | taeseongkim | } |
156 | }); |
||
157 | d33ef543 | taeseongkim | } |
158 | |||
159 | 68302e9d | taeseongkim | public System.Windows.Forms.Screen GetOnScreen(Window window) |
160 | { |
||
161 | var windowRect = new System.Drawing.Rectangle((int)window.Left, (int)window.Top, (int)window.Width, (int)window.Height); |
||
162 | return System.Windows.Forms.Screen.FromRectangle(windowRect); |
||
163 | } |
||
164 | |||
165 | 40a810fc | taeseongkim | void TitleBarMouseDoubleClick(object sender, MouseButtonEventArgs e) |
166 | d33ef543 | taeseongkim | { |
167 | 40a810fc | taeseongkim | |
168 | } |
||
169 | d33ef543 | taeseongkim | |
170 | 40a810fc | taeseongkim | void TitleBarMouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
171 | { |
||
172 | // System.Diagnostics.Debug.WriteLine("Click Time " + new TimeSpan(e.Timestamp).ToString()); |
||
173 | |||
174 | if (e.ClickCount > 1) |
||
175 | d33ef543 | taeseongkim | { |
176 | MaxButtonClick(sender, e); |
||
177 | } |
||
178 | 40a810fc | taeseongkim | //else if (e.LeftButton == MouseButtonState.Pressed) |
179 | //{ |
||
180 | d33ef543 | taeseongkim | |
181 | 40a810fc | taeseongkim | // sender.ForWindowFromTemplate(w => w.DragMove()); |
182 | // //e.Handled = true; |
||
183 | //} |
||
184 | d33ef543 | taeseongkim | } |
185 | |||
186 | void TitleBarMouseMove(object sender, MouseEventArgs e) |
||
187 | { |
||
188 | if (e.LeftButton == MouseButtonState.Pressed) |
||
189 | { |
||
190 | 40a810fc | taeseongkim | sender.ForWindowFromTemplate(w =>w.DragMove()); |
191 | } |
||
192 | // sender.ForWindowFromTemplate(w => |
||
193 | // { |
||
194 | // if (w.WindowState == WindowState.Maximized) |
||
195 | // { |
||
196 | // try |
||
197 | // { |
||
198 | // w.BeginInit(); |
||
199 | // double adjustment = 40.0; |
||
200 | |||
201 | // var mouse1 = GetScreenMousePoint(); |
||
202 | |||
203 | // //var mouse1 = e.MouseDevice.GetPosition(w); |
||
204 | |||
205 | // System.Diagnostics.Debug.WriteLine(mouse1.X + " + " + mouse1.Y); |
||
206 | |||
207 | // var width1 = Math.Max(w.ActualWidth - 2 * adjustment, adjustment); |
||
208 | // w.WindowState = WindowState.Normal; |
||
209 | // var width2 = Math.Max(w.ActualWidth - 2 * adjustment, adjustment); |
||
210 | // w.Left = mouse1.X - width1; |
||
211 | // w.Top = mouse1.Y; |
||
212 | // w.EndInit(); |
||
213 | // w.DragMove(); |
||
214 | // } |
||
215 | // catch (Exception ex) |
||
216 | // { |
||
217 | // } |
||
218 | // } |
||
219 | // else |
||
220 | // { |
||
221 | // w.DragMove(); |
||
222 | // } |
||
223 | // }); |
||
224 | //} |
||
225 | } |
||
226 | |||
227 | private Point GetScreenMousePoint() |
||
228 | { |
||
229 | Point result = new Point(); |
||
230 | |||
231 | //first get all the screens |
||
232 | System.Drawing.Rectangle ret; |
||
233 | |||
234 | var mousePosition = System.Windows.Forms.Cursor.Position; |
||
235 | |||
236 | for (int i = 1; i <= System.Windows.Forms.Screen.AllScreens.Count(); i++) |
||
237 | { |
||
238 | ret = System.Windows.Forms.Screen.AllScreens[i - 1].Bounds; |
||
239 | |||
240 | if (ret.Contains(mousePosition)) |
||
241 | d33ef543 | taeseongkim | { |
242 | 40a810fc | taeseongkim | return new Point(mousePosition.X, mousePosition.Y); |
243 | } |
||
244 | d33ef543 | taeseongkim | } |
245 | 40a810fc | taeseongkim | |
246 | return result; |
||
247 | d33ef543 | taeseongkim | } |
248 | |||
249 | 68302e9d | taeseongkim | |
250 | d33ef543 | taeseongkim | |
251 | #region P/Invoke |
||
252 | |||
253 | const int WM_SYSCOMMAND = 0x112; |
||
254 | const int SC_SIZE = 0xF000; |
||
255 | const int SC_KEYMENU = 0xF100; |
||
256 | |||
257 | [DllImport("user32.dll", CharSet = CharSet.Auto)] |
||
258 | static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); |
||
259 | |||
260 | void DragSize(IntPtr handle, SizingAction sizingAction) |
||
261 | { |
||
262 | SendMessage(handle, WM_SYSCOMMAND, (IntPtr)(SC_SIZE + sizingAction), IntPtr.Zero); |
||
263 | SendMessage(handle, 514, IntPtr.Zero, IntPtr.Zero); |
||
264 | } |
||
265 | |||
266 | public enum SizingAction |
||
267 | { |
||
268 | North = 3, |
||
269 | South = 6, |
||
270 | East = 2, |
||
271 | West = 1, |
||
272 | NorthEast = 5, |
||
273 | NorthWest = 4, |
||
274 | SouthEast = 8, |
||
275 | SouthWest = 7 |
||
276 | } |
||
277 | |||
278 | #endregion |
||
279 | } |
||
280 | } |