From ec175d3e20cfa800de44399592b33ba5f0ba434b Mon Sep 17 00:00:00 2001 From: zqm Date: Thu, 26 Mar 2026 13:35:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=B2=BE=E7=BB=86=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Framework4.0/Camera/Camera/Camera.csproj | 1 + .../Camera/Camera/Setting.Designer.cs | 188 +++++++++++++++++- .../CS/Framework4.0/Camera/Camera/Setting.cs | 158 ++++++++++++--- .../Camera/Camera/ToolStripNumericUpDown.cs | 111 +++++++++++ 4 files changed, 427 insertions(+), 31 deletions(-) create mode 100644 Windows/CS/Framework4.0/Camera/Camera/ToolStripNumericUpDown.cs diff --git a/Windows/CS/Framework4.0/Camera/Camera/Camera.csproj b/Windows/CS/Framework4.0/Camera/Camera/Camera.csproj index 4c4d1df..2a12287 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Camera.csproj +++ b/Windows/CS/Framework4.0/Camera/Camera/Camera.csproj @@ -54,6 +54,7 @@ Setting.cs + diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs index 4cb6fda..a85f86f 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs @@ -22,11 +22,19 @@ namespace Camera this.splitContainer2 = new System.Windows.Forms.SplitContainer(); this.toolStrip1 = new System.Windows.Forms.ToolStrip(); this.toolStripButton1 = new System.Windows.Forms.ToolStripButton(); - this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.toolStripButton2 = new System.Windows.Forms.ToolStripButton(); this.toolStripButton3 = new System.Windows.Forms.ToolStripButton(); this.toolStripButton4 = new System.Windows.Forms.ToolStripButton(); this.toolStripTextBox1 = new System.Windows.Forms.ToolStripTextBox(); + this.toolStripLabel1 = new System.Windows.Forms.ToolStripLabel(); + this.toolStripNumericUpDown1 = new ToolStripNumericUpDown(); + this.toolStripLabel2 = new System.Windows.Forms.ToolStripLabel(); + this.toolStripNumericUpDown2 = new ToolStripNumericUpDown(); + this.toolStripLabel3 = new System.Windows.Forms.ToolStripLabel(); + this.toolStripNumericUpDown3 = new ToolStripNumericUpDown(); + this.toolStripLabel4 = new System.Windows.Forms.ToolStripLabel(); + this.toolStripNumericUpDown4 = new ToolStripNumericUpDown(); + this.dataGridView1 = new System.Windows.Forms.DataGridView(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); @@ -66,7 +74,6 @@ namespace Camera this.picBoxCamera.Location = new System.Drawing.Point(0, 0); this.picBoxCamera.Name = "picBoxCamera"; this.picBoxCamera.Size = new System.Drawing.Size(700, 515); - this.picBoxCamera.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal; this.picBoxCamera.TabIndex = 0; this.picBoxCamera.TabStop = false; this.picBoxCamera.SizeChanged += new System.EventHandler(this.PicBoxCamera_SizeChanged); @@ -104,10 +111,18 @@ namespace Camera this.toolStripButton2, this.toolStripButton3, this.toolStripButton4, - this.toolStripTextBox1}); + this.toolStripTextBox1, + this.toolStripLabel1, + this.toolStripNumericUpDown1, + this.toolStripLabel2, + this.toolStripNumericUpDown2, + this.toolStripLabel3, + this.toolStripNumericUpDown3, + this.toolStripLabel4, + this.toolStripNumericUpDown4}); this.toolStrip1.Location = new System.Drawing.Point(0, 0); this.toolStrip1.Name = "toolStrip1"; - this.toolStrip1.Size = new System.Drawing.Size(80, 40); + this.toolStrip1.Size = new System.Drawing.Size(58, 25); this.toolStrip1.TabIndex = 0; this.toolStrip1.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.toolStrip1_ItemClicked); // @@ -116,7 +131,7 @@ namespace Camera this.toolStripButton1.CheckOnClick = true; this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.toolStripButton1.Name = "toolStripButton1"; - this.toolStripButton1.Size = new System.Drawing.Size(36, 36); + this.toolStripButton1.Size = new System.Drawing.Size(23, 22); this.toolStripButton1.ToolTipText = "修改检测区(点击开启/关闭)"; this.toolStripButton1.Click += new System.EventHandler(this.ToolStripButton1_Click); // @@ -125,7 +140,7 @@ namespace Camera this.toolStripButton2.CheckOnClick = true; this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.toolStripButton2.Name = "toolStripButton2"; - this.toolStripButton2.Size = new System.Drawing.Size(36, 36); + this.toolStripButton2.Size = new System.Drawing.Size(23, 22); this.toolStripButton2.ToolTipText = "绘制Led区(点击开启/关闭)"; this.toolStripButton2.Click += new System.EventHandler(this.ToolStripButton2_Click); // @@ -133,7 +148,7 @@ namespace Camera // this.toolStripButton3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.toolStripButton3.Name = "toolStripButton3"; - this.toolStripButton3.Size = new System.Drawing.Size(36, 36); + this.toolStripButton3.Size = new System.Drawing.Size(23, 28); this.toolStripButton3.ToolTipText = "选择颜色"; this.toolStripButton3.Visible = false; this.toolStripButton3.Click += new System.EventHandler(this.ToolStripButton3_Click); @@ -142,7 +157,7 @@ namespace Camera // this.toolStripButton4.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.toolStripButton4.Name = "toolStripButton4"; - this.toolStripButton4.Size = new System.Drawing.Size(36, 36); + this.toolStripButton4.Size = new System.Drawing.Size(23, 28); this.toolStripButton4.ToolTipText = "删除Led区"; this.toolStripButton4.Visible = false; this.toolStripButton4.Click += new System.EventHandler(this.ToolStripButton4_Click); @@ -158,6 +173,154 @@ namespace Camera this.toolStripTextBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ToolStripTextBox1_KeyDown); this.toolStripTextBox1.TextChanged += new System.EventHandler(this.ToolStripTextBox1_TextChanged); // + // toolStripLabel1 + // + this.toolStripLabel1.Name = "toolStripLabel1"; + this.toolStripLabel1.Size = new System.Drawing.Size(23, 27); + this.toolStripLabel1.Text = "X:"; + this.toolStripLabel1.Visible = false; + // + // toolStripNumericUpDown1 + // + this.toolStripNumericUpDown1.AutoSize = false; + this.toolStripNumericUpDown1.DecimalPlaces = 0; + this.toolStripNumericUpDown1.Increment = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.toolStripNumericUpDown1.Maximum = new decimal(new int[] { + 10000, + 0, + 0, + 0}); + this.toolStripNumericUpDown1.Minimum = new decimal(new int[] { + 0, + 0, + 0, + 0}); + this.toolStripNumericUpDown1.Name = "toolStripNumericUpDown1"; + this.toolStripNumericUpDown1.Size = new System.Drawing.Size(60, 27); + this.toolStripNumericUpDown1.ToolTipText = "X坐标"; + this.toolStripNumericUpDown1.Value = new decimal(new int[] { + 0, + 0, + 0, + 0}); + this.toolStripNumericUpDown1.Visible = false; + this.toolStripNumericUpDown1.ValueChanged += new System.EventHandler(this.ToolStripNumericUpDown1_ValueChanged); + // + // toolStripLabel2 + // + this.toolStripLabel2.Name = "toolStripLabel2"; + this.toolStripLabel2.Size = new System.Drawing.Size(22, 27); + this.toolStripLabel2.Text = "Y:"; + this.toolStripLabel2.Visible = false; + // + // toolStripNumericUpDown2 + // + this.toolStripNumericUpDown2.AutoSize = false; + this.toolStripNumericUpDown2.DecimalPlaces = 0; + this.toolStripNumericUpDown2.Increment = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.toolStripNumericUpDown2.Maximum = new decimal(new int[] { + 10000, + 0, + 0, + 0}); + this.toolStripNumericUpDown2.Minimum = new decimal(new int[] { + 0, + 0, + 0, + 0}); + this.toolStripNumericUpDown2.Name = "toolStripNumericUpDown2"; + this.toolStripNumericUpDown2.Size = new System.Drawing.Size(60, 27); + this.toolStripNumericUpDown2.ToolTipText = "Y坐标"; + this.toolStripNumericUpDown2.Value = new decimal(new int[] { + 0, + 0, + 0, + 0}); + this.toolStripNumericUpDown2.Visible = false; + this.toolStripNumericUpDown2.ValueChanged += new System.EventHandler(this.ToolStripNumericUpDown2_ValueChanged); + // + // toolStripLabel3 + // + this.toolStripLabel3.Name = "toolStripLabel3"; + this.toolStripLabel3.Size = new System.Drawing.Size(28, 27); + this.toolStripLabel3.Text = "W:"; + this.toolStripLabel3.Visible = false; + // + // toolStripNumericUpDown3 + // + this.toolStripNumericUpDown3.AutoSize = false; + this.toolStripNumericUpDown3.DecimalPlaces = 0; + this.toolStripNumericUpDown3.Increment = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.toolStripNumericUpDown3.Maximum = new decimal(new int[] { + 10000, + 0, + 0, + 0}); + this.toolStripNumericUpDown3.Minimum = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.toolStripNumericUpDown3.Name = "toolStripNumericUpDown3"; + this.toolStripNumericUpDown3.Size = new System.Drawing.Size(60, 27); + this.toolStripNumericUpDown3.ToolTipText = "宽度"; + this.toolStripNumericUpDown3.Value = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.toolStripNumericUpDown3.Visible = false; + this.toolStripNumericUpDown3.ValueChanged += new System.EventHandler(this.ToolStripNumericUpDown3_ValueChanged); + // + // toolStripLabel4 + // + this.toolStripLabel4.Name = "toolStripLabel4"; + this.toolStripLabel4.Size = new System.Drawing.Size(25, 27); + this.toolStripLabel4.Text = "H:"; + this.toolStripLabel4.Visible = false; + // + // toolStripNumericUpDown4 + // + this.toolStripNumericUpDown4.AutoSize = false; + this.toolStripNumericUpDown4.DecimalPlaces = 0; + this.toolStripNumericUpDown4.Increment = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.toolStripNumericUpDown4.Maximum = new decimal(new int[] { + 10000, + 0, + 0, + 0}); + this.toolStripNumericUpDown4.Minimum = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.toolStripNumericUpDown4.Name = "toolStripNumericUpDown4"; + this.toolStripNumericUpDown4.Size = new System.Drawing.Size(60, 27); + this.toolStripNumericUpDown4.ToolTipText = "高度"; + this.toolStripNumericUpDown4.Value = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.toolStripNumericUpDown4.Visible = false; + this.toolStripNumericUpDown4.ValueChanged += new System.EventHandler(this.ToolStripNumericUpDown4_ValueChanged); + // // dataGridView1 // this.dataGridView1.AllowUserToAddRows = false; @@ -172,6 +335,7 @@ namespace Camera this.dataGridView1.RowHeadersVisible = false; this.dataGridView1.RowTemplate.Height = 25; this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; + this.dataGridView1.Size = new System.Drawing.Size(559, 254); this.dataGridView1.TabIndex = 0; this.dataGridView1.SelectionChanged += new System.EventHandler(this.DataGridView1_SelectionChanged); // @@ -214,6 +378,14 @@ namespace Camera private System.Windows.Forms.ToolStripButton toolStripButton3; private System.Windows.Forms.ToolStripButton toolStripButton4; private System.Windows.Forms.ToolStripTextBox toolStripTextBox1; + private System.Windows.Forms.ToolStripLabel toolStripLabel1; + private ToolStripNumericUpDown toolStripNumericUpDown1; + private System.Windows.Forms.ToolStripLabel toolStripLabel2; + private ToolStripNumericUpDown toolStripNumericUpDown2; + private System.Windows.Forms.ToolStripLabel toolStripLabel3; + private ToolStripNumericUpDown toolStripNumericUpDown3; + private System.Windows.Forms.ToolStripLabel toolStripLabel4; + private ToolStripNumericUpDown toolStripNumericUpDown4; private System.Windows.Forms.DataGridView dataGridView1; } } \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs index 26c04e4..466d722 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs @@ -18,8 +18,8 @@ namespace Camera private bool _isDrawing = false; private bool _isMoving = false; private bool _isResizing = false; - private Point _startPoint; - private Point _currentMousePoint; + private Point? _startPoint; + private Point? _currentMousePoint; private Rectangle _originalZone; private bool _isEditingDetectionZone = false; private bool _isEditingLedZone = false; @@ -70,20 +70,22 @@ namespace Camera private void InitializeDataGridView() { - dataGridView1.Columns.Clear(); - dataGridView1.Columns.Add("Index", "索引"); - dataGridView1.Columns.Add("X", "X坐标"); - dataGridView1.Columns.Add("Y", "Y坐标"); - dataGridView1.Columns.Add("Width", "宽度"); - dataGridView1.Columns.Add("Height", "高度"); - dataGridView1.Columns.Add("Color", "颜色"); + if (dataGridView1.Columns.Count == 0) + { + dataGridView1.Columns.Add("Index", "索引"); + dataGridView1.Columns.Add("X", "X坐标"); + dataGridView1.Columns.Add("Y", "Y坐标"); + dataGridView1.Columns.Add("Width", "宽度"); + dataGridView1.Columns.Add("Height", "高度"); + dataGridView1.Columns.Add("Color", "颜色"); - dataGridView1.Columns["Index"].Width = 50; - dataGridView1.Columns["X"].Width = 60; - dataGridView1.Columns["Y"].Width = 60; - dataGridView1.Columns["Width"].Width = 60; - dataGridView1.Columns["Height"].Width = 60; - dataGridView1.Columns["Color"].Width = 70; + dataGridView1.Columns["Index"].Width = 50; + dataGridView1.Columns["X"].Width = 60; + dataGridView1.Columns["Y"].Width = 60; + dataGridView1.Columns["Width"].Width = 60; + dataGridView1.Columns["Height"].Width = 60; + dataGridView1.Columns["Color"].Width = 70; + } } private void Setting_Load(object sender, EventArgs e) @@ -359,10 +361,14 @@ namespace Camera } } - int x = (int)(Math.Min(_startPoint.X, _startPoint.X + (_currentMousePoint.X - _resizeStartPoint.X) / scaleX) * scaleX); - int y = (int)(Math.Min(_startPoint.Y, _startPoint.Y + (_currentMousePoint.Y - _resizeStartPoint.Y) / scaleY) * scaleY); - int w = (int)(Math.Abs(_currentMousePoint.X - _resizeStartPoint.X)); - int h = (int)(Math.Abs(_currentMousePoint.Y - _resizeStartPoint.Y)); + int x = 0, y = 0, w = 0, h = 0; + if (_startPoint.HasValue && _currentMousePoint.HasValue) + { + x = (int)(Math.Min(_startPoint.Value.X, _startPoint.Value.X + (_currentMousePoint.Value.X - _resizeStartPoint.X) / scaleX) * scaleX); + y = (int)(Math.Min(_startPoint.Value.Y, _startPoint.Value.Y + (_currentMousePoint.Value.Y - _resizeStartPoint.Y) / scaleY) * scaleY); + w = (int)(Math.Abs(_currentMousePoint.Value.X - _resizeStartPoint.X)); + h = (int)(Math.Abs(_currentMousePoint.Value.Y - _resizeStartPoint.Y)); + } if (w > 5 && h > 5) { @@ -876,10 +882,14 @@ namespace Camera if (_isDrawing) { - int x = Math.Min(_startPoint.X, imagePoint.X); - int y = Math.Min(_startPoint.Y, imagePoint.Y); - int width = Math.Abs(imagePoint.X - _startPoint.X); - int height = Math.Abs(imagePoint.Y - _startPoint.Y); + int x = 0, y = 0, width = 0, height = 0; + if (_startPoint.HasValue) + { + x = Math.Min(_startPoint.Value.X, imagePoint.X); + y = Math.Min(_startPoint.Value.Y, imagePoint.Y); + width = Math.Abs(imagePoint.X - _startPoint.Value.X); + height = Math.Abs(imagePoint.Y - _startPoint.Value.Y); + } if (width > 5 && height > 5) { @@ -1015,6 +1025,17 @@ namespace Camera if (dataGridView1.SelectedRows.Count > 0) { _selectedZoneIndex = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value); + // 更新位置和宽高值 + if (_selectedZoneIndex >= 0 && _camera != null) + { + Rectangle zone = _camera.GetLedZone(_selectedZoneIndex); + toolStripNumericUpDown1.Value = zone.X; + toolStripNumericUpDown2.Value = zone.Y; + toolStripNumericUpDown3.Value = zone.Width; + toolStripNumericUpDown4.Value = zone.Height; + } + // 确保控件在就绪状态下显示 + UpdateLedZoneButtonsVisibility(0); picBoxCamera.Invalidate(); } } @@ -1214,6 +1235,24 @@ namespace Camera toolStripButton4.Visible = false; toolStripTextBox1.Visible = false; toolStripTextBox1.Text = ""; + // 显示位置和宽高设置框 + toolStripLabel1.Visible = true; + toolStripNumericUpDown1.Visible = true; + toolStripLabel2.Visible = true; + toolStripNumericUpDown2.Visible = true; + toolStripLabel3.Visible = true; + toolStripNumericUpDown3.Visible = true; + toolStripLabel4.Visible = true; + toolStripNumericUpDown4.Visible = true; + // 更新位置和宽高值 + if (_selectedZoneIndex >= 0 && _camera != null) + { + Rectangle zone = _camera.GetLedZone(_selectedZoneIndex); + toolStripNumericUpDown1.Value = zone.X; + toolStripNumericUpDown2.Value = zone.Y; + toolStripNumericUpDown3.Value = zone.Width; + toolStripNumericUpDown4.Value = zone.Height; + } break; case 1: // 选中状态 toolStripButton2.Visible = false; @@ -1221,6 +1260,15 @@ namespace Camera toolStripButton4.Visible = true; toolStripTextBox1.Visible = true; toolStripTextBox1.Text = _currentLedIndex.ToString(); + // 不显示位置和宽高设置框 + toolStripLabel1.Visible = false; + toolStripNumericUpDown1.Visible = false; + toolStripLabel2.Visible = false; + toolStripNumericUpDown2.Visible = false; + toolStripLabel3.Visible = false; + toolStripNumericUpDown3.Visible = false; + toolStripLabel4.Visible = false; + toolStripNumericUpDown4.Visible = false; break; case 2: // 绘制状态 toolStripButton2.Visible = true; @@ -1228,6 +1276,14 @@ namespace Camera toolStripButton4.Visible = false; toolStripTextBox1.Visible = false; toolStripTextBox1.Text = ""; + toolStripLabel1.Visible = false; + toolStripNumericUpDown1.Visible = false; + toolStripLabel2.Visible = false; + toolStripNumericUpDown2.Visible = false; + toolStripLabel3.Visible = false; + toolStripNumericUpDown3.Visible = false; + toolStripLabel4.Visible = false; + toolStripNumericUpDown4.Visible = false; break; } } @@ -1246,5 +1302,61 @@ namespace Camera { } + + // X坐标相关事件 + private void ToolStripNumericUpDown1_ValueChanged(object sender, EventArgs e) + { + int index = _currentLedIndex >= 0 ? _currentLedIndex : _selectedZoneIndex; + if (index < 0 || _camera == null) return; + + int x = (int)toolStripNumericUpDown1.Value; + Rectangle zone = _camera.GetLedZone(index); + zone.X = x; + _camera.SetLedZone(index, zone); + UpdateDataGridView(); + picBoxCamera.Invalidate(); + } + + // Y坐标相关事件 + private void ToolStripNumericUpDown2_ValueChanged(object sender, EventArgs e) + { + int index = _currentLedIndex >= 0 ? _currentLedIndex : _selectedZoneIndex; + if (index < 0 || _camera == null) return; + + int y = (int)toolStripNumericUpDown2.Value; + Rectangle zone = _camera.GetLedZone(index); + zone.Y = y; + _camera.SetLedZone(index, zone); + UpdateDataGridView(); + picBoxCamera.Invalidate(); + } + + // 宽度相关事件 + private void ToolStripNumericUpDown3_ValueChanged(object sender, EventArgs e) + { + int index = _currentLedIndex >= 0 ? _currentLedIndex : _selectedZoneIndex; + if (index < 0 || _camera == null) return; + + int width = (int)toolStripNumericUpDown3.Value; + Rectangle zone = _camera.GetLedZone(index); + zone.Width = width; + _camera.SetLedZone(index, zone); + UpdateDataGridView(); + picBoxCamera.Invalidate(); + } + + // 高度相关事件 + private void ToolStripNumericUpDown4_ValueChanged(object sender, EventArgs e) + { + int index = _currentLedIndex >= 0 ? _currentLedIndex : _selectedZoneIndex; + if (index < 0 || _camera == null) return; + + int height = (int)toolStripNumericUpDown4.Value; + Rectangle zone = _camera.GetLedZone(index); + zone.Height = height; + _camera.SetLedZone(index, zone); + UpdateDataGridView(); + picBoxCamera.Invalidate(); + } } } diff --git a/Windows/CS/Framework4.0/Camera/Camera/ToolStripNumericUpDown.cs b/Windows/CS/Framework4.0/Camera/Camera/ToolStripNumericUpDown.cs new file mode 100644 index 0000000..abb3c80 --- /dev/null +++ b/Windows/CS/Framework4.0/Camera/Camera/ToolStripNumericUpDown.cs @@ -0,0 +1,111 @@ +using System; +using System.ComponentModel; +using System.Drawing; +using System.Windows.Forms; + +namespace Camera +{ + // 自定义ToolStripNumericUpDown控件 + [System.ComponentModel.ToolboxItem(false)] + [System.ComponentModel.DesignerCategory("Code")] + public class ToolStripNumericUpDown : ToolStripControlHost + { + private NumericUpDown _numericUpDown; + + // 公开的无参构造函数 - 设计器必需 + public ToolStripNumericUpDown() : this(new NumericUpDown()) { } + + // 私有的带参数构造函数 - 仅用于无参构造函数调用 + private ToolStripNumericUpDown(NumericUpDown numericUpDown) : base(numericUpDown) + { + _numericUpDown = numericUpDown ?? throw new ArgumentNullException(nameof(numericUpDown)); + this.AutoSize = false; + + // 设置NumericUpDown的基本属性 + _numericUpDown.Size = new Size(60, 22); + _numericUpDown.TextAlign = HorizontalAlignment.Right; + } + + [System.ComponentModel.Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public NumericUpDown NumericUpDownControl => _numericUpDown; + + // 下面的属性、事件都保持不变 + public decimal Value + { + get => _numericUpDown.Value; + set => _numericUpDown.Value = value; + } + + public decimal Minimum + { + get => _numericUpDown.Minimum; + set => _numericUpDown.Minimum = value; + } + + public decimal Maximum + { + get => _numericUpDown.Maximum; + set => _numericUpDown.Maximum = value; + } + + public decimal Increment + { + get => _numericUpDown.Increment; + set => _numericUpDown.Increment = value; + } + + public int DecimalPlaces + { + get => _numericUpDown.DecimalPlaces; + set => _numericUpDown.DecimalPlaces = value; + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new string Text + { + get => _numericUpDown.Text; + set => _numericUpDown.Text = value; + } + + public event EventHandler ValueChanged; + + protected override void OnSubscribeControlEvents(Control control) + { + base.OnSubscribeControlEvents(control); + if (control is NumericUpDown nud) + nud.ValueChanged += Nud_ValueChanged; + } + + protected override void OnUnsubscribeControlEvents(Control control) + { + base.OnUnsubscribeControlEvents(control); + if (control is NumericUpDown nud) + nud.ValueChanged -= Nud_ValueChanged; + } + + private void Nud_ValueChanged(object sender, EventArgs e) + { + ValueChanged?.Invoke(this, e); + } + + protected override Size DefaultSize => new Size(60, 27); + + // 重写GetPreferredSize以改进布局 + public override Size GetPreferredSize(Size constrainingSize) + { + return new Size(base.GetPreferredSize(constrainingSize).Width, 27); + } + + // 清理资源 + protected override void Dispose(bool disposing) + { + if (disposing && _numericUpDown != null) + { + _numericUpDown.Dispose(); + } + base.Dispose(disposing); + } + } +}