TextBox控件的多样边框实现

一个很简单的例子,重载TextBox,主要功能:

当BorderStyle为FixedSingle时:可以设置TextBox边框颜色;可以设置当鼠标Over或Leave控件的时候,TextBox边框颜色变化,以及是否启用这种HotTrack。

TextBoxXP.cs

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> 
        
/// 边框颜色 
        
/// </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 ; 
         
        
属性 属性 

        
/// <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(); 
            }
 
            
base.OnMouseMove (e); 
        }
 
        
/// <summary> 
        
/// 当鼠标从该控件移开时 
        
/// </summary> 
        
/// <param name="e"></param> 

        protected override void OnMouseLeave(EventArgs e) 
        

            
this._IsMouseOver = false ; 

            
if(this._HotTrack) 
            

                
//重绘 
                this.Invalidate(); 
            }
 
            
base.OnMouseLeave (e); 
        }
 
         
        
/// <summary> 
        
/// 当该控件获得焦点时 
        
/// </summary> 
        
/// <param name="e"></param> 

        protected override void OnGotFocus(EventArgs e) 
        


            
if(this._HotTrack) 
            

                
//重绘 
                this.Invalidate(); 
            }
 
            
base.OnGotFocus (e); 
        }
 
        
/// <summary> 
        
/// 当该控件失去焦点时 
        
/// </summary> 
        
/// <param name="e"></param> 

        protected override void OnLostFocus(EventArgs e) 
        

            
if(this._HotTrack) 
            

                
//重绘 
                this.Invalidate(); 
            }
 
            
base.OnLostFocus (e); 
        }
 

        
/// <summary> 
        
/// 获得操作系统消息 
        
/// </summary> 
        
/// <param name="m"></param> 

        protected override void WndProc(ref Message m) 
        


            
base.WndProc (ref m); 
            
if (m.Msg==0xf || m.Msg==0x133
            

                
//拦截系统消息,获得当前控件进程以便重绘。 
                
//一些控件(如TextBox、Button等)是由系统进程绘制,重载OnPaint方法将不起作用. 
                
//所有这里并没有使用重载OnPaint方法绘制TextBox边框。 
                
// 
                
//MSDN:重写 OnPaint 将禁止修改所有控件的外观。 
                
//那些由 Windows 完成其所有绘图的控件(例如 Textbox)从不调用它们的 OnPaint 方法, 
                
//因此将永远不会使用自定义代码。请参见您要修改的特定控件的文档, 
                
//查看 OnPaint 方法是否可用。如果某个控件未将 OnPaint 作为成员方法列出, 
                
//则您无法通过重写此方法改变其外观。 
                
// 
                
//MSDN:要了解可用的 Message.Msg、Message.LParam 和 Message.WParam 值, 
                
//请参考位于 MSDN 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为1个像素 
                    System.Drawing.Pen pen = new Pen(this._BorderColor,1) ;; 
                 
                    
if(this._HotTrack) 
                    
{                         
                        
if(this.Focused) 
                        

                            pen.Color 
= this._HotColor ; 
                        }
 
                        
else 
                        

                            
if(this._IsMouseOver) 
                            

                                pen.Color
=this._HotColor; 
                            }
 
                            
else 
                            

                                pen.Color 
= this._BorderColor ; 
                            }
 
                        }
                         
                    }
 
                    
//绘制边框 
                    System.Drawing.Graphics g  = Graphics.FromHdc(hDC);  
                    g.SmoothingMode 
= System.Drawing.Drawing2D.SmoothingMode.AntiAlias;       
                    g.DrawRectangle(pen,
0,0,this.Width-1this.Height-1) ; 
                    pen.Dispose(); 
                }
 
                
//返回结果 
                m.Result = IntPtr.Zero;  
                
//释放 
                ReleaseDC(m.HWnd,hDC); 
            }
 
        }
 

    }
 
}
 

下面的代码同样可以用在ComboBox控件上.

 

// USER32 
[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); 

protected   override   void  WndProc( ref  Message m) 


base.WndProc(ref m); 

IntPtr hDC 
= GetWindowDC(m.HWnd); 
if (hDC.ToInt32() == 0return

if (this.BorderStyle == BorderStyle.None) return

switch (m.Msg) 

case 0xf
case 0x85
case 0x133

Graphics g 
= Graphics.FromHdc(hDC); 

g.DrawRectangle(Pens.Blue, 
00
this.Size.Width - 1
this.Size.Height - 1); 

g.SmoothingMode 
= System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 

g.Dispose(); 
m.Result 
= IntPtr.Zero; 
ReleaseDC(m.HWnd, hDC); 
break
}
 
}
 

 

用下面的代码可以在Windows XP下改变控件边框

using  (Pen p  =   new  Pen( this .BackColor,  3 )) 

g.DrawRectangle(p, 
00
this.Size.Width - 1
this.Size.Height - 1); 
}
 

 

以下的写法可以不闪烁

在Leave方法中,Move方法同理。

 

if  ( this ._HotTrack) 

if (this._IsMouseOver) 

//鼠标状态 
this._IsMouseOver = false

//重绘 
this.Invalidate(); 
}
 
}
 
base .OnMouseLeave(e);   


自由使用,转载注明出处。Ben.Hui<BenSoftCN#Gmail.Com>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值