/// <summary> /// WinAPI 的摘要说明。 /// </summary> public class WinAPI { /// <summary> /// 重画窗口 /// </summary> public const int WM_PAINT = 0x000F; /// <summary> /// 当一个编辑型控件将要被绘制时发送此消息给它的父窗口;通过响应这条消息,所有者窗口可以通过使用给定的相关显示设备的句柄来设置编辑框的文本和背景颜色 /// </summary> public const int WM_CTLCOLOREDIT = 0x0133; /// <summary> /// 获得焦点 /// </summary> public const int WM_SETFOCUS = 0x007; /// <summary> /// 获取整个控件的设备场景 /// </summary> /// <param name="hwnd">将获取其设备场景的窗口</param> /// <returns></returns> [DllImport ("User32.dll")] public static extern IntPtr GetWindowDC(IntPtr hWnd); /// <summary> /// 释放由调用GetDC或GetWindowDC函数获取的指定设备场景。它对类或私有设备场景无效(但这样的调用不会造成损害) /// </summary> /// <param name="hwnd">要释放的设备场景相关的窗口句柄</param> /// <param name="hdc">要释放的设备场景句柄</param> /// <returns></returns> [DllImport("user32.dll")] public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); public WinAPI() { // // TODO: 在此处添加构造函数逻辑 // }// end }// end class
public class TextBox:System.Windows.Forms.TextBox { protected override void WndProc( ref Message m ) { base.WndProc (ref m); switch( m.Msg ) { case WinAPI.WM_CTLCOLOREDIT: goto case WinAPI.WM_PAINT; case WinAPI.WM_PAINT: IntPtr hDC = WinAPI.GetWindowDC( this.Handle ); //检查获取对象是否成功 if ( hDC.ToInt32() != 0 ) { using( Graphics g = Graphics.FromHdc( hDC ) ) { DrawTextBox( g ); g.Dispose(); }// end using }// end if //返回结果 m.Result = IntPtr.Zero; //释放资源 WinAPI.ReleaseDC(m.HWnd, hDC); break; }// end swithc }// end void//protected virtual void DrawTextBox( Graphics g ) /// <summary> /// 绘制文本框 /// </summary> /// <param name="g">绘图对象</param> private void DrawTextBox( Graphics g ) { switch( this._BorderStyle) { case TextBorderStyle.None: DrawNoneBorder( g ); break; case TextBorderStyle.XPStyle: DrawXPStyle( g ); break; case TextBorderStyle.Macgic: DrawMacgic( g ); break; case TextBorderStyle.FixedSingle: DrawFixedSingle( g ); break; case TextBorderStyle.Line3D: DrawLine3D( g ); break; case TextBorderStyle.Dashed: DrawDashed( g ); break; case TextBorderStyle.BottomLine: DrawBottomLine( g ); break; case TextBorderStyle.BottomDashed: DrawBottomDashed( g ); break; default: DrawLine3D( g ); break; }// end switch }// end void /// <summary> /// 绘制只有下边框的文本框 /// </summary> /// <param name="g"></param> private void DrawBottomLine( Graphics g ) { Pen p = new Pen( this.BackColor,2 ); g.DrawRectangle( p,1,1,this.Width - 2,this.Height - 2 ); p = new Pen( this._BorderColor,1); g.DrawLine( p,0,this.Height - 1,this.Width,this.Height - 1 ); p.Dispose(); }// end void }
/// 边框样式
/// </summary>
public enum TextBorderStyle
{
/// <summary>
/// 无边框
/// </summary>
None,
/// <summary>
/// XP样式的边框
/// </summary>
XPStyle,
/// <summary>
/// 单行边框
/// </summary>
FixedSingle,
/// <summary>
/// 3维边框
/// </summary>
Line3D,
/// <summary>
/// 点线边框
/// </summary>
Dashed,
/// <summary>
/// 控件无边框,当光标移动到控件上时显示边框
/// </summary>
Macgic,
/// <summary>
/// 只显示下边框,下边框用实线表示
/// </summary>
BottomLine,
/// <summary>
/// 只显示下边框,下边框线用虚线表示
/// </summary>
BottomDashed
}// end enum
/// WinAPI 的摘要说明。
/// </summary>
public class WinAPI
{
/// <summary>
/// 重画窗口
/// </summary>
public const int WM_PAINT = 0x000F; /// <summary>
/// 当一个编辑型控件将要被绘制时发送此消息给它的父窗口;通过响应这条消息,所有者窗口可以通过使用给定的相关显示设备的句柄来设置编辑框的文本和背景颜色
/// </summary>
public const int WM_CTLCOLOREDIT = 0x0133; /// <summary>
/// 获得焦点
/// </summary>
public const int WM_SETFOCUS = 0x007;
/// <summary>
/// 获取整个控件的设备场景
/// </summary>
/// <param name="hwnd">将获取其设备场景的窗口</param>
/// <returns></returns>
[DllImport ("User32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd); /// <summary>
/// 释放由调用GetDC或GetWindowDC函数获取的指定设备场景。它对类或私有设备场景无效(但这样的调用不会造成损害)
/// </summary>
/// <param name="hwnd">要释放的设备场景相关的窗口句柄</param>
/// <param name="hdc">要释放的设备场景句柄</param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
public WinAPI()
{
//
// TODO: 在此处添加构造函数逻辑
//
}// end
}// end class
{
protected override void WndProc( ref Message m )
{
base.WndProc (ref m);
switch( m.Msg )
{
case WinAPI.WM_CTLCOLOREDIT:
goto case WinAPI.WM_PAINT;
case WinAPI.WM_PAINT:
IntPtr hDC = WinAPI.GetWindowDC( this.Handle );
//检查获取对象是否成功
if ( hDC.ToInt32() != 0 )
{
using( Graphics g = Graphics.FromHdc( hDC ) )
{
DrawTextBox( g );
g.Dispose();
}// end using }// end if
//返回结果
m.Result = IntPtr.Zero;
//释放资源
WinAPI.ReleaseDC(m.HWnd, hDC);
break;
}// end swithc
}// end void//protected virtual void DrawTextBox( Graphics g )
/// <summary>
/// 绘制文本框
/// </summary>
/// <param name="g">绘图对象</param>
private void DrawTextBox( Graphics g )
{
switch( this._BorderStyle)
{
case TextBorderStyle.None:
DrawNoneBorder( g );
break;
case TextBorderStyle.XPStyle:
DrawXPStyle( g );
break;
case TextBorderStyle.Macgic:
DrawMacgic( g );
break;
case TextBorderStyle.FixedSingle:
DrawFixedSingle( g );
break;
case TextBorderStyle.Line3D:
DrawLine3D( g );
break;
case TextBorderStyle.Dashed:
DrawDashed( g );
break;
case TextBorderStyle.BottomLine:
DrawBottomLine( g );
break;
case TextBorderStyle.BottomDashed:
DrawBottomDashed( g );
break;
default:
DrawLine3D( g );
break;
}// end switch
}// end void
/// <summary>
/// 绘制只有下边框的文本框
/// </summary>
/// <param name="g"></param>
private void DrawBottomLine( Graphics g )
{
Pen p = new Pen( this.BackColor,2 );
g.DrawRectangle( p,1,1,this.Width - 2,this.Height - 2 );
p = new Pen( this._BorderColor,1);
g.DrawLine( p,0,this.Height - 1,this.Width,this.Height - 1 );
p.Dispose();
}// end void
}