프로젝트

일반

사용자정보

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

markus / KCOM / Controls / CustomWindow.cs @ d33ef543

이력 | 보기 | 이력해설 | 다운로드 (5.9 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

    
13
namespace KCOM.Controls.CustomizedWindow
14
{
15
    internal static class LocalExtensions
16
    {
17
        public static void ForWindowFromChild(this object childDependencyObject, Action<Window> action)
18
        {
19
            var element = childDependencyObject as DependencyObject;
20
            while (element != null)
21
            {
22
                element = VisualTreeHelper.GetParent(element);
23
                if (element is Window) { action(element as Window); break; }
24
            }
25
        }
26

    
27
        public static void ForWindowFromTemplate(this object templateFrameworkElement, Action<Window> action)
28
        {
29
            Window window = ((FrameworkElement)templateFrameworkElement).TemplatedParent as Window;
30
            if (window != null) action(window);
31
        }
32

    
33
        public static IntPtr GetWindowHandle(this Window window)
34
        {
35
            WindowInteropHelper helper = new WindowInteropHelper(window);
36
            return helper.Handle;
37
        }
38
    }
39

    
40
    public partial class VS2012WindowStyle
41
    {
42
        #region sizing event handlers
43

    
44
        void OnSizeSouth(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.South); }
45
        void OnSizeNorth(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.North); }
46
        void OnSizeEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.East); }
47
        void OnSizeWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.West); }
48
        void OnSizeNorthWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.NorthWest); }
49
        void OnSizeNorthEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.NorthEast); }
50
        void OnSizeSouthEast(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.SouthEast); }
51
        void OnSizeSouthWest(object sender, MouseButtonEventArgs e) { OnSize(sender, SizingAction.SouthWest); }
52

    
53
        void OnSize(object sender, SizingAction action)
54
        {
55
            if (Mouse.LeftButton == MouseButtonState.Pressed)
56
            {
57
                sender.ForWindowFromTemplate(w =>
58
                {
59
                    if (w.WindowState == WindowState.Normal)
60
                        DragSize(w.GetWindowHandle(), action);
61
                });
62
            }
63
        }
64

    
65
        void IconMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
66
        {
67
            if (e.ClickCount > 1)
68
            {
69
                sender.ForWindowFromTemplate(w => w.Close());
70
            }
71
            else
72
            {
73
                sender.ForWindowFromTemplate(w =>
74
                    SendMessage(w.GetWindowHandle(), WM_SYSCOMMAND, (IntPtr)SC_KEYMENU, (IntPtr)' '));
75
            }
76
        }
77

    
78
        void CloseButtonClick(object sender, RoutedEventArgs e)
79
        {
80
            sender.ForWindowFromTemplate(w => w.Close());
81
        }
82

    
83
        void MinButtonClick(object sender, RoutedEventArgs e)
84
        {
85
            sender.ForWindowFromTemplate(w => w.WindowState = WindowState.Minimized);
86
        }
87

    
88
        void MaxButtonClick(object sender, RoutedEventArgs e)
89
        {
90
            sender.ForWindowFromTemplate(w => w.WindowState = (w.WindowState == WindowState.Maximized) ? WindowState.Normal : WindowState.Maximized);
91
        }
92

    
93
        void TitleBarMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
94
        {
95
           // System.Diagnostics.Debug.WriteLine("Click Time " + new TimeSpan(e.Timestamp).ToString());
96

    
97
            if (e.ClickCount == 2 && new TimeSpan(e.Timestamp) < new TimeSpan(0, 0, 0, 3))
98
            {
99
                MaxButtonClick(sender, e);
100

    
101
                e.Handled = true;
102
            }
103
            else if (e.LeftButton == MouseButtonState.Pressed)
104
            {
105
                sender.ForWindowFromTemplate(w => w.DragMove());
106

    
107
                e.Handled = true;
108
            }
109
        }
110

    
111
        void TitleBarMouseMove(object sender, MouseEventArgs e)
112
        {
113
            if (e.LeftButton == MouseButtonState.Pressed)
114
            {
115
                sender.ForWindowFromTemplate(w =>
116
                {
117
                    if (w.WindowState == WindowState.Maximized)
118
                    {
119
                        w.BeginInit();
120
                        double adjustment = 40.0;
121
                        var mouse1 = e.MouseDevice.GetPosition(w);
122

    
123
                        System.Diagnostics.Debug.WriteLine(mouse1.X + " + " + mouse1.Y);
124

    
125
                        var width1 = Math.Max(w.ActualWidth - 2 * adjustment, adjustment);
126
                        w.WindowState = WindowState.Normal;
127
                        var width2 = Math.Max(w.ActualWidth - 2 * adjustment, adjustment);
128
                        w.Left = (mouse1.X - adjustment) * (1 - width2 / width1);
129
                        w.Top = -7;
130
                        w.EndInit();
131
                        w.DragMove();
132
                    }
133
                });
134
            }
135
        }
136

    
137
        #endregion
138

    
139
        #region P/Invoke
140

    
141
        const int WM_SYSCOMMAND = 0x112;
142
        const int SC_SIZE = 0xF000;
143
        const int SC_KEYMENU = 0xF100;
144

    
145
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
146
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
147

    
148
        void DragSize(IntPtr handle, SizingAction sizingAction)
149
        {
150
            SendMessage(handle, WM_SYSCOMMAND, (IntPtr)(SC_SIZE + sizingAction), IntPtr.Zero);
151
            SendMessage(handle, 514, IntPtr.Zero, IntPtr.Zero);
152
        }
153

    
154
        public enum SizingAction
155
        {
156
            North = 3,
157
            South = 6,
158
            East = 2,
159
            West = 1,
160
            NorthEast = 5,
161
            NorthWest = 4,
162
            SouthEast = 8,
163
            SouthWest = 7
164
        }
165

    
166
        #endregion
167
    }
168
}
클립보드 이미지 추가 (최대 크기: 500 MB)