Winform控件重写

Winform控件重写

因为最近的项目中越来越多的遇到了比较特殊的一些控件,有时候我们自己封装一下可能更加方便我们的使用,下面是我们项目中用到的,简单做一个记录。

TextBox控件重写

主要的控制代码都在WndProc中

只能输入数字,包括小数

 1  public class FloatNumTextBox : TextBox
 2     {
 3         protected override void WndProc(ref Message m)
 4         {
 5             int WM_CHAR = 0x0102;
 6             if (m.Msg == WM_CHAR)
 7             {
 8                 if (((char)m.WParam >= '0') && ((char)m.WParam <= '9') ||
 9                 (int)m.WParam == (int)Keys.Back || (int)m.WParam == (int)Keys.Delete)
10                 {
11                     base.WndProc(ref m);
12                 }
13             }
14             else
15             {
16                 base.WndProc(ref m);
17             }
18         }
19     }

只能输入整数

 1 public class IntNumTextBox :TextBox
 2     {
 3         protected override void WndProc(ref Message m)
 4         {
 5             int WM_CHAR = 0x0102;
 6             if (m.Msg == WM_CHAR)
 7             {
 8                 if (((char)m.WParam >= '0') && ((char)m.WParam <= '9') ||
 9                 (int)m.WParam == (int)Keys.Back)
10                 {
11                     base.WndProc(ref m);
12                 }
13             }
14             else
15             {
16                 base.WndProc(ref m);
17             }
18         }
19     }

DataGridFView重写

DataGridView的主要代码是因为有很多的样式不需要再重新设置了,这样可以只修改一处,即可修改整个项目中的所有DataGridView样式的显示,主要的代码起始都是在构造函数中完成

 1  public class MydataGridview :DataGridView
 2     {
 3         public MydataGridview():base()
 4         {
 5             System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
 6             System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
 7             System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
 8             this.AutoGenerateColumns = false;
 9             this.AllowUserToAddRows = false;
10             this.AllowUserToDeleteRows = false;
11             this.AllowUserToResizeColumns = false;
12             this.AllowUserToResizeRows = false;
13             dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(245)))), ((int)(((byte)(243)))), ((int)(((byte)(244)))));
14             this.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
15             this.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
16             this.BackgroundColor = System.Drawing.Color.White;
17             this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
18             this.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
19             dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
20             dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(241)))), ((int)(((byte)(241)))), ((int)(((byte)(241)))));
21             dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
22             dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Navy;
23             dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
24             dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
25             this.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
26             this.ColumnHeadersHeight = 25;
27             this.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
28             dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
29             dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Window;
30             dataGridViewCellStyle3.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
31             dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.ControlText;
32             dataGridViewCellStyle3.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(173)))), ((int)(((byte)(214)))), ((int)(((byte)(237)))));
33             dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.ControlText;
34             dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
35             this.DefaultCellStyle = dataGridViewCellStyle3;
36             this.EnableHeadersVisualStyles = false;
37             this.GridColor = System.Drawing.Color.FromArgb(((int)(((byte)(236)))), ((int)(((byte)(233)))), ((int)(((byte)(216)))));
38             this.Location = new System.Drawing.Point(3, 17);
39             
40             this.RowHeadersVisible = false;
41             this.RowTemplate.Height = 23;
42             this.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
43             this.Size = new System.Drawing.Size(870, 354);
44             
45         }
46     }
原文地址:https://www.cnblogs.com/jswjia/p/4625660.html