프로젝트

일반

사용자정보

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

markus / KCOM / Behaviors / WindowBehavior.cs @ b1fc6833

이력 | 보기 | 이력해설 | 다운로드 (4.5 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using System.Windows;
7
using System.Windows.Media;
8
using System.Windows.Shapes;
9

    
10
namespace KCOM.Behaviors
11
{
12
    public class WindowBehavior : System.Windows.Interactivity.Behavior<System.Windows.Controls.Control>
13
    {
14
        protected override void OnAttached()
15
        {
16
            AssociatedObject.Loaded += AssociatedObject_Loaded;
17
            base.OnAttached();
18
        }
19

    
20
        protected override void OnDetaching()
21
        {
22
            AssociatedObject.Loaded -= AssociatedObject_Loaded;
23
            base.OnDetaching();
24
        }
25

    
26
        private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
27
        {
28
            var objectType = AssociatedObject.GetType();
29

    
30
            double objectWidth;
31
            double objectHeight;
32

    
33
            double ownerLeft = 0.0;
34
            double ownerTop = 0.0;
35
            double ownerWidth = 0.0;
36
            double ownerHeight = 0.0;
37

    
38

    
39
            if (AssociatedObject is System.Windows.Window || AssociatedObject is Telerik.Windows.Controls.RadWindow)
40
            {
41
                
42
                if (objectType.GetProperty("Owner") != null)
43
                {
44
                    var owner = AssociatedObject.GetType().GetProperty("Owner").GetValue(AssociatedObject);
45

    
46
                    if (owner is System.Windows.Controls.ContentControl)
47
                    {
48
                        var  ownerType = owner.GetType();
49

    
50
                        objectWidth = (double)objectType.GetProperty("Width").GetValue(AssociatedObject);
51
                        objectHeight = (double)objectType.GetProperty("Height").GetValue(AssociatedObject);
52

    
53
                        ownerLeft = (double)ownerType.GetProperty("Left").GetValue(owner);
54
                        ownerTop = (double)ownerType.GetProperty("Top").GetValue(owner);
55
                        ownerWidth = (double)ownerType.GetProperty("Width").GetValue(owner);
56
                        ownerHeight = (double)ownerType.GetProperty("Height").GetValue(owner);
57

    
58
                        if ((WindowState)ownerType.GetProperty("WindowState").GetValue(owner) != WindowState.Normal)
59
                        {
60
                            Point locationFromScreen = (owner as Visual).PointToScreen(new Point(0, 0));
61

    
62
                            ownerLeft = locationFromScreen.X;
63
                            ownerTop = locationFromScreen.Y;
64
                        }
65

    
66
                        objectType.GetProperty("Left").SetValue(AssociatedObject, ownerLeft + (ownerWidth - objectWidth) / 2);
67
                        objectType.GetProperty("Top").SetValue(AssociatedObject, ownerTop + (ownerHeight - objectHeight) / 2);
68
                    }
69
                    else
70
                    {
71
                        throw new Exception("Owner base Type is Not System.Windows.Controls.ContentControl");
72
                    }
73
                }
74
                else
75
                {
76
                    throw new Exception("Owner is Null");
77
                }
78
            }
79
            else
80
            {
81
                throw new Exception("this Behavior Only System.Windows.Window or Telerik.Windows.Controls.RadWindow");
82
            }
83

    
84

    
85
        }
86

    
87
        public static bool GetApplicationCenter(DependencyObject obj)
88
        {
89
            return (bool)obj.GetValue(ApplicationCenterProperty);
90
        }
91
        public static void SetApplicationCenter(DependencyObject obj, bool value)
92
        {
93
            obj.SetValue(ApplicationCenterProperty, value);
94
        }
95

    
96
        public static readonly DependencyProperty ApplicationCenterProperty = DependencyProperty.RegisterAttached("ApplicationCenter", typeof(bool), 
97
                                                                    typeof(WindowBehavior), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(ApplicationCenterPropertyChanged)));
98

    
99
        private static void ApplicationCenterPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
100
        {
101

    
102
        }
103
    }
104

    
105
    public class ScreenBoundsConverter
106
    {
107
        private Matrix _transform;
108

    
109
        public ScreenBoundsConverter(Visual visual)
110
        {
111
            _transform =
112
              PresentationSource.FromVisual(visual).CompositionTarget.TransformFromDevice;
113
        }
114

    
115
        public Rect ConvertBounds(System.Drawing.Rectangle bounds)
116
        {
117
            var result = new Rect(bounds.X, bounds.Y, bounds.Width, bounds.Height);
118
            result.Transform(_transform);
119
            return result;
120
        }
121
    }
122
}
클립보드 이미지 추가 (최대 크기: 500 MB)