yangyin
2025-02-28 baa80d650adebcce70f1113cc1020c6039c159a0
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using DPumpHydr.WinFrmUI.WenSkin.Controls;
using static DPumpHydr.WinFrmUI.WenSkin.WenJsonConfig.JsonConfig;
 
namespace DPumpHydr.WinFrmUI.WenSkin.WenJsonConfig
{
    public partial class WenConfigForm : DPumpHydr.WinFrmUI.WenSkin.Forms.WenForm
    {
        public WenConfigForm() : this("WenAutoConfig")
        {
        }
        public WenConfigForm(string jsonfilePath)
        {
            InitializeComponent();
            ControlAdd();
            Text += " - " + jsonfilePath;
            config = new JsonConfig(jsonfilePath);
 
            ConfigAdd();
        }
        
        public WenConfigForm(JsonConfig jsonConfig)
        {
            InitializeComponent();
            ControlAdd();
            Text += " - " + jsonConfig.JsonFileName;
            config = jsonConfig;
 
            ConfigAdd();
        }
        private readonly JsonConfig config;
 
        #region 公有属性
 
        public string SaveMessage { get; set; } = "保存成功!部分设置需要重启软件才能生效!";
 
        #endregion
 
        #region 委托事件
 
        [Category("Wen"), Description("按钮点击事件")]
        public event JsonConfigClickEventHandler JsonConfigItemClick;
 
        [Category("Wen"), Description("移除一项")]
        public event JsonConfigItemEventHandler JsonConfigItemRemove;
 
        [Category("Wen"), Description("新增一项")]
        public event JsonConfigItemEventHandler JsonConfigItemAdd;
 
        [Category("Wen"), Description("值变化")]
        public event JsonConfigItemValueEventHandler JsonConfigItemValueChanged;
        
        [Category("Wen"), Description("保存信息")]
        public event JsonConfigEventHandler JsonConfigSaveChanged;
 
 
 
        [ComVisible(true)]
        public delegate void JsonConfigClickEventHandler(object sender, JsonConfigItemClickEventArgs e);
        public class JsonConfigItemClickEventArgs : JsonConfigItemEventArgs
        {
            public JsonConfigItemClickEventArgs(bool showTextBox, JsonConfigGroup configXmlGroup, JsonConfigItem configXmlItem) : base(configXmlGroup, configXmlItem)
            {
                ShowTextBox = showTextBox;
                ConfigXmlGroup = configXmlGroup;
                ConfigXmlItem = configXmlItem;
            }
 
            public bool ShowTextBox { get; set; }
        }
 
        [ComVisible(true)]
        public delegate void JsonConfigItemValueEventHandler(object sender, JsonConfigItemValueEventArgs e);
        public class JsonConfigItemValueEventArgs : JsonConfigItemEventArgs
        {
            public JsonConfigItemValueEventArgs(JsonConfigGroup configXmlGroup, JsonConfigItem configXmlItem, string changeValue) : base(configXmlGroup, configXmlItem)
            {
                ChangeValue = changeValue;
                State = true;
            }
            public string ChangeValue { get; set; }
            public bool State { get; set; }
        }
 
        [ComVisible(true)]
        public delegate void JsonConfigItemEventHandler(object sender, JsonConfigItemEventArgs e);
        public class JsonConfigItemEventArgs : EventArgs
        {
            public JsonConfigItemEventArgs(JsonConfigGroup configXmlGroup, JsonConfigItem configXmlItem)
            {
                ConfigXmlGroup = configXmlGroup;
                ConfigXmlItem = configXmlItem;
            }
            public JsonConfigGroup ConfigXmlGroup { get; set; }
            public JsonConfigItem ConfigXmlItem { get; set; }
        }              
        
        [ComVisible(true)]
        public delegate void JsonConfigEventHandler(object sender, JsonConfigEventArgs e);
        public class JsonConfigEventArgs : EventArgs
        {
            private readonly JsonConfig configXmlParsing;
 
            public JsonConfigEventArgs(JsonConfig configXmlParsing)
            {
                this.configXmlParsing = configXmlParsing;
            }
 
            public JsonConfig ConfigXmlParsing => configXmlParsing;
        }
        #endregion
 
        private void ControlAdd()
        {
            WenButton WenImageButton = new WenButton()
            {
                Text = "保存",
                Dock = DockStyle.Bottom,
            };
            WenImageButton.Click += WenImageButton_Click;
            this.Controls.Add(WenImageButton);
        }
 
