|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Drawing; |
| 4 | +using System.Windows.Forms; |
| 5 | + |
| 6 | +namespace QpTestClient.Controls |
| 7 | +{ |
| 8 | + public partial class AotPropertyGrid : UserControl |
| 9 | + { |
| 10 | + private List<Label> propertyLabelList = new List<Label>(); |
| 11 | + |
| 12 | + public AotPropertyGrid() |
| 13 | + { |
| 14 | + InitializeComponent(); |
| 15 | + } |
| 16 | + |
| 17 | + public void RegisterGroup(string groupName) |
| 18 | + { |
| 19 | + var groupLabel = new Label(); |
| 20 | + groupLabel.BackColor = System.Drawing.SystemColors.GradientInactiveCaption; |
| 21 | + groupLabel.Dock = DockStyle.Top; |
| 22 | + groupLabel.Margin = new Padding(0); |
| 23 | + groupLabel.BorderStyle = BorderStyle.FixedSingle; |
| 24 | + groupLabel.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, System.Drawing.FontStyle.Bold); |
| 25 | + groupLabel.ForeColor = System.Drawing.SystemColors.ActiveCaptionText; |
| 26 | + groupLabel.Location = new System.Drawing.Point(0, 0); |
| 27 | + groupLabel.Name = "label1"; |
| 28 | + groupLabel.Size = new System.Drawing.Size(878, 40); |
| 29 | + groupLabel.TabIndex = 1; |
| 30 | + groupLabel.Text = $"∇ {groupName}"; |
| 31 | + groupLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; |
| 32 | + var collapse = false; |
| 33 | + groupLabel.Click += (_, _) => |
| 34 | + { |
| 35 | + collapse = !collapse; |
| 36 | + if (collapse) |
| 37 | + groupLabel.Text = $"▷ {groupName}"; |
| 38 | + else |
| 39 | + groupLabel.Text = $"∇ {groupName}"; |
| 40 | + |
| 41 | + var enterGroup = false; |
| 42 | + foreach (Control control in flp.Controls) |
| 43 | + { |
| 44 | + if (enterGroup) |
| 45 | + { |
| 46 | + if (control is Label) |
| 47 | + break; |
| 48 | + control.Visible = !collapse; |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + if (control == groupLabel) |
| 53 | + enterGroup = true; |
| 54 | + } |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + |
| 59 | + flp.Controls.Add(groupLabel); |
| 60 | + } |
| 61 | + |
| 62 | + private Label createPropertyLabel(string propertyName, string propertyDescription) |
| 63 | + { |
| 64 | + var label = new Label(); |
| 65 | + label.BackColor = System.Drawing.SystemColors.ControlLightLight; |
| 66 | + label.BorderStyle = BorderStyle.FixedSingle; |
| 67 | + label.Dock = DockStyle.Left; |
| 68 | + label.Location = new System.Drawing.Point(0, 0); |
| 69 | + label.Margin = new Padding(0); |
| 70 | + label.MinimumSize = new System.Drawing.Size(180, 30); |
| 71 | + label.Size = new System.Drawing.Size(180, 38); |
| 72 | + label.TabIndex = 0; |
| 73 | + label.Text = propertyName; |
| 74 | + label.Click += (_, _) => |
| 75 | + { |
| 76 | + lblPropertyName.Text = propertyName; |
| 77 | + lblPropertyDescription.Text = propertyDescription; |
| 78 | + }; |
| 79 | + return label; |
| 80 | + } |
| 81 | + |
| 82 | + private TextBox createPropertyTextBox(string propertyName, string propertyDescription) |
| 83 | + { |
| 84 | + var textBox = new TextBox(); |
| 85 | + textBox.BorderStyle = BorderStyle.FixedSingle; |
| 86 | + textBox.Dock = DockStyle.Fill; |
| 87 | + textBox.Location = new Point(180, 0); |
| 88 | + textBox.Margin = new Padding(0); |
| 89 | + textBox.Size = new Size(698, 38); |
| 90 | + textBox.GotFocus += (_, _) => |
| 91 | + { |
| 92 | + lblPropertyName.Text = propertyName; |
| 93 | + lblPropertyDescription.Text = propertyDescription; |
| 94 | + }; |
| 95 | + return textBox; |
| 96 | + } |
| 97 | + |
| 98 | + private CheckBox createPropertyCheckBox(string propertyName, string propertyDescription) |
| 99 | + { |
| 100 | + var checkBox=new CheckBox(); |
| 101 | + checkBox.BackColor = System.Drawing.SystemColors.ControlLightLight; |
| 102 | + checkBox.Dock = System.Windows.Forms.DockStyle.Fill; |
| 103 | + checkBox.Location = new System.Drawing.Point(0, 0); |
| 104 | + checkBox.Padding = new System.Windows.Forms.Padding(10, 0, 0, 0); |
| 105 | + checkBox.Size = new System.Drawing.Size(696, 36); |
| 106 | + checkBox.UseVisualStyleBackColor = false; |
| 107 | + return checkBox; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + private ComboBox createPropertyComboBox(string propertyName, string propertyDescription) |
| 112 | + { |
| 113 | + var comboBox=new ComboBox(); |
| 114 | + comboBox.Dock = System.Windows.Forms.DockStyle.Fill; |
| 115 | + comboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; |
| 116 | + comboBox.FlatStyle = System.Windows.Forms.FlatStyle.Flat; |
| 117 | + comboBox.FormattingEnabled = true; |
| 118 | + comboBox.Location = new System.Drawing.Point(0, 0); |
| 119 | + comboBox.Size = new System.Drawing.Size(696, 39); |
| 120 | + return comboBox; |
| 121 | + } |
| 122 | + |
| 123 | + private void addPropertyControl(Label label1, Control control1) |
| 124 | + { |
| 125 | + propertyLabelList.Add(label1); |
| 126 | + LinkControl(label1, control1); |
| 127 | + |
| 128 | + var panel1 = new Panel(); |
| 129 | + panel1.Margin = new Padding(0); |
| 130 | + panel1.Controls.Add(control1); |
| 131 | + panel1.Controls.Add(label1); |
| 132 | + panel1.Padding = new Padding(0); |
| 133 | + panel1.Dock = DockStyle.Top; |
| 134 | + panel1.Location = new System.Drawing.Point(0, 40); |
| 135 | + panel1.Size = new System.Drawing.Size(878, 38); |
| 136 | + |
| 137 | + flp.Controls.Add(panel1); |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + private void ActiveLabel(Label label) |
| 142 | + { |
| 143 | + foreach (var item in propertyLabelList) |
| 144 | + { |
| 145 | + if (item == label) |
| 146 | + { |
| 147 | + item.ForeColor = SystemColors.HighlightText; |
| 148 | + item.BackColor = SystemColors.Highlight; |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + item.ForeColor = SystemColors.ControlText; |
| 153 | + item.BackColor = SystemColors.ControlLightLight; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + private void LinkControl(Label label, Control control) |
| 160 | + { |
| 161 | + label.Click += (_, _) => |
| 162 | + { |
| 163 | + ActiveLabel(label); |
| 164 | + if (control.CanFocus) |
| 165 | + { |
| 166 | + if (!control.Focused) |
| 167 | + { |
| 168 | + control.Focus(); |
| 169 | + } |
| 170 | + } |
| 171 | + }; |
| 172 | + control.GotFocus += (_, _) => |
| 173 | + { |
| 174 | + ActiveLabel(label); |
| 175 | + }; |
| 176 | + } |
| 177 | + |
| 178 | + public void RegisterProperty(string propertyName, string propertyDescription, Func<string> getValueHandler, Action<string> setValueHandler) |
| 179 | + { |
| 180 | + var textBox = createPropertyTextBox(propertyName, propertyDescription); |
| 181 | + textBox.Text = getValueHandler(); |
| 182 | + textBox.TextChanged += (_, _) => setValueHandler(textBox.Text); |
| 183 | + addPropertyControl(createPropertyLabel(propertyName, propertyDescription), textBox); |
| 184 | + } |
| 185 | + |
| 186 | + public void RegisterProperty(string propertyName, string propertyDescription, Func<int> getValueHandler, Action<int> setValueHandler) |
| 187 | + { |
| 188 | + var textBox = createPropertyTextBox(propertyName, propertyDescription); |
| 189 | + textBox.Text = getValueHandler().ToString(); |
| 190 | + textBox.TextChanged += (_, _) => |
| 191 | + { |
| 192 | + if (int.TryParse(textBox.Text, out var v)) |
| 193 | + setValueHandler(v); |
| 194 | + else |
| 195 | + textBox.Text = getValueHandler().ToString(); |
| 196 | + }; |
| 197 | + addPropertyControl(createPropertyLabel(propertyName, propertyDescription), textBox); |
| 198 | + } |
| 199 | + |
| 200 | + public void RegisterProperty(string propertyName, string propertyDescription, Func<int?> getValueHandler, Action<int?> setValueHandler) |
| 201 | + { |
| 202 | + var textBox = createPropertyTextBox(propertyName, propertyDescription); |
| 203 | + textBox.Text = getValueHandler()?.ToString(); |
| 204 | + textBox.TextChanged += (_, _) => |
| 205 | + { |
| 206 | + var text = textBox.Text; |
| 207 | + if (string.IsNullOrEmpty(text)) |
| 208 | + setValueHandler(null); |
| 209 | + else if (int.TryParse(text, out var v)) |
| 210 | + setValueHandler(v); |
| 211 | + else |
| 212 | + textBox.Text = getValueHandler().ToString(); |
| 213 | + }; |
| 214 | + addPropertyControl(createPropertyLabel(propertyName, propertyDescription), textBox); |
| 215 | + } |
| 216 | + |
| 217 | + public void RegisterProperty(string propertyName, string propertyDescription, Func<bool> getValueHandler, Action<bool> setValueHandler) |
| 218 | + { |
| 219 | + var checkBox = createPropertyCheckBox(propertyName, propertyDescription); |
| 220 | + checkBox.Checked = getValueHandler(); |
| 221 | + checkBox.CheckedChanged += (_, _) => setValueHandler(checkBox.Checked); |
| 222 | + addPropertyControl(createPropertyLabel(propertyName, propertyDescription), checkBox); |
| 223 | + } |
| 224 | + |
| 225 | + public void RegisterProperty<TEnum>(string propertyName, string propertyDescription, Func<TEnum> getValueHandler, Action<TEnum> setValueHandler) |
| 226 | + where TEnum : struct, Enum |
| 227 | + { |
| 228 | + var comboBox = createPropertyComboBox(propertyName, propertyDescription); |
| 229 | + foreach (var item in Enum.GetValues<TEnum>()) |
| 230 | + comboBox.Items.Add(item); |
| 231 | + comboBox.SelectedItem = getValueHandler(); |
| 232 | + comboBox.SelectedIndexChanged += (_, _) => setValueHandler((TEnum)comboBox.SelectedItem); |
| 233 | + addPropertyControl(createPropertyLabel(propertyName, propertyDescription), comboBox); |
| 234 | + } |
| 235 | + } |
| 236 | +} |
0 commit comments