using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms;namespace JcsControlLibrary { public partial class JcsLineTextBox : TextBox { public JcsLineTextBox() { InitializeComponent(); this.Width = 100; this.BorderStyle = BorderStyle.None; } private Color _linecolor = Color.Black ; /// <summary> /// 线条颜色 /// </summary> public Color LineColor { get { return this._linecolor; } set { this._linecolor = value; this.Invalidate(); } } private const int WM_PAINT = 0xF; protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_PAINT) { DrawLine(); } } private void DrawLine() { Graphics g = this.CreateGraphics(); using(Pen p = new Pen(this._linecolor )) { g.DrawLine(p,0,this.Height -1 ,this.Width ,this.Height -1); } } } } 顺便顶一下
不用重写, 搞成没边框的, 下面加个做成线的label
WndProc(ref Message m)有什么用 ,什么时候需要重写
为什么重写OnPaint不行呢? using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing;namespace MyTextBox { public class LineTextBox:TextBox { public LineTextBox()
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;namespace JcsControlLibrary
{
public partial class JcsLineTextBox : TextBox
{
public JcsLineTextBox()
{
InitializeComponent();
this.Width = 100;
this.BorderStyle = BorderStyle.None;
}
private Color _linecolor = Color.Black ;
/// <summary>
/// 线条颜色
/// </summary>
public Color LineColor
{
get
{
return this._linecolor;
}
set
{
this._linecolor = value;
this.Invalidate();
}
}
private const int WM_PAINT = 0xF;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
DrawLine();
}
}
private void DrawLine()
{
Graphics g = this.CreateGraphics();
using(Pen p = new Pen(this._linecolor ))
{
g.DrawLine(p,0,this.Height -1 ,this.Width ,this.Height -1);
}
}
}
}
顺便顶一下
,什么时候需要重写
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;namespace MyTextBox
{
public class LineTextBox:TextBox
{
public LineTextBox()
{ this.BorderStyle = BorderStyle.None;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Black, new Point(0, this.Height - 1), new Point(this.Width, this.Height - 1));
base.OnPaint(e);
}
}
}
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
DrawLine();
}
}
这块代码搞不懂,以前没有用过这个WndProc方法
textBox1.BackColor = this.BackColor;
textBox1.Refresh();
Graphics gph = textBox1.CreateGraphics();
Rectangle Rec = textBox1.ClientRectangle;
gph.DrawLine(new Pen(Color.Black), new Point(0, Rec.Height-1), new Point(Rec.Width, Rec.Height-1));恢复textBox默认格式 textBox1.BorderStyle = BorderStyle.Fixed3D;
textBox1.BackColor =SystemColors.Window;
textBox1.Text = "";
例如:
.txt
{
border-width: 1px;
border-style: none none solid none;
height:20px;
text-align:left;
}
第二步:添加From_Paint事件;
第三步:看代码;
private void From_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black, 1.0f);
foreach (Control ctr in this.Controls)
{ if (ctr is TextBox)
{
g.DrawLine(pen, ctr.Location.X, ctr.Location.Y + 14, ctr.Location.X + ctr.Width, ctr.Location.Y + 14);
}
}
pen.Dispose(); }