프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / Controls / CustomWindow.cs @ 0d97ab05

이력 | 보기 | 이력해설 | 다운로드 (9.74 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 0d2d2a08 이지연
               
131 68302e9d taeseongkim
                    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 0d2d2a08 이지연
                 
144 68302e9d taeseongkim
                    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 e550eccb 이지연
   
159 d33ef543 taeseongkim
160 68302e9d taeseongkim
        public System.Windows.Forms.Screen GetOnScreen(Window window)
161
        {
162
            var windowRect = new System.Drawing.Rectangle((int)window.Left, (int)window.Top, (int)window.Width, (int)window.Height);
163
            return System.Windows.Forms.Screen.FromRectangle(windowRect);
164
        }
165
166 40a810fc taeseongkim
        void TitleBarMouseDoubleClick(object sender, MouseButtonEventArgs e)
167 d33ef543 taeseongkim
        {
168 40a810fc taeseongkim
      
169
        }
170 d33ef543 taeseongkim
171 40a810fc taeseongkim
        void TitleBarMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
172
        {
173
            // System.Diagnostics.Debug.WriteLine("Click Time " + new TimeSpan(e.Timestamp).ToString());
174
         
175
            if (e.ClickCount > 1)
176 d33ef543 taeseongkim
            {
177
                MaxButtonClick(sender, e);
178
            }
179 40a810fc taeseongkim
            //else if (e.LeftButton == MouseButtonState.Pressed)
180
            //{
181 d33ef543 taeseongkim
182 40a810fc taeseongkim
           // sender.ForWindowFromTemplate(w => w.DragMove());
183
            //    //e.Handled = true;
184
            //}
185 d33ef543 taeseongkim
        }
186
187
        void TitleBarMouseMove(object sender, MouseEventArgs e)
188
        {
189
            if (e.LeftButton == MouseButtonState.Pressed)
190
            {
191 40a810fc taeseongkim
                sender.ForWindowFromTemplate(w =>w.DragMove());
192
            }
193
            //    sender.ForWindowFromTemplate(w =>
194
            //    {
195
            //        if (w.WindowState == WindowState.Maximized)
196
            //        {
197
            //            try
198
            //            {
199
            //                w.BeginInit();
200
            //                double adjustment = 40.0;
201
202
            //                var mouse1 = GetScreenMousePoint();
203
204
            //                //var mouse1 = e.MouseDevice.GetPosition(w);
205
206
            //                System.Diagnostics.Debug.WriteLine(mouse1.X + " + " + mouse1.Y);
207
208
            //                var width1 = Math.Max(w.ActualWidth - 2 * adjustment, adjustment);
209
            //                w.WindowState = WindowState.Normal;
210
            //                var width2 = Math.Max(w.ActualWidth - 2 * adjustment, adjustment);
211
            //                w.Left = mouse1.X - width1;
212
            //                w.Top = mouse1.Y;
213
            //                w.EndInit();
214
            //                w.DragMove();
215
            //            }
216
            //            catch (Exception ex)
217
            //            {
218
            //            }
219
            //        }
220
            //        else
221
            //        {
222
            //           w.DragMove();
223
            //        }
224
            //    });
225
            //}
226
        }
227
228
        private Point GetScreenMousePoint()
229
        {
230
            Point result = new Point();
231
232
            //first get all the screens 
233
            System.Drawing.Rectangle ret;
234
       
235
            var mousePosition = System.Windows.Forms.Cursor.Position;
236
237
            for (int i = 1; i <= System.Windows.Forms.Screen.AllScreens.Count(); i++)
238
            {
239
                ret = System.Windows.Forms.Screen.AllScreens[i - 1].Bounds;
240
241
                if (ret.Contains(mousePosition))
242 d33ef543 taeseongkim
                {
243 40a810fc taeseongkim
                    return new Point(mousePosition.X, mousePosition.Y);
244
                }
245 d33ef543 taeseongkim
            }
246 40a810fc taeseongkim
247
            return result;
248 d33ef543 taeseongkim
        }
249
250 68302e9d taeseongkim
        
251 d33ef543 taeseongkim
252
        #region P/Invoke
253
254
        const int WM_SYSCOMMAND = 0x112;
255
        const int SC_SIZE = 0xF000;
256
        const int SC_KEYMENU = 0xF100;
257
258
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
259
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
260
261
        void DragSize(IntPtr handle, SizingAction sizingAction)
262
        {
263
            SendMessage(handle, WM_SYSCOMMAND, (IntPtr)(SC_SIZE + sizingAction), IntPtr.Zero);
264
            SendMessage(handle, 514, IntPtr.Zero, IntPtr.Zero);
265
        }
266
267
        public enum SizingAction
268
        {
269
            North = 3,
270
            South = 6,
271
            East = 2,
272
            West = 1,
273
            NorthEast = 5,
274
            NorthWest = 4,
275
            SouthEast = 8,
276
            SouthWest = 7
277
        }
278
279
        #endregion
280
    }
281
}
클립보드 이미지 추가 (최대 크기: 500 MB)