Winform改變Textbox邊框顏色


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TextDemo
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent(); 
        }

        /// <summary>
        /// 添加一個繼承自textbox的新類,重寫WndProc方法,在重寫的方法里重繪邊框就可以了
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            borderDrawer borderDrawer1 = new borderDrawer();
            borderDrawer1.DrawBorder(ref m, this.Width, this.Height);
        }
    }



    #region 第一種方法,使用時必須把文本框的BorderStyle為FixedSingle才能使用
    [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.Black;

        /// <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();
            }
            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 - 1, this.Height - 1);
                    pen.Dispose();
                }
                //返回結果 
                m.Result = IntPtr.Zero;
                //釋放 
                ReleaseDC(m.HWnd, hDC);
            }
        }
    } 
    #endregion
 
    /// <summary>
    /// 第二種方法
    /// </summary>
    class borderDrawer : System.Windows.Forms.TextBox
    {
        private Color borderColor = Color.Red;   // 設置默認的邊框顏色
        private static int WM_NCPAINT = 0x0085;    // WM_NCPAINT message
        private static int WM_ERASEBKGND = 0x0014; // WM_ERASEBKGND message
        private static int WM_PAINT = 0x000F;      // WM_PAINT message
        [DllImport("user32.dll")]
        static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgnclip, uint fdwOptions);
        //釋放DC
        [DllImport("user32.dll")]
        static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);
        /// <summary>
        /// 重繪邊框的方法
        /// </summary>
        /// <param name="message"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public void DrawBorder(ref Message message, int width, int height)
        {
            if (message.Msg == WM_NCPAINT || message.Msg == WM_ERASEBKGND ||
                message.Msg == WM_PAINT)
            {
                IntPtr hdc = GetDCEx(message.HWnd, (IntPtr)1, 1 | 0x0020);

                if (hdc != IntPtr.Zero)
                {
                    Graphics graphics = Graphics.FromHdc(hdc);
                    Rectangle rectangle = new Rectangle(0, 0, width, height);
                    ControlPaint.DrawBorder(graphics, rectangle,
                                 borderColor, ButtonBorderStyle.Solid);

                    message.Result = (IntPtr)1;
                    ReleaseDC(message.HWnd, hdc);
                }
            }
        }
    }
}

 


免責聲明!

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



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