yangyin
2025-01-13 809cab44fa1cbea94d84c42cce94a35cc49ce5ad
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// 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.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Layout;
 
namespace AntdUI
{
    /// <summary>
    /// StackPanel 堆栈布局
    /// </summary>
    [Description("StackPanel 堆栈布局")]
    [ToolboxItem(true)]
    [DefaultProperty("Vertical")]
    [Designer(typeof(IControlDesigner))]
    public class StackPanel : IControl
    {
        bool autoscroll = false;
        /// <summary>
        /// 是否显示滚动条
        /// </summary>
        [Description("是否显示滚动条"), Category("外观"), DefaultValue(false)]
        public bool AutoScroll
        {
            get => autoscroll;
            set
            {
                if (autoscroll == value) return;
                autoscroll = value;
                if (autoscroll) ScrollBar = new ScrollBar(this);
                else ScrollBar = null;
                IOnSizeChanged();
            }
        }
 
        /// <summary>
        /// 滚动条
        /// </summary>
        [Browsable(false)]
        public ScrollBar? ScrollBar;
 
        public override Rectangle DisplayRectangle
        {
            get
            {
                var rect = ClientRectangle.DeflateRect(Padding);
                if (ScrollBar != null && ScrollBar.Show)
                {
                    if (ScrollBar.EnabledY) rect.Width -= ScrollBar.SIZE;
                    else rect.Height -= ScrollBar.SIZE;
                }
                return rect;
            }
        }
 
        /// <summary>
        /// 是否垂直方向
        /// </summary>
        [Description("是否垂直方向"), Category("外观"), DefaultValue(false)]
        public bool Vertical
        {
            get => layoutengine.Vertical;
            set
            {
                if (layoutengine.Vertical == value) return;
                layoutengine.Vertical = value;
                if (autoscroll) ScrollBar = new ScrollBar(this);
                else ScrollBar = null;
                IOnSizeChanged();
            }
        }
 
        /// <summary>
        /// 内容大小
        /// </summary>
        [Description("内容大小"), Category("外观"), DefaultValue(null)]
        public string? ItemSize
        {
            get => layoutengine.ItemSize;
            set
            {
                if (layoutengine.ItemSize == value) return;
                layoutengine.ItemSize = value;
                IOnSizeChanged();
            }
        }
 