        private void WenImageButton_Click(object sender, EventArgs e)
        {
            SaveXmlConfig();
            MsgBoxInformation(SaveMessage);
        }
        /// <summary>
        /// 保存设置
        /// </summary>
        public void SaveXmlConfig()
        {
            config.Save();
            JsonConfigSaveChanged?.Invoke(this, new JsonConfigEventArgs(this.config));
        }
 
        private void ConfigAdd()
        {
            flowLayoutPanel1.Controls.Clear();
 
            ControlsAdd(flowLayoutPanel1,config.Groups);
 
 
            void ControlsAdd(Control owner,JsonConfigGroupList groups)
            {
                foreach (var gr in groups)
                {
                    GroupBox groupBox = new GroupBox()
                    {
                        Name = gr.Name,
                        Text = gr.Info,
                        AutoSize = true,
                        BackColor = Color.Transparent,
                        ForeColor = WenSkin.Controls.ControlHelper.ColorReverse(this.BackColor),
                        Visible = gr.Visible,
                    };
                    FlowLayoutPanel flow = new FlowLayoutPanel()
                    {
                        Dock = DockStyle.Fill,
                        Size = new Size(450, 0),
                        AutoSize = true,
                        FlowDirection = FlowDirection.TopDown
                    };
                    groupBox.Controls.Add(flow);
                    foreach (var item in gr.Items)
                    {
                        JsonConfigItemControl configXmlItemControl = new JsonConfigItemControl(gr, item);
                        configXmlItemControl.JsonConfigItemClick += (s, e) =>
                        {
                            //事件准发即可
                            JsonConfigItemClick?.Invoke(this, e);
                        };
                        configXmlItemControl.JsonConfigItemRemove += (s, e) =>
                        {
                            //移除内容界面需要更改,做处理
                            JsonConfigItemRemove?.Invoke(this, e);
                            gr.Items.Remove(item);
                            config.Save();
                            ConfigAdd();
                        };
                        configXmlItemControl.JsonConfigItemValueChanged += (s, e) =>
                        {
                            JsonConfigItemValueChanged?.Invoke(this, e);
                        };
                        flow.Controls.Add(configXmlItemControl);
                    }
                    //添加按钮
                    if (gr.AllowAdd)
                    {
                        flow.Controls.Add(AllowAddButton(gr));
                    }
                    if (gr.Groups.Count > 0)
                        ControlsAdd(flow, gr.Groups);
 
                    owner.Controls.Add(groupBox);
                }
            }
 
            
        }
 
        #region 增添一个添加一项按钮
        private WenButton AllowAddButton(JsonConfigGroup group)
        {
            WenButton button = new WenButton()
            {
                Text = "新增一项",
                Size = new Size(500, 26)
            };
            button.Click += (s, e) =>
            {
                JsonConfigItem configXmlItem = new JsonConfigItem(group.Items)
                {
                    Name = $"{group.Name}{group.Items.Count + 1}",
                    Info = "自动添加",
                    Ecc = false,
                    ReadOnly = false,
                    Delete = true,
                    Value = "",
                };
 
                WenSkin.Forms.WenForm form = new Forms.WenForm()
                {
                    StartPosition = FormStartPosition.CenterScreen,
                    Size = new Size(300, 100),
                    Text = "请输入名称"
                };
 
                TextBox textBox = new TextBox()
                {
                    Text = "新增名称",
                    Dock = DockStyle.Fill,
                    //Multiline = true,
                    ScrollBars = ScrollBars.Both,
                    WordWrap = false
                };
 
                WenButton button1 = new WenButton()
                {
                    Text = "确定",
                    Dock = DockStyle.Bottom,
                };
                button1.Click += (sx, ex) =>
                {
                    JsonConfigItemAdd?.Invoke(this, new JsonConfigItemEventArgs(group, configXmlItem));
                    configXmlItem.Info = textBox.Text;
                    group.Items.Add(configXmlItem);
                    config.Save();
                    form.Dispose();
                    ConfigAdd();
                };
 
                form.Controls.Add(textBox);
                form.Controls.Add(button1);
                form.ShowDialog();
            };
            return button;
        }
 
        #endregion
 
        #region 单项样式
 
