tangxu
2025-01-13 4f7cb65b079d88d5a829688b24d26d5145c5df47
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Drawing;
 
namespace Microsoft.Windows.Forms
{
    partial class UIControl
    {
        private Sprite m_Sprite;
        /// <summary>
        /// 获取精灵
        /// </summary>
        protected virtual Sprite Sprite
        {
            get
            {
                this.CheckDisposed();
                if (this.m_Sprite == null)
                    this.m_Sprite = new Sprite(this);
                return this.m_Sprite;
            }
        }
 
        private int m_UpdateSuspendCount;
        /// <summary>
        /// 获取刷新操作是否被挂起
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool UpdateSuspended
        {
            get
            {
                return this.m_UpdateSuspendCount != 0 || (this.m_UIParent != null && this.m_UIParent.UpdateSuspended);
            }
        }
 
        /// <summary>
        /// 渲染控件
        /// </summary>
        /// <param name="e">数据</param>
        protected virtual void RenderSelf(PaintEventArgs e)
        {
            //准备
            Graphics g = e.Graphics;
            Rectangle rect = this.ClientRectangle;
            //渲染
            this.Sprite.BackColor = this.BackColor;
            this.Sprite.BeginRender(g);
            this.Sprite.RenderBackColor(rect);
            this.Sprite.RenderBorder(rect);
            this.Sprite.EndRender();
        }
 
        /// <summary>
        /// 渲染子控件
        /// </summary>
        /// <param name="e">数据</param>
        protected virtual void RenderChildren(PaintEventArgs e)
        {
            foreach (IUIControl control in this.UIControls)
            {
                if (control.Visible)
                    using (new TranslateGraphics(e.Graphics, control.Location))
                        control.RenderCore(e);
            }
        }
 
        /// <summary>
        /// 渲染控件和子控件
        /// </summary>
        /// <param name="e">数据</param>
        void IUIControl.RenderCore(PaintEventArgs e)
        {
            this.RenderSelf(e);
            this.RenderChildren(e);
            this.RaisePaintEvent(this, e);
        }
 
        /// <summary>
        /// 挂起刷新 UI
        /// </summary>
        public void BeginUpdate()
        {
            this.m_UpdateSuspendCount++;
        }
 
        /// <summary>
        /// 恢复刷新 UI
        /// </summary>
        public void EndUpdate()
        {
            this.EndUpdate(false);
        }
 
        /// <summary>
        /// 恢复刷新 UI,可以选择强制刷新
        /// </summary>
        /// <param name="forceUpdate">若要执行刷新为 true,否则为 false</param>
        public void EndUpdate(bool forceUpdate)
        {
            this.m_UpdateSuspendCount--;
            this.InvalidateCore(this.ClientRectangle, false, forceUpdate);
        }
 
        /// <summary>
        /// 使控件工作区无效
        /// </summary>
        public void Invalidate()
        {
            this.InvalidateCore(this.ClientRectangle, false, false);
        }
 
        /// <summary>
        /// 使控件工作区无效
        /// </summary>
        /// <param name="invalidateChildren">使控件所在的 Win32 窗口的子控件无效为 true,否则为 false</param>
        public void Invalidate(bool invalidateChildren)
        {
            this.InvalidateCore(this.ClientRectangle, invalidateChildren, false);
        }
 
        /// <summary>
        /// 使控件矩形无效
        /// </summary>
        /// <param name="rc">无效矩形</param>
        public void Invalidate(Rectangle rc)
        {
            this.InvalidateCore(rc, false, false);
        }
 
        /// <summary>
        /// 使控件矩形无效
        /// </summary>
        /// <param name="rc">无效矩形</param>
        /// <param name="invalidateChildren">使控件所在的 Win32 窗口的子控件无效为 true,否则为 false</param>
        public void Invalidate(Rectangle rc, bool invalidateChildren)
        {
            this.InvalidateCore(rc, invalidateChildren, false);
        }
 
        /// <summary>
        /// 使控件矩形无效,可以选择是否强制刷新
        /// </summary>
        /// <param name="rc">无效矩形</param>
        /// <param name="invalidateChildren">使控件所在的 Win32 窗口的子控件无效为 true,否则为 false</param>
        /// <param name="forceUpdate">强制刷新为 true,否则为false</param>
        protected void InvalidateCore(Rectangle rc, bool invalidateChildren, bool forceUpdate)
        {
            if (forceUpdate || !this.UpdateSuspended)
            {
                IUIControl parent = this;
                IUIWindow window = null;
                while (parent != null && (window = parent as IUIWindow) == null)
                {
                    rc.Offset(parent.Left, parent.Top);
                    parent = parent.UIParent;
                }
                if (window == null)
                    return;
                window.Invalidate(rc, invalidateChildren);
            }
        }
 
        /// <summary>
        /// 重绘所在 Win32 窗口的无效区域
        /// </summary>
        public void Update()
        {
            this.UpdateCore(false);
        }
 
        /// <summary>
        /// 重绘所在 Win32 窗口的无效区域,可以选择是否强制刷新
        /// </summary>
        /// <param name="forceUpdate">强制刷新为 true,否则为false</param>
        protected void UpdateCore(bool forceUpdate)
        {
            if (forceUpdate || !this.UpdateSuspended)
            {
                IUIControl parent = this;
                IUIWindow window = null;
                while (parent != null && (window = parent as IUIWindow) == null)
                    parent = parent.UIParent;
                if (window == null)
                    return;
                window.Update();
            }
        }
 
        /// <summary>
        /// 立即刷新所在 Win32 窗口和其子控件
        /// </summary>
        public void Refresh()
        {
            this.RefreshCore(false);
        }
 
        /// <summary>
        /// 立即刷新所在 Win32 窗口和其子控件,可以选择是否强制刷新
        /// </summary>
        /// <param name="forceUpdate">强制刷新为 true,否则为false</param>
        protected void RefreshCore(bool forceUpdate)
        {
            if (forceUpdate || !this.UpdateSuspended)
            {
                IUIControl parent = this;
                IUIWindow window = null;
                Rectangle rc = this.ClientRectangle;
                while (parent != null && (window = parent as IUIWindow) == null)
                {
                    rc.Offset(parent.Left, parent.Top);
                    parent = parent.UIParent;
                }
                if (window == null)
                    return;
                window.Invalidate(rc, true);
                window.Update();
            }
        }
    }
}