Browse Source

加了个时钟控件,同时修复了下称重控件的点击启用停用,dataGridView支持拖动滚动行。

master
yibo 7 years ago
parent
commit
07efb41207
9 changed files with 229 additions and 21 deletions
  1. +83
    -8
      Test/Form1.Designer.cs
  2. +48
    -0
      Test/Form1.cs
  3. +6
    -0
      Test/Form1.resx
  4. +1
    -3
      WinFormControl/UButton.cs
  5. +23
    -0
      WinFormControl/UDataGridView.cs
  6. +1
    -2
      WinFormControl/ULabel.cs
  7. +44
    -0
      WinFormControl/UTimerLabel.cs
  8. +20
    -8
      WinFormControl/UWeightControl.cs
  9. +3
    -0
      WinFormControl/WinFormControl.csproj

+ 83
- 8
Test/Form1.Designer.cs View File

@ -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<decimal>(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;
}
}

+ 48
- 0
Test/Form1.cs View File

@ -13,10 +13,20 @@ namespace Test
{
public partial class Form1 : Form
{
List<Sut> list = new List<Sut>();
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; }
}
}

+ 6
- 0
Test/Form1.resx View File

@ -126,4 +126,10 @@
KAgKgoKg1ZsPvpCB0hBohjQAAAAASUVORK5CYII=
</value>
</data>
<metadata name="RID.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="RName.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
</root>

+ 1
- 3
WinFormControl/UButton.cs View File

@ -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; }


+ 23
- 0
WinFormControl/UDataGridView.cs View File

@ -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;
}
};
}
}
}

+ 1
- 2
WinFormControl/ULabel.cs View File

@ -10,9 +10,8 @@ namespace WinFormControl
{
public class ULabel : Label
{
protected override void InitLayout()
public ULabel()
{
base.InitLayout();
this.BackColor = Color.Transparent;
}
}


+ 44
- 0
WinFormControl/UTimerLabel.cs View File

@ -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); };
}
}
}
}

+ 20
- 8
WinFormControl/UWeightControl.cs View File

@ -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 = "启用称重";
}
}


+ 3
- 0
WinFormControl/WinFormControl.csproj View File

@ -57,6 +57,9 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="UTimerLabel.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="UScanPanel.cs">
<SubType>UserControl</SubType>
</Compile>


Loading…
Cancel
Save