        /// <summary>
        /// 间距
        /// </summary>
        [Description("间距"), Category("外观"), DefaultValue(0)]
        public int Gap
        {
            get => layoutengine.Gap;
            set
            {
                if (layoutengine.Gap == value) return;
                layoutengine.Gap = value;
                IOnSizeChanged();
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            ScrollBar?.Paint(e.Graphics.High());
            base.OnPaint(e);
        }
 
        #region 布局
 
        protected override void OnSizeChanged(EventArgs e)
        {
            var rect = ClientRectangle;
            ScrollBar?.SizeChange(rect);
            base.OnSizeChanged(e);
        }
 
        StackLayout layoutengine = new StackLayout();
        public override LayoutEngine LayoutEngine => layoutengine;
        internal class StackLayout : LayoutEngine
        {
            /// <summary>
            /// 是否垂直方向
            /// </summary>
            public bool Vertical { get; set; } = false;
            /// <summary>
            /// 内容大小
            /// </summary>
            public string? ItemSize { get; set; }
 
            /// <summary>
            /// 间距
            /// </summary>
            public int Gap { get; set; }
 
            public override bool Layout(object container, LayoutEventArgs layoutEventArgs)
            {
                if (container is StackPanel parent && parent.Controls.Count > 0)
                {
                    var controls = new List<Control>(parent.Controls.Count);
                    foreach (Control it in parent.Controls)
                    {
                        if (it.Visible) controls.Insert(0, it);
                    }
                    if (controls.Count > 0)
                    {
                        var rect = parent.DisplayRectangle;
                        int val = 0;
                        if (ItemSize == null || string.IsNullOrEmpty(ItemSize)) val = HandLayout(parent, controls, rect);
                        else
                        {
                            if (ItemSize.EndsWith("%") && float.TryParse(ItemSize.TrimEnd('%'), out var f)) val = HandLayout(parent, controls, rect, (int)Math.Round((Vertical ? rect.Height : rect.Width) * (f / 100F)));
                            else if (int.TryParse(ItemSize, out var i)) val = HandLayout(parent, controls, rect, (int)Math.Round(i * Config.Dpi));
                            else val = HandLayoutFill(controls, rect);
                        }
                        if (parent.ScrollBar != null)
                        {
                            bool old = parent.ScrollBar.Show;
                            parent.ScrollBar.SetVrSize(val);
                            if (old != parent.ScrollBar.Show) parent.BeginInvoke(new Action(parent.IOnSizeChanged));
                        }
                    }
                }
                return false;
            }
 
            int HandLayout(StackPanel parent, List<Control> controls, Rectangle rect)
            {
                int count = controls.Count;
                int offset = 0, use = 0, last_len = 0, gap = 0;
                if (parent.ScrollBar != null) offset = parent.ScrollBar.Value;
                if (Gap > 0 && count > 1) gap = (int)Math.Round(Gap * Config.Dpi);
                if (Vertical)
                {
                    foreach (var control in controls)
                    {
                        var point = rect.Location;
                        point.Offset(control.Margin.Left, -offset + control.Margin.Top + use);
                        control.Location = point;
                        control.Width = rect.Width - control.Margin.Horizontal;
 
                        use += control.Height + gap + control.Margin.Vertical;
                        last_len = point.Y + offset + control.Height;
                    }
                }
                else
                {
                    foreach (var control in controls)
                    {
                        Point point = rect.Location;
                        point.Offset(-offset + control.Margin.Left + use, control.Margin.Top);
                        control.Location = point;
                        control.Height = rect.Height - control.Margin.Vertical;
 
                        use += control.Width + gap + control.Margin.Horizontal;
                        last_len = control.Left + offset + control.Width;
                    }
                }
                return last_len;
            }
            int HandLayout(StackPanel parent, List<Control> controls, Rectangle rect, int size)
            {
                int count = controls.Count;
                int offset = 0, use = 0, last_len = 0, gap = 0;
                if (parent.ScrollBar != null) offset = parent.ScrollBar.Value;
                if (Gap > 0 && count > 1) gap = (int)Math.Round(Gap * Config.Dpi);
                if (Vertical)
                {
                    foreach (var control in controls)
                    {
                        var point = rect.Location;
                        point.Offset(control.Margin.Left, -offset + control.Margin.Top + use);
                        control.Location = point;
                        control.Size = new Size(rect.Width - control.Margin.Horizontal, size);
 
                        use += control.Height + gap + control.Margin.Vertical;
                        last_len = point.Y + offset + control.Height;
                    }
                }
                else
                {
                    foreach (var control in controls)
                    {
                        Point point = rect.Location;
                        point.Offset(-offset + control.Margin.Left + use, control.Margin.Top);
                        control.Location = point;
                        control.Size = new Size(size, rect.Height - control.Margin.Vertical);
 
                        use += control.Width + gap + control.Margin.Horizontal;
                        last_len = control.Left + offset + control.Width;
                    }
                }
                return last_len;
            }
            int HandLayoutFill(List<Control> controls, Rectangle rect)
            {
                int count = controls.Count;
                int usex = 0, usey = 0, gap = 0;
                if (Gap > 0 && count > 1) gap = (int)Math.Round(Gap * Config.Dpi);
                if (Vertical)
                {
                    int size = (rect.Height - (gap * (count - 1))) / count;
                    foreach (var control in controls)
                    {
                        Point point = rect.Location;
                        point.Offset(control.Margin.Left + usex, control.Margin.Top + usey);
                        control.Location = point;
                        control.Size = new Size(rect.Width - control.Margin.Horizontal, size - control.Margin.Vertical);
 
                        usey += size + gap;
                    }
                }
                else
                {
                    int size = (rect.Width - (gap * (count - 1))) / count;
                    foreach (var control in controls)
                    {
                        Point point = rect.Location;
                        point.Offset(control.Margin.Left + usex, control.Margin.Top + usey);
                        control.Location = point;
                        control.Size = new Size(size - control.Margin.Horizontal, rect.Height - control.Margin.Vertical);
 
                        usex += size + gap;
                    }
                }
                return 0;
            }
        }
 
        #endregion
 
        #region 鼠标
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (ScrollBar != null && ScrollBar.MouseDown(e.Location)) { OnTouchDown(e.X, e.Y); return; }
            base.OnMouseDown(e);
        }
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (ScrollBar != null && ScrollBar.MouseMove(e.Location) && OnTouchMove(e.X, e.Y)) return;
            base.OnMouseMove(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            ScrollBar?.MouseUp();
            OnTouchUp();
        }
 
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            ScrollBar?.Leave();
        }
 
        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            ScrollBar?.Leave();
        }
 
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            if (ScrollBar != null && ScrollBar.EnabledY) ScrollBar.MouseWheel(e.Delta);
            base.OnMouseWheel(e);
        }
        protected override bool OnTouchScrollX(int value)
        {
            if (ScrollBar != null && ScrollBar.EnabledX) return ScrollBar.MouseWheelX(value);
            return false;
        }
        protected override bool OnTouchScrollY(int value)
        {
            if (ScrollBar != null && ScrollBar.EnabledY) return ScrollBar.MouseWheelY(value);
            return false;
        }
 
        #endregion
 
        protected override void Dispose(bool disposing)
        {
            ScrollBar?.Dispose();
            base.Dispose(disposing);
        }
    }
}