winform中文本框样式只显示下面一条线
2个回答
展开全部
这个需要你自己重写TextBox了,因为.net里面的文本框只是简单地包装了系统的文本框,并没有提供任何多余的功能.
给个例子吧,这是很久之前我写的一个...但是几乎没用过.
public class LTextBox : TextBox
{
public LTextBox()
{
//设置允许透明背景色
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.LineType = BorderType.None;
}
protected override CreateParams CreateParams
{
get
{
System.Windows.Forms.CreateParams ps = base.CreateParams;
ps.ExStyle = ps.ExStyle | 0x20;
return ps;
}
}
#region 预先定义消息
[Flags]
public enum BorderType : int
{
/// <summary>
/// 不绘制
/// </summary>
None = 0,
/// <summary>
/// 左侧
/// </summary>
Left = 1,
/// <summary>
/// 上侧
/// </summary>
Top = 2,
/// <summary>
/// 右侧
/// </summary>
Right = 4,
/// <summary>
/// 底部
/// </summary>
Bottom = 8
}
#endregion
#region 属性
BorderType _lineType;
/// <summary>
/// 要绘制的边框
/// </summary>
[Description("要绘制的边框"), DefaultValue(BorderType.None)]
public BorderType LineType
{
get
{ return _lineType; }
set { _lineType = value; this.Invalidate(); }
}
Color _lineColor;
/// <summary>
/// 绘制的边框颜色
/// </summary>
[Description("绘制的边框颜色")]
public Color LineColor { get
{
return _lineColor;
}
set
{
_lineColor = value;
this.Invalidate();
}
}
#endregion
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
/// <summary>
/// 重写消息处理
/// </summary>
/// <param name="m">消息</param>
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0) return;
if (this.BorderStyle == BorderStyle.None) return;
switch (m.Msg)
{
case 0xf:
case 0x85:
case 0x133:
Graphics g = Graphics.FromHdc(hDC);
Pen p = new Pen(LineColor);
Pen b = new Pen(BackColor);
g.DrawLine(((LineType & BorderType.Bottom) == BorderType.Bottom) ? p : b, 0, this.Size.Height - 1, this.Size.Width - 1, this.Size.Height - 1);
g.DrawLine(((LineType & BorderType.Left) == BorderType.Left) ? p : b, 0, 0, 0, this.Size.Height - 1);
g.DrawLine(((LineType & BorderType.Right) == BorderType.Right) ? p : b, this.Size.Width - 1, 0, this.Size.Width - 1, this.Size.Height - 1);
g.DrawLine(((LineType & BorderType.Top) == BorderType.Top) ? p : b, 0, 0, this.Size.Width - 1, 0);
g.Dispose();
m.Result = IntPtr.Zero;
ReleaseDC(m.HWnd, hDC);
break;
}
}
给个例子吧,这是很久之前我写的一个...但是几乎没用过.
public class LTextBox : TextBox
{
public LTextBox()
{
//设置允许透明背景色
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.LineType = BorderType.None;
}
protected override CreateParams CreateParams
{
get
{
System.Windows.Forms.CreateParams ps = base.CreateParams;
ps.ExStyle = ps.ExStyle | 0x20;
return ps;
}
}
#region 预先定义消息
[Flags]
public enum BorderType : int
{
/// <summary>
/// 不绘制
/// </summary>
None = 0,
/// <summary>
/// 左侧
/// </summary>
Left = 1,
/// <summary>
/// 上侧
/// </summary>
Top = 2,
/// <summary>
/// 右侧
/// </summary>
Right = 4,
/// <summary>
/// 底部
/// </summary>
Bottom = 8
}
#endregion
#region 属性
BorderType _lineType;
/// <summary>
/// 要绘制的边框
/// </summary>
[Description("要绘制的边框"), DefaultValue(BorderType.None)]
public BorderType LineType
{
get
{ return _lineType; }
set { _lineType = value; this.Invalidate(); }
}
Color _lineColor;
/// <summary>
/// 绘制的边框颜色
/// </summary>
[Description("绘制的边框颜色")]
public Color LineColor { get
{
return _lineColor;
}
set
{
_lineColor = value;
this.Invalidate();
}
}
#endregion
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
/// <summary>
/// 重写消息处理
/// </summary>
/// <param name="m">消息</param>
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0) return;
if (this.BorderStyle == BorderStyle.None) return;
switch (m.Msg)
{
case 0xf:
case 0x85:
case 0x133:
Graphics g = Graphics.FromHdc(hDC);
Pen p = new Pen(LineColor);
Pen b = new Pen(BackColor);
g.DrawLine(((LineType & BorderType.Bottom) == BorderType.Bottom) ? p : b, 0, this.Size.Height - 1, this.Size.Width - 1, this.Size.Height - 1);
g.DrawLine(((LineType & BorderType.Left) == BorderType.Left) ? p : b, 0, 0, 0, this.Size.Height - 1);
g.DrawLine(((LineType & BorderType.Right) == BorderType.Right) ? p : b, this.Size.Width - 1, 0, this.Size.Width - 1, this.Size.Height - 1);
g.DrawLine(((LineType & BorderType.Top) == BorderType.Top) ? p : b, 0, 0, this.Size.Width - 1, 0);
g.Dispose();
m.Result = IntPtr.Zero;
ReleaseDC(m.HWnd, hDC);
break;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询