yangyin
2024-10-25 1e65c49ba929103e79d87322ca1a3573c2b532e2
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace DPumpHydr.WinFrmUI.WenSkin.Forms
{
    public class LeftButtonForm : WenBottomInfoForm
    {
        public LeftButtonForm()
        {
            RefreshPadding();
            AddOffonButton();
            this.Name = "LeftButtonForm";
            this.Text = "LeftButtonForm";
 
            flow = new FlowLayoutPanel()
            {
                AutoSize = true,
                Location = new Point(2, this.TitleHeight + 50),
                Height = 0,
                Width = LeftWidth - 6,
                BackColor = Color.Transparent,
                FlowDirection = FlowDirection.TopDown,
                Anchor = AnchorStyles.Left | AnchorStyles.Top
            };
            flow.SizeChanged += (s, e) =>
            {
                foreach (WenSkin.Controls.WenButton button in flow.Controls)
                {
                    button.Width = flow.Width - 6;
                }
            };
            this.Controls.Add(flow);
 
            this.FrameColorChanged += (s, e) =>
            {
                if (FrameColor == Color.Transparent)
                    return;
 
                foreach (WenSkin.Controls.WenButton button in flow.Controls)
                {
                    Color color = this.FrameColor;
                    button.CustomForeColor = WenSkin.Controls.ControlHelper.ColorReverse(color);
                }
            };
        }
 
        #region 委托事件
 
        protected virtual void OnLeftMenuClick(LeftMenuItem item)
        {
            LeftMenuClick?.Invoke(this, new LeftMenuEventArgs(item));
        }
        public class LeftMenuEventArgs : EventArgs
        {
            public LeftMenuEventArgs(LeftMenuItem item)
            {
                Item = item;
            }
 
            public LeftMenuItem Item { get; set; }
        }
 
        #endregion
 
        #region 委托
 
        public delegate void LeftMenuItemEventHandler(object sender, LeftMenuEventArgs e);
 
        [Category("Wen"), Description("左侧按钮点击事件")]
 
        public event LeftMenuItemEventHandler LeftMenuClick;
 
        #endregion
 
        #region 私有属性
 
        private int leftWidth = 50;
        private FlowLayoutPanel flow;
        private int leftMaxWidth = 150;
        private int leftMinWidth = 50;
        private LeftMenuItemCollection leftMenu;
 
        #endregion
 
        #region 公有属性
        [Category("WenWidth"), Description("左边按钮区域尺寸"), DefaultValue(100)]
        [RefreshProperties(RefreshProperties.All)]
        public int LeftWidth { get => leftWidth; set { leftWidth = value; RefreshPadding(); } }
 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Category("Wen"), Description("左侧菜单列表")]
        [Editor("WenSkin.Design.Editor.WenCollectionEditor", typeof(System.Drawing.Design.UITypeEditor))]
        public LeftMenuItemCollection LeftMenu => leftMenu ?? (leftMenu = new LeftMenuItemCollection(this));
 
        [Category("WenWidth"), Description("左边展开最大尺寸"), DefaultValue(150)]
        public int LeftMaxWidth { get => leftMaxWidth; set => leftMaxWidth = value; }
 
        [Category("WenWidth"), Description("左边展开最小尺寸"), DefaultValue(50)]
        public int LeftMinWidth { get => leftMinWidth; set => leftMinWidth = value; }
 
        [DefaultValue(typeof(Padding), "30,50,0,30"), Category("布局"), Description("指定控件内部间距")]
        public new Padding Padding { get => base.Padding; set => base.Padding = value; }
        #endregion
 
        #region 重绘
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawLeft(e.Graphics);
        }
 
        private void DrawLeft(Graphics g)
        {
            Brush b = new SolidBrush(FrameColor);
            g.FillRectangle(b, 0, this.TitleHeight, this.LeftWidth, this.Height - TitleHeight - BottomWidth);
        }
 
        #endregion
 
        //添加按钮
        private void AddOffonButton()
        {
            WenSkin.Controls.WenButtonTriangle wenButton = new Controls.WenButtonTriangle()
            {
                Location = new Point(this.LeftWidth - 24, this.TitleHeight + 4),
                BackColor = Color.Transparent,
                State = true,
                ButtonColor = Color.FromArgb(199, 199, 199),
                OpenStyle = WenSkin.Controls.WenButtonTriangleStyle.Right,
                CloseStyle = WenSkin.Controls.WenButtonTriangleStyle.Left,
                Size = new Size(16, 16),
            };
 
            wenButton.Click += (s, e) =>
            {
                if (wenButton.State)
                {
                    LeftWidth = LeftMinWidth;
                    flow.Width = LeftWidth - 6;
                    flow.Location = new Point(3, this.TitleHeight + 50);
                }
                else
                {
                    LeftWidth = LeftMaxWidth;
                    flow.Width = LeftWidth - 6;
                    flow.Location = new Point(3, this.TitleHeight + 50);
                }
                wenButton.Location = new Point(this.LeftWidth - 24, this.TitleHeight + 4);
            };
 
            this.Controls.Add(wenButton);
 
            //尝试添加一个按钮
        }
        protected override void RefreshPadding()
        {
            this.Padding = new Padding() { Top = TitleHeight, Left = LeftWidth, Bottom = BottomWidth, Right = FrameWidth };
            this.Invalidate();
        }
 
        public class LeftMenuItemCollection : DPumpHydr.WinFrmUI.WenSkin.Controls.WenCollection
        {
            private LeftButtonForm owner;
            public LeftMenuItemCollection(LeftButtonForm owner)
            {
                this.owner = owner;
            }
 
            public LeftMenuItem this[int index] => Items[index] as LeftMenuItem;
 
            public LeftMenuItem this[string key] => Items.ToArray().ToList().Find(a => (a as LeftMenuItem).Name == key) as LeftMenuItem;
 
            public int Add(LeftMenuItem value)
            {
                Controls.WenButton button = new Controls.WenButton()
                {
                    Name = value.Name,
                    Text = value.Text,
                    Image = value.Image,
                    ImageSize = new Size(32, 32),
                    Tag = value,
                    Height = 38,
                    BackColor = Color.Transparent,
                    Width = owner.LeftWidth - 12,
                    CustomForeColor = value.ForeColor,
                };
                button.Click += (s, ex) =>
                {
                    owner.OnLeftMenuClick(value);
                };
                value.WenImageButton = button;
 
                owner.flow.Controls.Add(button);
                owner.flow.Invalidate();
 
                return Items.Add(value);
            }
 
            public int Add(string text)
            {
                return (Add(new LeftMenuItem(text)));
            }
            public override int Add(object value)
            {
                return Add(value as LeftMenuItem);
            }
 
            public void AddRange(LeftMenuItem[] items)
            {
                foreach (var item in items)
                {
                    Add(item);
                }
            }
            public void Remove(LeftMenuItem value)
            {
                base.Remove(value);
                foreach (WenSkin.Controls.WenButton button in owner.flow.Controls)
                {
                    if (button.Tag is LeftMenuItem leftMenuItem)
                    {
                        if (leftMenuItem == value)
                            owner.flow.Controls.Remove(button);
                    }
                }
            }
 
            public override void RemoveAt(int index)
            {
                base.RemoveAt(index);
                owner.flow.Controls.RemoveAt(index);
            }
 
            public override void Remove(object value)
            {
                base.Remove(value);
            }
            public override void Clear()
            {
                base.Clear();
                owner.flow.Controls.Clear();
            }
        }
 
        public class LeftMenuItem
        {
            public LeftMenuItem()
            {
 
            }
 
            public LeftMenuItem(string text)
            {
                Text = text;
            }
 
            #region 私有属性
 
            private string name = "MenuItem";
            private Image image = null;
            private Color foreColor = Color.FromArgb(199, 199, 199);
            private Color backColor = Color.Transparent;
            private string text = "菜单";
            private bool visible = true;
 
            #endregion
 
            public Controls.WenButton WenImageButton { get; set; }
 
            [Category("Wen"), Description("按钮图标"), DefaultValue("MenuItem")]
            public string Name { get => name; set { name = value; if (WenImageButton != null) WenImageButton.Name = value; } }
            [Category("Wen"), Description("按钮图标"), DefaultValue("菜单")]
            public string Text { get => text; set { text = value; if (WenImageButton != null) WenImageButton.Text = value; } }
            [Category("Wen"), Description("按钮图标"), DefaultValue(null)]
            public Image Image { get => image; set { image = value; if (WenImageButton != null) WenImageButton.Image = value; } }
            [DefaultValue(typeof(Color), "Transparent"), Category("WenColor"), Description("背景色")]
            public Color BackColor { get => backColor; set { backColor = value; if (WenImageButton != null) WenImageButton.BackColor = value; } }
            [DefaultValue(typeof(Color), "199, 199, 199"), Category("WenColor"), Description("前景色")]
            public Color ForeColor { get => foreColor; set { foreColor = value; if (WenImageButton != null) WenImageButton.ForeColor = value; } }
 
            [DefaultValue(true), Category("Wen"), Description("获取或设置一个值,该值指示是否显示该控件及其所有子控件")]
            public bool Visible { get => visible; set { visible = value; if (WenImageButton != null) WenImageButton.Visible = value; } }
 
            public override string ToString()
            {
                return $"{Name}[{Text}]";
            }
        }
    }
}