Winform控件重写
因为最近的项目中越来越多的遇到了比较特殊的一些控件,有时候我们自己封装一下可能更加方便我们的使用,下面是我们项目中用到的,简单做一个记录。
TextBox控件重写
主要的控制代码都在WndProc中
只能输入数字,包括小数
public class FloatNumTextBox : TextBox
{
protected override void WndProc(ref Message m)
{
int WM_CHAR = 0x0102;
if (m.Msg == WM_CHAR)
{
if (((char)m.WParam >= '') && ((char)m.WParam <= '') ||
(int)m.WParam == (int)Keys.Back || (int)m.WParam == (int)Keys.Delete)
{
base.WndProc(ref m);
}
}
else
{
base.WndProc(ref m);
}
}
}
只能输入整数
public class IntNumTextBox :TextBox
{
protected override void WndProc(ref Message m)
{
int WM_CHAR = 0x0102;
if (m.Msg == WM_CHAR)
{
if (((char)m.WParam >= '') && ((char)m.WParam <= '') ||
(int)m.WParam == (int)Keys.Back)
{
base.WndProc(ref m);
}
}
else
{
base.WndProc(ref m);
}
}
}
DataGridFView重写
DataGridView的主要代码是因为有很多的样式不需要再重新设置了,这样可以只修改一处,即可修改整个项目中的所有DataGridView样式的显示,主要的代码起始都是在构造函数中完成
public class MydataGridview :DataGridView
{
public MydataGridview():base()
{
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.AutoGenerateColumns = false;
this.AllowUserToAddRows = false;
this.AllowUserToDeleteRows = false;
this.AllowUserToResizeColumns = false;
this.AllowUserToResizeRows = false;
dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
this.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.BackgroundColor = System.Drawing.Color.White;
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Navy;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
this.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
this.ColumnHeadersHeight = ;
this.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle3.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle3.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.DefaultCellStyle = dataGridViewCellStyle3;
this.EnableHeadersVisualStyles = false;
this.GridColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.Location = new System.Drawing.Point(, ); this.RowHeadersVisible = false;
this.RowTemplate.Height = ;
this.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.Size = new System.Drawing.Size(, ); }
}