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 class CustomWindow : Window
|
42
|
{
|
43
|
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
|
|
61
|
private static void CustomStatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
62
|
{
|
63
|
|
64
|
}
|
65
|
|
66
|
protected override void OnStateChanged(EventArgs e)
|
67
|
{
|
68
|
base.OnStateChanged(e);
|
69
|
}
|
70
|
}
|
71
|
|
72
|
public partial class VS2012WindowStyle
|
73
|
{
|
74
|
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
|
var win = w as CustomWindow;
|
90
|
|
91
|
if (win.CustomState == WindowState.Normal)
|
92
|
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
|
sender.ForWindowFromTemplate(w =>
|
123
|
{
|
124
|
var win = w as CustomizedWindow.CustomWindow;
|
125
|
|
126
|
if (win.CustomState == WindowState.Maximized)
|
127
|
{
|
128
|
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
|
}
|
139
|
else
|
140
|
{
|
141
|
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
|
win.Top = 0 + screen.Bounds.Y;
|
153
|
win.Height = screen.WorkingArea.Height;
|
154
|
});
|
155
|
|
156
|
int windowHandle = FindWindow("Shell_TrayWnd", string.Empty);
|
157
|
|
158
|
ShowWindow(windowHandle, SW_SHOW);
|
159
|
}
|
160
|
});
|
161
|
}
|
162
|
/// <summary>
|
163
|
/// SW_HIDE
|
164
|
/// </summary>
|
165
|
private const int SW_HIDE = 0;
|
166
|
|
167
|
/// <summary>
|
168
|
/// SW_SHOW
|
169
|
/// </summary>
|
170
|
private const int SW_SHOW = 1;
|
171
|
|
172
|
#region 윈도우 찾기 - FindWindow(className, windowText)
|
173
|
|
174
|
/// <summary>
|
175
|
/// 윈도우 찾기
|
176
|
/// </summary>
|
177
|
/// <param name="className">클래스명</param>
|
178
|
/// <param name="windowText">윈도우 텍스트</param>
|
179
|
/// <returns>윈도우 핸들</returns>
|
180
|
[DllImport("user32.dll")]
|
181
|
private static extern int FindWindow(string className, string windowText);
|
182
|
|
183
|
#endregion
|
184
|
#region 윈도우 표시하기 - ShowWindow(windowHandle, command)
|
185
|
|
186
|
/// <summary>
|
187
|
/// 윈도우 표시하기
|
188
|
/// </summary>
|
189
|
/// <param name="windowHandle">윈도우 핸들</param>
|
190
|
/// <param name="command">명령</param>
|
191
|
/// <returns>처리 결과</returns>
|
192
|
[DllImport("user32.dll")]
|
193
|
private static extern int ShowWindow(int windowHandle, int command);
|
194
|
|
195
|
#endregion
|
196
|
|
197
|
public System.Windows.Forms.Screen GetOnScreen(Window window)
|
198
|
{
|
199
|
var windowRect = new System.Drawing.Rectangle((int)window.Left, (int)window.Top, (int)window.Width, (int)window.Height);
|
200
|
return System.Windows.Forms.Screen.FromRectangle(windowRect);
|
201
|
}
|
202
|
|
203
|
void TitleBarMouseDoubleClick(object sender, MouseButtonEventArgs e)
|
204
|
{
|
205
|
|
206
|
}
|
207
|
|
208
|
void TitleBarMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
209
|
{
|
210
|
// System.Diagnostics.Debug.WriteLine("Click Time " + new TimeSpan(e.Timestamp).ToString());
|
211
|
|
212
|
if (e.ClickCount > 1)
|
213
|
{
|
214
|
MaxButtonClick(sender, e);
|
215
|
}
|
216
|
//else if (e.LeftButton == MouseButtonState.Pressed)
|
217
|
//{
|
218
|
|
219
|
// sender.ForWindowFromTemplate(w => w.DragMove());
|
220
|
// //e.Handled = true;
|
221
|
//}
|
222
|
}
|
223
|
|
224
|
void TitleBarMouseMove(object sender, MouseEventArgs e)
|
225
|
{
|
226
|
if (e.LeftButton == MouseButtonState.Pressed)
|
227
|
{
|
228
|
sender.ForWindowFromTemplate(w =>w.DragMove());
|
229
|
}
|
230
|
// sender.ForWindowFromTemplate(w =>
|
231
|
// {
|
232
|
// if (w.WindowState == WindowState.Maximized)
|
233
|
// {
|
234
|
// try
|
235
|
// {
|
236
|
// w.BeginInit();
|
237
|
// double adjustment = 40.0;
|
238
|
|
239
|
// var mouse1 = GetScreenMousePoint();
|
240
|
|
241
|
// //var mouse1 = e.MouseDevice.GetPosition(w);
|
242
|
|
243
|
// System.Diagnostics.Debug.WriteLine(mouse1.X + " + " + mouse1.Y);
|
244
|
|
245
|
// var width1 = Math.Max(w.ActualWidth - 2 * adjustment, adjustment);
|
246
|
// w.WindowState = WindowState.Normal;
|
247
|
// var width2 = Math.Max(w.ActualWidth - 2 * adjustment, adjustment);
|
248
|
// w.Left = mouse1.X - width1;
|
249
|
// w.Top = mouse1.Y;
|
250
|
// w.EndInit();
|
251
|
// w.DragMove();
|
252
|
// }
|
253
|
// catch (Exception ex)
|
254
|
// {
|
255
|
// }
|
256
|
// }
|
257
|
// else
|
258
|
// {
|
259
|
// w.DragMove();
|
260
|
// }
|
261
|
// });
|
262
|
//}
|
263
|
}
|
264
|
|
265
|
private Point GetScreenMousePoint()
|
266
|
{
|
267
|
Point result = new Point();
|
268
|
|
269
|
//first get all the screens
|
270
|
System.Drawing.Rectangle ret;
|
271
|
|
272
|
var mousePosition = System.Windows.Forms.Cursor.Position;
|
273
|
|
274
|
for (int i = 1; i <= System.Windows.Forms.Screen.AllScreens.Count(); i++)
|
275
|
{
|
276
|
ret = System.Windows.Forms.Screen.AllScreens[i - 1].Bounds;
|
277
|
|
278
|
if (ret.Contains(mousePosition))
|
279
|
{
|
280
|
return new Point(mousePosition.X, mousePosition.Y);
|
281
|
}
|
282
|
}
|
283
|
|
284
|
return result;
|
285
|
}
|
286
|
|
287
|
|
288
|
|
289
|
#region P/Invoke
|
290
|
|
291
|
const int WM_SYSCOMMAND = 0x112;
|
292
|
const int SC_SIZE = 0xF000;
|
293
|
const int SC_KEYMENU = 0xF100;
|
294
|
|
295
|
[DllImport("user32.dll", CharSet = CharSet.Auto)]
|
296
|
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
|
297
|
|
298
|
void DragSize(IntPtr handle, SizingAction sizingAction)
|
299
|
{
|
300
|
SendMessage(handle, WM_SYSCOMMAND, (IntPtr)(SC_SIZE + sizingAction), IntPtr.Zero);
|
301
|
SendMessage(handle, 514, IntPtr.Zero, IntPtr.Zero);
|
302
|
}
|
303
|
|
304
|
public enum SizingAction
|
305
|
{
|
306
|
North = 3,
|
307
|
South = 6,
|
308
|
East = 2,
|
309
|
West = 1,
|
310
|
NorthEast = 5,
|
311
|
NorthWest = 4,
|
312
|
SouthEast = 8,
|
313
|
SouthWest = 7
|
314
|
}
|
315
|
|
316
|
#endregion
|
317
|
}
|
318
|
}
|