프로젝트

일반

사용자정보

개정판 aff63364

IDaff6336420baf43f655d71dd08142920a0bbea1c
상위 8a2231b2
하위 00c771e0, 65fbe3cb

김태성이(가) 5년 이상 전에 추가함

WindowBehavior
WindowHelper 추가
메인 윈도우에 따른 자식창의 최초 위치를 자동으로 center로 수정함.

Change-Id: I5d94b56162fe551809cb4a90e50a8e4bf97c55ab

차이점 보기:

KCOM/App.xaml.cs
95 95

  
96 96
        public App()
97 97
        {
98

  
98
            Telerik.Windows.Controls.StyleManager.ApplicationTheme = new Telerik.Windows.Controls.VisualStudio2013Theme();
99 99
        }
100 100

  
101 101
        protected override async void OnStartup(StartupEventArgs e)
KCOM/Behaviors/WindowBehavior.cs
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
}
KCOM/Extensions/WindowHelper.cs
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.Interactivity;
7

  
8
namespace KCOM
9
{
10
    public static class WindowHelper
11
    {
12
        public static void ShowCenter(this System.Windows.Controls.ContentControl window)
13
        {
14
            SetApplicationCenterBehavior(window);
15

  
16
            if (window is System.Windows.Window)
17
            {
18
                (window as System.Windows.Window).Show();
19
            }
20
            else if (window is Telerik.Windows.Controls.RadWindow)
21
            {
22
                (window as System.Windows.Window).Show();
23
            }
24
            else
25
            {
26
                throw new Exception("window Type is System.Windows.Window or  Telerik.Windows.Controls.RadWindow");
27
            }
28
        }
29

  
30
        public static bool? ShowDialogCenter(this System.Windows.Window window)
31
        {
32
            if (window is System.Windows.Window)
33
            {
34
                SetApplicationCenterBehavior(window);
35

  
36
                return window.ShowDialog();
37
            }
38
            else
39
            {
40
                throw new Exception("window Type is System.Windows.Window");
41
            }
42
        }
43

  
44
        public static void ShowDialogCenter(this Telerik.Windows.Controls.RadWindow window)
45
        {
46
            if (window is Telerik.Windows.Controls.RadWindow)
47
            {
48
                SetApplicationCenterBehavior(window);
49
                window.ShowDialog();
50
            }
51
            else
52
            {
53
                throw new Exception("window Type is Telerik.Windows.Controls.RadWindow");
54
            }
55
        }
56

  
57
        private static void SetApplicationCenterBehavior(System.Windows.Controls.ContentControl window)
58
        {
59
            if (window.GetType().GetProperty("Owner").GetValue(window) == null)
60
            {
61
                window.GetType().GetProperty("Owner").SetValue(window, App.Current.Windows.Cast<System.Windows.Window>().FirstOrDefault());
62
            }
63

  
64
            if (Interaction.GetBehaviors(window).Count(f => f is KCOM.Behaviors.WindowBehavior) == 0)
65
            {
66
                KCOM.Behaviors.WindowBehavior windowBehavior = new Behaviors.WindowBehavior();
67

  
68
                windowBehavior.SetValue(KCOM.Behaviors.WindowBehavior.ApplicationCenterProperty, true);
69

  
70
                //windowBehavior.SetBinding(KCOM.Behaviors.WindowBehavior.ApplicationCenterProperty, new Binding()
71
                //{
72
                //    ElementName = "_uc",
73
                //    Path = new PropertyPath("SelectedItems"),
74
                //    Mode = BindingMode.TwoWay
75
                //});
76

  
77
                Interaction.GetBehaviors(window).Add(windowBehavior);
78
            }
79
        }
80
    }
81
}
KCOM/KCOM.csproj
258 258
    </ApplicationDefinition>
259 259
    <Compile Include="AbstractDatabase.cs" />
260 260
    <Compile Include="AppSQLiteDatabase.cs" />
261
    <Compile Include="Behaviors\WindowBehavior.cs" />
261 262
    <Compile Include="Common\Check_Inferface.cs" />
262 263
    <Compile Include="Common\Check_Uri.cs" />
263 264
    <Compile Include="Common\Commons.cs" />
......
329 330
    <Compile Include="Events\UndoCommand.cs" />
330 331
    <Compile Include="Events\SaveCommand.cs" />
331 332
    <Compile Include="Events\RedoCommand.cs" />
333
    <Compile Include="Extensions\WindowHelper.cs" />
332 334
    <Compile Include="IAbstractDatabase.cs" />
333 335
    <Compile Include="Logger.cs" />
334 336
    <Compile Include="Messenger\ConversationView.xaml.cs">

내보내기 Unified diff

클립보드 이미지 추가 (최대 크기: 500 MB)