        [ToolboxItem(false)]
        private class JsonConfigItemControl : WenControl
        {
            public JsonConfigItemControl(JsonConfigGroup gr, JsonConfigItem item) : base()
            {
                this.Visible = item.Visible;
 
                if (item.ComboBox)
                {
                    WenLableComboBox comboBox = new WenLableComboBox()
                    {
                        Dock = DockStyle.Fill,
                        Name = item.Name,
                        TextLable = item.Info,
                        DropDownStyle = item.ReadOnly ? ComboBoxStyle.DropDownList : ComboBoxStyle.DropDown,
                    };
                    comboBox.Items.AddRange(item.Items.ToArray());
                    comboBox.Text = item.Value?.ToString();
 
                    if (item.Tip != null && item.Tip.Length != 0)
                    {
                        foreach (Control c in comboBox.Controls)
                        {
                            tip.SetToolTip(c, item.Tip);
                        }
                    }
 
                    comboBox.TextChanged += (s, e) =>
                    {
                        //值改变响应事件
                        JsonConfigItemValueEventArgs configXmlItemValueEventArgs = new JsonConfigItemValueEventArgs(gr, item, comboBox.Text);
                        JsonConfigItemValueChanged?.Invoke(this, configXmlItemValueEventArgs);
 
                        if (configXmlItemValueEventArgs.State)
                            item.Value = comboBox.Text;
                    };
 
                    comboBox.Size = new Size(450, comboBox.Height);
                    this.Controls.Add(comboBox);
 
                    //移除项目 按钮
                    if (gr.AllowAdd && item.Delete)
                    {
                        WenButton button = new WenButton()
                        {
                            Image = Properties.Resources.close,
                            ImageSize = new Size(16, 16),
                            Dock = DockStyle.Right,
                            Size = new Size(20, 20)
                        };
                        button.Click += (s, e) =>
                        {
                            if (this.MsgBoxAsterisk("删除不可逆,是否删除"))
                            {
                                JsonConfigItemRemove?.Invoke(this, new JsonConfigItemEventArgs(gr, item));
                            }
                        };
                        this.Controls.Add(button);
                    }
                    this.Size = new Size(500, comboBox.Height + 3);
                }
                else
                {
                    WenLableTextBox textBox = new WenLableTextBox()
                    {
                        Dock = DockStyle.Fill,
                        Name = item.Name,
                        TextLable = item.Info,
                        Text = item.Value?.ToString(),
                        Password = item.Ecc,
                        ReadOnly = item.ReadOnly,
                    };
                    //按钮点击事件
                    textBox.ButtonClick += (s, e) =>
                    {
                        JsonConfigItemClickEventArgs ex = new JsonConfigItemClickEventArgs(e.ShowTextBox, gr, item);
                        JsonConfigItemClick?.Invoke(this, ex);
                        e.ShowTextBox = ex.ShowTextBox;
                    };
                    if (item.Tip != null && item.Tip.Length != 0)
                    {
                        foreach (Control c in textBox.Controls)
                        {
                            tip.SetToolTip(c, item.Tip);
                        }
                    }
 
                    textBox.Size = new Size(450, textBox.Height);
                    textBox.TextBeforeChanged += (s, e) =>
                    {
                        //值改变响应事件
                        JsonConfigItemValueEventArgs configXmlItemValueEventArgs = new JsonConfigItemValueEventArgs(gr, item, textBox.Text);
 
                        JsonConfigItemValueChanged?.Invoke(this, configXmlItemValueEventArgs);
 
                        e.State = configXmlItemValueEventArgs.State;
                        if (configXmlItemValueEventArgs.State)
                            item.Value = textBox.Text;
                    };
                    this.Controls.Add(textBox);
 
                    //移除项目 按钮
                    if (gr.AllowAdd && item.Delete)
                    {
                        WenButton button = new WenButton()
                        {
                            Image = Properties.Resources.close,
                            ImageSize = new Size(16, 16),
                            Dock = DockStyle.Right,
                            Size = new Size(20, 20)
                        };
                        button.Click += (s, e) =>
                        {
                            if (this.MsgBoxAsterisk("删除不可逆,是否删除"))
                            {
                                JsonConfigItemRemove?.Invoke(this, new JsonConfigItemEventArgs(gr, item));
                            }
                        };
                        this.Controls.Add(button);
                    }
                    this.Size = new Size(500, textBox.Height);
                }
            }
 
            #region 私有属性
 
            private readonly ToolTip tip = new ToolTip();
 
            #endregion
 
            #region 委托事件
 
            [Category("Wen"), Description("按钮点击事件")]
            public event JsonConfigClickEventHandler JsonConfigItemClick;
 
            [Category("Wen"), Description("移除一项")]
            public event JsonConfigItemEventHandler JsonConfigItemRemove;
 
            [Category("Wen"), Description("值变化")]
            public event JsonConfigItemValueEventHandler JsonConfigItemValueChanged;
 
            #endregion
        }
        #endregion
    }
}