tangxu
2024-12-29 72e75456f8b30ec5b6f355539d9c883b0f810d21
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
 
namespace Microsoft.Windows.Forms
{
    partial class UIControl
    {
        /// <summary>
        /// 虚拟控件集合
        /// </summary>
        public class UIControlCollection : Collection<UIControl>, IDisposable
        {
            //集合所属控件
            private IUIControl m_Owner;
 
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="owner">所属控件</param>
            internal UIControlCollection(IUIControl owner)
            {
                if (owner == null)
                    throw new ArgumentNullException("owner");
                this.m_Owner = owner;
            }
 
            /// <summary>
            /// 析构函数
            /// </summary>
            ~UIControlCollection()
            {
                this.Dispose(false);
            }
 
            /// <summary>
            /// 插入控件
            /// </summary>
            /// <param name="index">索引,值越小 Z 轴顺序越靠后</param>
            /// <param name="control">控件</param>
            protected override void InsertItem(int index, UIControl control)
            {
                if (control == null)
                    throw new ArgumentNullException("control");
 
                if (control.UIParentInternal == this.m_Owner)
                {
                    control.BringToFront();
                }
                else
                {
                    if (control.UIParentInternal != null)
                        control.UIParentInternal.UIControls.Remove(control);
                    this.m_Owner.SuspendLayout();
                    try
                    {
                        base.InsertItem(index, control);
                        control.UIParentInternal = this.m_Owner;
                    }
                    finally
                    {
                        this.m_Owner.ResumeLayout();
                    }
                }
            }
 
            /// <summary>
            /// 移除控件
            /// </summary>
            /// <param name="index">索引</param>
            protected override void RemoveItem(int index)
            {
                UIControl control = this[index];
                this.m_Owner.SuspendLayout();
                try
                {
                    base.RemoveItem(index);
                    control.UIParentInternal = null;
                }
                finally
                {
                    this.m_Owner.ResumeLayout();
                }
            }
 
            /// <summary>
            /// 不支持的操作
            /// </summary>
            /// <param name="index">索引</param>
            /// <param name="control">控件</param>
            protected override void SetItem(int index, UIControl control)
            {
                throw new NotSupportedException("不支持的操作");
            }
 
            /// <summary>
            /// 清空所有控件
            /// </summary>
            protected override void ClearItems()
            {
                this.m_Owner.SuspendLayout();
                try
                {
                    foreach (UIControl control in this)
                        control.UIParentInternal = null;
                    base.ClearItems();
                }
                finally
                {
                    this.m_Owner.ResumeLayout();
                }
            }
 
            /// <summary>
            /// 批量添加控件
            /// </summary>
            /// <param name="controls">控件集合</param>
            public void AddRange(IEnumerable<UIControl> controls)
            {
                this.m_Owner.SuspendLayout();
                try
                {
                    foreach (UIControl control in controls)
                        this.Add(control);
                }
                finally
                {
                    this.m_Owner.ResumeLayout();
                }
            }
 
            /// <summary>
            /// 释放资源
            /// </summary>
            /// <param name="disposing">释放托管资源为 true,否则为 false</param>
            private void Dispose(bool disposing)
            {
                if (this.m_Owner != null)
                {
                    if (this.Count > 0)
                    {
                        this.m_Owner.SuspendLayout();
                        try
                        {
                            UIControl[] array = new UIControl[this.Count];
                            this.CopyTo(array, 0);
                            foreach (UIControl control in array)
                                control.Dispose();
                            base.ClearItems();
                        }
                        finally
                        {
                            this.m_Owner.ResumeLayout();
                        }
                    }
                    this.m_Owner = null;
                }
            }
 
            /// <summary>
            /// 释放资源
            /// </summary>
            public void Dispose()
            {
                this.Dispose(true);
                GC.SuppressFinalize(this);
            }
        }
    }
}