tangxu
2024-12-24 91105b77c916d06dd30380e20594e29f85eae3da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// COPYRIGHT (C) Tom. ALL RIGHTS RESERVED.
// THE AntdUI PROJECT IS AN WINFORM LIBRARY LICENSED UNDER THE Apache-2.0 License.
// LICENSED UNDER THE Apache License, VERSION 2.0 (THE "License")
// YOU MAY NOT USE THIS FILE EXCEPT IN COMPLIANCE WITH THE License.
// YOU MAY OBTAIN A COPY OF THE LICENSE AT
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE
// DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
// SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING PERMISSIONS AND
// LIMITATIONS UNDER THE License.
// GITEE: https://gitee.com/antdui/AntdUI
// GITHUB: https://github.com/AntdUI/AntdUI
// CSDN: https://blog.csdn.net/v_132
// QQ: 17379620
 
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
 
namespace AntdUI
{
    partial class Tabs
    {
        internal class TabControlDesigner : ParentControlDesigner
        {
            public new Tabs Control => (Tabs)base.Control;
 
            #region 初始化
 
            public IDesignerHost DesignerHost { get; private set; }
            public ISelectionService SelectionService { get; private set; }
 
            public override void Initialize(IComponent component)
            {
                base.Initialize(component);
                DesignerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
                SelectionService = (ISelectionService)GetService(typeof(ISelectionService));
            }
 
            DesignerActionListCollection actionLists;
            public override DesignerActionListCollection ActionLists
            {
                get
                {
                    if (actionLists == null)
                    {
                        actionLists = base.ActionLists;
                        actionLists.Add(new TabControlActionList(Control));
                    }
                    return actionLists;
                }
            }
 
            #endregion
 
            protected override bool GetHitTest(Point point)
            {
                var point_ = Control.PointToClient(point);
                foreach (var tab in Control.Pages)
                {
                    if (tab.Contains(point_.X, point_.Y)) return true;
                }
                return base.GetHitTest(point);
            }
        }
 
        internal class TabControlActionList : DesignerActionList
        {
            #region Properties
 
            /// <summary>
            /// Gets the associated <see cref="TabControl"/>.
            /// </summary>
            public Tabs Control { get; private set; }
 
            /// <summary>
            /// Gets the designer host.
            /// </summary>
            public IDesignerHost DesignerHost { get; private set; }
 
            /// <summary>
            /// Gets the selection service.
            /// </summary>
            public ISelectionService SelectionService { get; private set; }
            #endregion
 
            #region Actions
 
            /// <summary>
            /// Gets or sets the ContentAlignment of the designed control.
            /// </summary>
            public System.Windows.Forms.TabAlignment Alignment
            {
                get { return Control.Alignment; }
                set { GetPropertyByName("Alignment").SetValue(Control, value); }
            }
 
            /// <summary>
            /// Adds a tab to the designed control.
            /// </summary>
            public void AddTab()
            {
                if (DesignerHost != null)
                {
                    TabPage tab = (TabPage)DesignerHost.CreateComponent(typeof(TabPage));
                    PropertyDescriptor nameProp = TypeDescriptor.GetProperties(tab)["Name"];
                    string name = (string)nameProp.GetValue(tab);
                    PropertyDescriptor textProp = TypeDescriptor.GetProperties(tab)["Text"];
                    textProp.SetValue(tab, name);
 
                    Control.Pages.Add(tab);
                    Control.SelectedIndex = Control.Pages.IndexOf(tab);
                }
            }
 
            /// <summary>
            /// Removes the current page of the designed control.
            /// </summary>
            protected void RemoveTab()
            {
                if (DesignerHost != null)
                {
                    if (Control.Pages.Count > 1)
                    {
                        int index = Control.SelectedIndex;
                        //DesignerHost.DestroyComponent(tab);
                        //if (index == Control.Tabs.Count)
                        //    index = Control.Tabs.Count - 1;
                        Control.SelectedIndex = index;
                    }
                }
            }
            #endregion
 
            #region Constructor
            /// <summary>
            /// Initializes a new instance of the <see cref="TabControlActionList"/> class.
            /// </summary>
            /// <param name="component">A component related to the DesignerActionList.</param>
            public TabControlActionList(IComponent component) : base(component)
            {
                Control = (Tabs)component;
                DesignerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
                SelectionService = (ISelectionService)GetService(typeof(ISelectionService));
            }
            #endregion
 
            #region Helper Methods
 
            /// <summary>
            /// Helper method to retrieve control properties for undo support.
            /// </summary>
            /// <param name="propName">Property name.</param>
            private PropertyDescriptor GetPropertyByName(string propName)
            {
                PropertyDescriptor prop;
                prop = TypeDescriptor.GetProperties(Control)[propName];
                if (prop == null)
                    throw new ArgumentException("Unknown property.", propName);
                else
                    return prop;
            }
            #endregion
 
            #region Overrides
            // <summary>
            /// Returns the collection of <see cref="T:System.ComponentModel.Design.DesignerActionItem"/> objects contained in the list.
            /// </summary>
            public override DesignerActionItemCollection GetSortedActionItems()
            {
                return new DesignerActionItemCollection() {
                    new DesignerActionHeaderItem("外观"),
                    new DesignerActionHeaderItem("数据"),
                    new DesignerActionPropertyItem("Alignment", "选项卡位置", "外观", "确定选项卡是否显示在控件的顶部、底部、左侧或右侧(在左侧或右侧将隐式地分为多行)。"),
                    new DesignerActionMethodItem(this, "AddTab", "添加选项卡", "数据", "向控件添加新选项卡"),
                    new DesignerActionMethodItem(this, "RemoveTab", "移除选项卡", "数据" ,"删除当前选项卡")
                };
            }
            #endregion
        }
    }
}