markus / KCOM / Controls / CustomWindow.cs @ 40a810fc
이력 | 보기 | 이력해설 | 다운로드 (7.27 KB)
1 |
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 |
using System.Linq; |
13 |
|
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 |
public partial class VS2012WindowStyle |
42 |
{ |
43 |
|
44 |
#region sizing event handlers |
45 |
|
46 |
void OnSizeSouth(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.South); } |
47 |
void OnSizeNorth(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.North); } |
48 |
void OnSizeEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.East); } |
49 |
void OnSizeWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.West); } |
50 |
void OnSizeNorthWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.NorthWest); } |
51 |
void OnSizeNorthEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.NorthEast); } |
52 |
void OnSizeSouthEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.SouthEast); } |
53 |
void OnSizeSouthWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.SouthWest); } |
54 |
|
55 |
void OnSize(object sender, SizingAction action) |
56 |
{ |
57 |
if (Mouse.LeftButton == MouseButtonState.Pressed) |
58 |
{ |
59 |
sender.ForWindowFromTemplate(w => |
60 |
{ |
61 |
if (w.WindowState == WindowState.Normal) |
62 |
DragSize(w.GetWindowHandle(), action); |
63 |
}); |
64 |
} |
65 |
} |
66 |
|
67 |
void IconMouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
68 |
{ |
69 |
if (e.ClickCount > 1) |
70 |
{ |
71 |
sender.ForWindowFromTemplate(w => w.Close()); |
72 |
} |
73 |
else |
74 |
{ |
75 |
sender.ForWindowFromTemplate(w => |
76 |
SendMessage(w.GetWindowHandle(), WM_SYSCOMMAND, (IntPtr)SC_KEYMENU, (IntPtr)' ')); |
77 |
} |
78 |
} |
79 |
|
80 |
void CloseButtonClick(object sender, RoutedEventArgs e) |
81 |
{ |
82 |
sender.ForWindowFromTemplate(w => w.Close()); |
83 |
} |
84 |
|
85 |
void MinButtonClick(object sender, RoutedEventArgs e) |
86 |
{ |
87 |
sender.ForWindowFromTemplate(w => w.WindowState = WindowState.Minimized); |
88 |
} |
89 |
|
90 |
void MaxButtonClick(object sender, RoutedEventArgs e) |
91 |
{ |
92 |
sender.ForWindowFromTemplate(w => |
93 |
{ |
94 |
if (w.WindowState == WindowState.Maximized) |
95 |
{ |
96 |
w.WindowState = WindowState.Normal; |
97 |
} |
98 |
else |
99 |
{ |
100 |
w.WindowState = WindowState.Maximized; |
101 |
} |
102 |
}); |
103 |
} |
104 |
|
105 |
void TitleBarMouseDoubleClick(object sender, MouseButtonEventArgs e) |
106 |
{ |
107 |
|
108 |
} |
109 |
|
110 |
void TitleBarMouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
111 |
{ |
112 |
// System.Diagnostics.Debug.WriteLine("Click Time " + new TimeSpan(e.Timestamp).ToString()); |
113 |
|
114 |
if (e.ClickCount > 1) |
115 |
{ |
116 |
MaxButtonClick(sender, e); |
117 |
} |
118 |
//else if (e.LeftButton == MouseButtonState.Pressed) |
119 |
//{ |
120 |
|
121 |
// sender.ForWindowFromTemplate(w => w.DragMove()); |
122 |
// //e.Handled = true; |
123 |
//} |
124 |
} |
125 |
|
126 |
void TitleBarMouseMove(object sender, MouseEventArgs e) |
127 |
{ |
128 |
if (e.LeftButton == MouseButtonState.Pressed) |
129 |
{ |
130 |
sender.ForWindowFromTemplate(w =>w.DragMove()); |
131 |
} |
132 |
// sender.ForWindowFromTemplate(w => |
133 |
// { |
134 |
// if (w.WindowState == WindowState.Maximized) |
135 |
// { |
136 |
// try |
137 |
// { |
138 |
// w.BeginInit(); |
139 |
// double adjustment = 40.0; |
140 |
|
141 |
// var mouse1 = GetScreenMousePoint(); |
142 |
|
143 |
// //var mouse1 = e.MouseDevice.GetPosition(w); |
144 |
|
145 |
// System.Diagnostics.Debug.WriteLine(mouse1.X + " + " + mouse1.Y); |
146 |
|
147 |
// var width1 = Math.Max(w.ActualWidth - 2 * adjustment, adjustment); |
148 |
// w.WindowState = WindowState.Normal; |
149 |
// var width2 = Math.Max(w.ActualWidth - 2 * adjustment, adjustment); |
150 |
// w.Left = mouse1.X - width1; |
151 |
// w.Top = mouse1.Y; |
152 |
// w.EndInit(); |
153 |
// w.DragMove(); |
154 |
// } |
155 |
// catch (Exception ex) |
156 |
// { |
157 |
// } |
158 |
// } |
159 |
// else |
160 |
// { |
161 |
// w.DragMove(); |
162 |
// } |
163 |
// }); |
164 |
//} |
165 |
} |
166 |
|
167 |
private Point GetScreenMousePoint() |
168 |
{ |
169 |
Point result = new Point(); |
170 |
|
171 |
//first get all the screens |
172 |
System.Drawing.Rectangle ret; |
173 |
|
174 |
var mousePosition = System.Windows.Forms.Cursor.Position; |
175 |
|
176 |
for (int i = 1; i <= System.Windows.Forms.Screen.AllScreens.Count(); i++) |
177 |
{ |
178 |
ret = System.Windows.Forms.Screen.AllScreens[i - 1].Bounds; |
179 |
|
180 |
if (ret.Contains(mousePosition)) |
181 |
{ |
182 |
return new Point(mousePosition.X, mousePosition.Y); |
183 |
} |
184 |
} |
185 |
|
186 |
return result; |
187 |
} |
188 |
|
189 |
#endregion |
190 |
|
191 |
#region P/Invoke |
192 |
|
193 |
const int WM_SYSCOMMAND = 0x112; |
194 |
const int SC_SIZE = 0xF000; |
195 |
const int SC_KEYMENU = 0xF100; |
196 |
|
197 |
[DllImport("user32.dll", CharSet = CharSet.Auto)] |
198 |
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); |
199 |
|
200 |
void DragSize(IntPtr handle, SizingAction sizingAction) |
201 |
{ |
202 |
SendMessage(handle, WM_SYSCOMMAND, (IntPtr)(SC_SIZE + sizingAction), IntPtr.Zero); |
203 |
SendMessage(handle, 514, IntPtr.Zero, IntPtr.Zero); |
204 |
} |
205 |
|
206 |
public enum SizingAction |
207 |
{ |
208 |
North = 3, |
209 |
South = 6, |
210 |
East = 2, |
211 |
West = 1, |
212 |
NorthEast = 5, |
213 |
NorthWest = 4, |
214 |
SouthEast = 8, |
215 |
SouthWest = 7 |
216 |
} |
217 |
|
218 |
#endregion |
219 |
} |
220 |
} |