重写TextBox重绘控件
当BorderStyle为FixedSingle时:可以设置TextBox边框颜;可以设置当鼠标Over或Leave控件的时候,TextBox边框颜变化,以及是否启用这种HotTrack
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace BenSoftCN.WinForms.UI
{
[ToolboxItem(true)]
public class TextBoxXP : System.Windows.Forms.TextBox
{
/// <summary>
/// 获得当前进程,以便重绘控件
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
[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);
/// <summary>
/// 是否启用热点效果
/// </summary>
private bool _HotTrack = true ;
/// <summary>
/// 边框颜
textstyle
/// </summary>
private Color _BorderColor =
Color.FromArgb(0xA7,0xA6,0xAA);
/// <summary>
/// 热点边框颜
/
// </summary>
private Color _HotColor =
Color.FromArgb(0x33,0x5E,0xA8);
/// <summary>
/// 是否鼠标MouseOver状态
/// </summary>
private bool _IsMouseOver = false ;
#region 属性
/// <summary>
/// 是否启用热点效果
/// </summary>
[ Category("行为"),
Description("获得或设置一个值,指示当鼠标经过控件时控件边框是否发生变化。只在控件的BorderStyle为FixedSingle时有效"),
DefaultValue(true)]
public bool HotTrack
{
get
{
return this._HotTrack ;
}
set
{
this._HotTrack = value ;
//在该值发生变化时重绘控件,下同
//在设计模式下,更改该属性时,如果不调用该语句,
//则不能立即看到设计试图中该控件相应的变化
this.Invalidate();
}
}
/// <summary>
/// 边框颜
/// </summary>
[ Category("外观"),
Description("获得或设置控件的边框颜"),
DefaultValue(typeof(Color),"#A7A6AA")]
public Color BorderColor
{
get
{
return this._BorderColor;
}
set
{
this._BorderColor = value;
this.Invalidate();
}
}
/// <summary>
/// 热点时边框颜
/// </summary>
[ Category("外观"),
Description("获得或设置当鼠标经过控件时控件的边框颜。只在控件的BorderStyle为FixedSingle时有效"),
DefaultValue(typeof(Color),"#335EA8")]
public Color HotColor
{
get
{
return this._HotColor;
}
set
{
this._HotColor = value;
this.Invalidate();
}
}
#endregion 属性
/
// <summary>
///
/// </summary>
public TextBoxXP():base()
{
}
/// <summary>
/// 鼠标移动到该控件上时
/// </summary>
/// <param name="e"></param>
protected override void OnMouseMove(MouseEventArgs e)
{
//鼠标状态
this._IsMouseOver = true ;
//如果启用HotTrack,则开始重绘
//如果不加判断这里不加判断,则当不启用HotTrack,            //鼠标在控件上移动时,控件边框会不断重绘,
//导致控件边框闪烁。下同
//谁有更好的办法?Please tell me , Thanks。
if(this._HotTrack)
{
//重绘
this.Invalidate();