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)
            {
                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);
            }

        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM