Winform改变Textbox边框颜⾊(转)
namespace MyTextBoxOne
{
//使⽤时必须把⽂本框的BorderStyle为FixedSingle才能使⽤
//⼀些控件(如TextBox、Button等)是由系统进程绘制,重载OnPaint⽅法将不起作⽤.
//所有这⾥并没有使⽤重载OnPaint⽅法绘制TextBox边框。
public class TextBoxTwo : TextBox
{
//获取的当前进程,以便重绘控件
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
//是否启⽤热点效果
private bool _HotTrack = true;
//边框颜⾊
private Color _BorderColor = Color.Gray;
//热点边框颜⾊
private Color _HotColor = Color.FromArgb(100, 0, 255, 255);
//是否⿏标经过
private bool _IsMouseOver = false;
#region属性
///<summary>
///是否启⽤热点效果
///</summary>
[Category("⾏为"),
Description("获得或设置⼀个值,指⽰当前⿏标经过控件时控件边框是否发⽣变化。只在控件的BorderStyle为FixedString时有效"), DefaultValue(true)]
public bool HotTrack
{
get { return _HotTrack; }
set
{
this._HotTrack = value;
//该值发⽣变化时重绘控件,在设计模式下,更改该属性时,如果不调⽤该语句,则不能⽴即看到设计试图中该控件相应的变化this.Invalidate();
}
}
///<summary>
///边框颜⾊
///</summary>
[Category("外观"),
Description("获得或设置控件的边框颜⾊"),
DefaultValue(typeof(Color), "black")]
public Color BorderColor
{
get { return this._BorderColor; }
set
{
this._BorderColor = value;
this.Invalidate();
}
}
///<summary>
///热点时边框颜⾊
///</summary>
[Category("外观"),
Description("获得或设置当⿏标经过时控件的边框颜⾊。只在控件的BorderStyle为FixedSing时有效"),
DefaultValue(typeof(Color), "red")]
public Color HotColor
{
get { return this._HotColor; }
set
{
this._HotColor = value;
this.Invalidate();
}
}
#endregion
View Code---属性
public TextBoxTwo() : base() {
this.BorderStyle = BorderStyle.FixedSingle;
this.Width = 200;
this.Height = 20;
}
///<summary>
///⿏标移动到该控件上时
///</summary>
protected override void OnMouseMove(MouseEventArgs e)
{
this._IsMouseOver = true;
//如果启⽤HotTrack,则开始重绘,
//如果不加判断,则当⿏标在控件上移动时,控件边框会不断重绘,导致控件边框闪烁。下同if (this._HotTrack)
{
this.Invalidate();
}
base.OnMouseMove(e);
}
///<summary>
///当⿏标从该控件移开时
///</summary>
protected override void OnMouseLeave(EventArgs e)
{
this._IsMouseOver = false;
if (this._HotTrack)
{
this.Invalidate();
}
base.OnMouseLeave(e);
}
/
//<summary>
///控件获得焦点时
///</summary>
protected override void OnGotFocus(EventArgs e)
{
if (this._HotTrack)
{
this.Invalidate();
}
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
if (this._HotTrack)
{
textbox控件边框设置this.Invalidate();
}
base.OnLostFocus(e);
}
///<summary>
///获得操作系统消息
///</summary>
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0xf || m.Msg == 0x133)
{
//拦截系统消息,获得当前控件进程以便重绘。
//
//MSDN:重写 OnPaint 将禁⽌修改所有控件的外观
//那些由 Windows 完成所有绘图的控件(例如 TextBox)从不调⽤他的OnPaint⽅法
//因此将永远不会使⽤⾃定义码。请参见你要修改的特定控件的⽂档,
//查看 OnPaint ⽅法是否可⽤。如果某个控件未将 OnPaint 作为成员⽅法列出,
/
/则你⽆法通过重写此⽅法改变外观。
//MSDN:要了解可⽤的Message.Msg,Message.LParam 和 Message.WParam 值,
//请参考位于 MSND Library 中的 Platform SDK ⽂档参考。可在 Platform SDK ("Core SDK" ⼀节) //下载中包含的 windows.h 头⽂件中到实际常数值,该⽂件也可在 MSDN上到.
IntPtr hDC = GetWindowDC(m.HWnd);
if (hDC.ToInt32() == 0)
return;
//只有在边框为FixedSingle时⾃定义边框样式才有效
if (this.BorderStyle == BorderStyle.FixedSingle)
{
//边框Width为 2个像素
Pen p = new Pen(this._BorderColor, 2);
if (this.HotTrack)
{
if (this.Focused)
{
p.Color = this._HotColor;
}
}
else
{
if (this._IsMouseOver)
{
p.Color = this._HotColor;
}
else
{
p.Color = this._BorderColor;
}
}
//绘制边框
Graphics g = Graphics.FromHdc(hDC);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawRectangle(p, 0, 0, this.Width - 2, this.Height - 2);
p.Dispose();
}
//返回结果
m.Result = IntPtr.Zero;
//释放
ReleaseDC(m.HWnd, hDC);
}
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论