From 07efb41207bf8c94d7ae601618a79a5a9c9678df Mon Sep 17 00:00:00 2001 From: yibo <361071264@qq.com> Date: Fri, 30 Mar 2018 13:01:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=BA=86=E4=B8=AA=E6=97=B6=E9=92=9F?= =?UTF-8?q?=E6=8E=A7=E4=BB=B6=EF=BC=8C=E5=90=8C=E6=97=B6=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=BA=86=E4=B8=8B=E7=A7=B0=E9=87=8D=E6=8E=A7=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E5=90=AF=E7=94=A8=E5=81=9C=E7=94=A8=EF=BC=8C?= =?UTF-8?q?dataGridView=E6=94=AF=E6=8C=81=E6=8B=96=E5=8A=A8=E6=BB=9A?= =?UTF-8?q?=E5=8A=A8=E8=A1=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Test/Form1.Designer.cs | 91 +++++++++++++++++++++++++--- Test/Form1.cs | 48 +++++++++++++++ Test/Form1.resx | 6 ++ WinFormControl/UButton.cs | 4 +- WinFormControl/UDataGridView.cs | 23 +++++++ WinFormControl/ULabel.cs | 3 +- WinFormControl/UTimerLabel.cs | 44 ++++++++++++++ WinFormControl/UWeightControl.cs | 28 ++++++--- WinFormControl/WinFormControl.csproj | 3 + 9 files changed, 229 insertions(+), 21 deletions(-) create mode 100644 WinFormControl/UTimerLabel.cs diff --git a/Test/Form1.Designer.cs b/Test/Form1.Designer.cs index 2bb4656..23ec7ec 100644 --- a/Test/Form1.Designer.cs +++ b/Test/Form1.Designer.cs @@ -29,10 +29,18 @@ private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.uButton1 = new WinFormControl.UButton(); this.uScanPanel1 = new WinFormControl.UScanPanel(); + this.uTimerLabel1 = new WinFormControl.UTimerLabel(); this.uWeightControl1 = new WinFormControl.UWeightControl(); + this.uDataGridView1 = new WinFormControl.UDataGridView(); + this.RID = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.RName = new System.Windows.Forms.DataGridViewTextBoxColumn(); + ((System.ComponentModel.ISupportInitialize)(this.uDataGridView1)).BeginInit(); this.SuspendLayout(); // // comboBox1 @@ -71,38 +79,105 @@ this.uScanPanel1.BeforeScan += new System.Action(this.uScanPanel1_BeforeScan); this.uScanPanel1.AfterScan += new System.Action(this.uScanPanel1_AfterScan); // + // uTimerLabel1 + // + this.uTimerLabel1.AutoSize = true; + this.uTimerLabel1.BackColor = System.Drawing.Color.Transparent; + this.uTimerLabel1.Font = new System.Drawing.Font("宋体", 15F); + this.uTimerLabel1.Format = "M月d日 H:m:s"; + this.uTimerLabel1.Location = new System.Drawing.Point(315, 124); + this.uTimerLabel1.Name = "uTimerLabel1"; + this.uTimerLabel1.Size = new System.Drawing.Size(159, 20); + this.uTimerLabel1.TabIndex = 4; + this.uTimerLabel1.Text = "3月30日 11:37:0"; + // // uWeightControl1 // this.uWeightControl1.BackColor = System.Drawing.Color.Transparent; - this.uWeightControl1.Location = new System.Drawing.Point(2, 12); + this.uWeightControl1.Location = new System.Drawing.Point(12, 12); this.uWeightControl1.Name = "uWeightControl1"; - this.uWeightControl1.Size = new System.Drawing.Size(348, 78); - this.uWeightControl1.TabIndex = 0; - this.uWeightControl1.WeightFalg = "Test"; - this.uWeightControl1.ReceivedValue += new System.Action(this.uWeightControl1_ReceivedValue); + this.uWeightControl1.Size = new System.Drawing.Size(349, 78); + this.uWeightControl1.TabIndex = 5; + this.uWeightControl1.WeightFalg = null; + // + // uDataGridView1 + // + this.uDataGridView1.AllowUserToAddRows = false; + this.uDataGridView1.AllowUserToDeleteRows = false; + this.uDataGridView1.AllowUserToResizeColumns = false; + this.uDataGridView1.AllowUserToResizeRows = false; + dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(235)))), ((int)(((byte)(235))))); + this.uDataGridView1.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1; + this.uDataGridView1.BackgroundColor = System.Drawing.Color.White; + dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 12F); + dataGridViewCellStyle2.ForeColor = System.Drawing.Color.White; + dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.True; + this.uDataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2; + this.uDataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.uDataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.RID, + this.RName}); + this.uDataGridView1.Location = new System.Drawing.Point(558, 47); + this.uDataGridView1.MultiSelect = false; + this.uDataGridView1.Name = "uDataGridView1"; + this.uDataGridView1.ReadOnly = true; + this.uDataGridView1.RowHeadersVisible = false; + dataGridViewCellStyle3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + dataGridViewCellStyle3.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(66)))), ((int)(((byte)(163)))), ((int)(((byte)(218))))); + this.uDataGridView1.RowsDefaultCellStyle = dataGridViewCellStyle3; + this.uDataGridView1.RowTemplate.Height = 23; + this.uDataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; + this.uDataGridView1.Size = new System.Drawing.Size(259, 196); + this.uDataGridView1.TabIndex = 6; + this.uDataGridView1.CellMouseDown += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.uDataGridView1_CellMouseDown); + this.uDataGridView1.CellMouseMove += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.uDataGridView1_CellMouseMove); + this.uDataGridView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.uDataGridView1_DragDrop); + // + // RID + // + this.RID.DataPropertyName = "ID"; + this.RID.HeaderText = "序号"; + this.RID.Name = "RID"; + this.RID.ReadOnly = true; + // + // RName + // + this.RName.DataPropertyName = "Name"; + this.RName.HeaderText = "名称"; + this.RName.Name = "RName"; + this.RName.ReadOnly = true; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.White; - this.ClientSize = new System.Drawing.Size(604, 363); + this.ClientSize = new System.Drawing.Size(922, 453); + this.Controls.Add(this.uDataGridView1); + this.Controls.Add(this.uWeightControl1); + this.Controls.Add(this.uTimerLabel1); this.Controls.Add(this.comboBox1); this.Controls.Add(this.uButton1); this.Controls.Add(this.uScanPanel1); - this.Controls.Add(this.uWeightControl1); this.KeyPreview = true; this.Name = "Form1"; this.Text = "Form1"; + ((System.ComponentModel.ISupportInitialize)(this.uDataGridView1)).EndInit(); this.ResumeLayout(false); + this.PerformLayout(); } #endregion - private WinFormControl.UWeightControl uWeightControl1; private WinFormControl.UScanPanel uScanPanel1; private WinFormControl.UButton uButton1; private System.Windows.Forms.ComboBox comboBox1; + private WinFormControl.UTimerLabel uTimerLabel1; + private WinFormControl.UWeightControl uWeightControl1; + private WinFormControl.UDataGridView uDataGridView1; + private System.Windows.Forms.DataGridViewTextBoxColumn RID; + private System.Windows.Forms.DataGridViewTextBoxColumn RName; } } \ No newline at end of file diff --git a/Test/Form1.cs b/Test/Form1.cs index 9fd8ac9..3286e8c 100644 --- a/Test/Form1.cs +++ b/Test/Form1.cs @@ -13,10 +13,20 @@ namespace Test { public partial class Form1 : Form { + List list = new List(); + public Form1() { InitializeComponent(); comboBox1.DataSource = Enum.GetNames(typeof(SoundType)); + + uDataGridView1.AutoGenerateColumns = false; + for (var i = 1; i <= 20; i++) + { + list.Add(new Sut { ID = i, Name = i.ToString() }); + } + uDataGridView1.DataSource = list; + uDataGridView1.Refresh(); } private void uWeightControl1_ReceivedValue(decimal obj) @@ -40,5 +50,43 @@ namespace Test //if (Enum.TryParse(comboBox1.SelectedValue.ToString(), out sy)) // SoundPalyUtil.PlaySound(sy); } + + //int clickIdx; + private void uDataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) + { + //if (e.Button == MouseButtons.Left) + // clickIdx = e.RowIndex; + } + + private void uDataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) + { + //if (e.Button != MouseButtons.Left || e.RowIndex < 0) + // return; + + //if (clickIdx != e.RowIndex) + //{ + // var add = clickIdx - e.RowIndex; + // if (add > 0) + // add += 1; + // var fRowIdx = uDataGridView1.FirstDisplayedScrollingRowIndex + add; + // uScanPanel1.TextBox.Text = add.ToString(); + // if (fRowIdx < 0 || fRowIdx > uDataGridView1.RowCount - 1) + // return; + + // uDataGridView1.FirstDisplayedScrollingRowIndex = fRowIdx; + //} + } + + private void uDataGridView1_DragDrop(object sender, DragEventArgs e) + { + //MessageBox.Show("dd"); + } + } + + class Sut + { + public long ID { get; set; } + + public string Name { get; set; } } } diff --git a/Test/Form1.resx b/Test/Form1.resx index 681c9a5..7672ca5 100644 --- a/Test/Form1.resx +++ b/Test/Form1.resx @@ -126,4 +126,10 @@ KAgKgoKg1ZsPvpCB0hBohjQAAAAASUVORK5CYII= + + True + + + True + \ No newline at end of file diff --git a/WinFormControl/UButton.cs b/WinFormControl/UButton.cs index 48e46d3..c954740 100644 --- a/WinFormControl/UButton.cs +++ b/WinFormControl/UButton.cs @@ -12,9 +12,8 @@ namespace WinFormControl { public class UButton : Button { - protected override void InitLayout() + public UButton() { - base.InitLayout(); this.BackgroundImage = Resources.simpleBtn; this.ForeColor = Color.Black; this.FlatStyle = FlatStyle.Flat; @@ -22,7 +21,6 @@ namespace WinFormControl this.FlatAppearance.BorderColor = Color.FromArgb(45, 155, 214); } - [Browsable(true), Category("Sound"), Description("是否播放声音")] public bool PlaySound { get; set; } diff --git a/WinFormControl/UDataGridView.cs b/WinFormControl/UDataGridView.cs index 60b3741..5ede41d 100644 --- a/WinFormControl/UDataGridView.cs +++ b/WinFormControl/UDataGridView.cs @@ -33,11 +33,34 @@ namespace WinFormControl ForeColor = System.Drawing.Color.White, WrapMode = System.Windows.Forms.DataGridViewTriState.True, }; + var clickIndex = 0; this.RowsDefaultCellStyle = new DataGridViewCellStyle() { SelectionBackColor = System.Drawing.Color.FromArgb(66, 163, 218), Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134) }; + this.CellMouseDown += (sender, e) => + { + if (e.Button == MouseButtons.Left) + clickIndex = e.RowIndex; + }; + + this.CellMouseMove += (sender, e) => + { + if (e.Button != MouseButtons.Left || e.RowIndex < 0) + return; + + if (clickIndex != e.RowIndex) + { + var add = clickIndex - e.RowIndex; + if (add > 0) + add += 1; + var fRowIdx = this.FirstDisplayedScrollingRowIndex + add; + if (fRowIdx < 0 || fRowIdx > this.RowCount - 1) + return; + this.FirstDisplayedScrollingRowIndex = fRowIdx; + } + }; } } } diff --git a/WinFormControl/ULabel.cs b/WinFormControl/ULabel.cs index fdd2dca..abd14b3 100644 --- a/WinFormControl/ULabel.cs +++ b/WinFormControl/ULabel.cs @@ -10,9 +10,8 @@ namespace WinFormControl { public class ULabel : Label { - protected override void InitLayout() + public ULabel() { - base.InitLayout(); this.BackColor = Color.Transparent; } } diff --git a/WinFormControl/UTimerLabel.cs b/WinFormControl/UTimerLabel.cs new file mode 100644 index 0000000..bfdea06 --- /dev/null +++ b/WinFormControl/UTimerLabel.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using WinFormControl.Converter; + +namespace WinFormControl +{ + public class UTimerLabel : ULabel + { + private string mFormat = "yyyy-MM-dd HH:mm:ss"; + [Browsable(true), Description("显示格式")] + public string Format + { + get { return mFormat; } + set + { + if (!string.IsNullOrEmpty(value)) + mFormat = value; + } + } + + public UTimerLabel() + { + this.Font = new System.Drawing.Font("黑体", 13); + } + + protected override void OnCreateControl() + { + base.OnCreateControl(); + var form = ControlUtil.GetParentFormm(this); + if (form != null) + { + var timer = new Timer(); + timer.Interval = 1000; + timer.Enabled = true; + timer.Tick += delegate { this.Text = DateTime.Now.ToString(mFormat); }; + } + } + } +} diff --git a/WinFormControl/UWeightControl.cs b/WinFormControl/UWeightControl.cs index a6f3d53..b22e66d 100644 --- a/WinFormControl/UWeightControl.cs +++ b/WinFormControl/UWeightControl.cs @@ -73,28 +73,40 @@ namespace WinFormControl } settingBtn.Enabled = switchOn; switchOn = !switchOn; + ChangeWeightBtnState(); if (switchOn) { - weightSwitch.BackgroundImage = Resources.onclickBtn; - weightSwitch.ForeColor = Color.White; - weightSwitch.Text = "停止称重"; - OpenSerialPort(); _mainProcessIsRun = true; ReadData(); } else { - weightSwitch.BackgroundImage = Resources.simpleBtn; - weightSwitch.ForeColor = Color.Black; - weightSwitch.Text = "启用称重"; DisableWeight(); } } catch { - settingBtn.Enabled = !switchOn; switchOn = !switchOn; + settingBtn.Enabled = !switchOn; + ChangeWeightBtnState(); + throw; + } + } + + void ChangeWeightBtnState() + { + if (switchOn) + { + weightSwitch.BackgroundImage = Resources.onclickBtn; + weightSwitch.ForeColor = Color.White; + weightSwitch.Text = "停止称重"; + } + else + { + weightSwitch.BackgroundImage = Resources.simpleBtn; + weightSwitch.ForeColor = Color.Black; + weightSwitch.Text = "启用称重"; } } diff --git a/WinFormControl/WinFormControl.csproj b/WinFormControl/WinFormControl.csproj index bbecdec..d1ef811 100644 --- a/WinFormControl/WinFormControl.csproj +++ b/WinFormControl/WinFormControl.csproj @@ -57,6 +57,9 @@ True Resources.resx + + Component + UserControl