自定义文本框 鼠标经过 边框颜色变化

来源:互联网 发布:淘宝返的积分在哪里 编辑:程序博客网 时间:2024/05/21 10:48
using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using System.ComponentModel;using System.Drawing;namespace mmDesk.MainForm.Controls{    public class LTextBox : TextBox    {        public LTextBox()        {            //设置允许透明背景色            SetStyle(ControlStyles.SupportsTransparentBackColor, true);            this.LineType = BorderType.None;        }        protected override CreateParams CreateParams        {            get            {                System.Windows.Forms.CreateParams ps = base.CreateParams;                ps.ExStyle = ps.ExStyle | 0x20;                return ps;            }        }        #region 预先定义消息        [Flags]        public enum BorderType : int        {            /// <summary>            /// 不绘制            /// </summary>            None = 0,            /// <summary>            /// 左侧            /// </summary>            Left = 1,            /// <summary>            /// 上侧            /// </summary>            Top = 2,            /// <summary>            /// 右侧            /// </summary>            Right = 4,            /// <summary>            /// 底部            /// </summary>            Bottom = 8,            /// <summary>            /// 全部            /// </summary>            All=16        }        #endregion        #region 属性        BorderType _lineType;        /// <summary>        /// 要绘制的边框        /// </summary>        [Description("要绘制的边框"), DefaultValue(BorderType.None)]        public BorderType LineType        {            get            { return _lineType; }            set { _lineType = value; this.Invalidate(); }        }         Color _lineColor;        /// <summary>        /// 绘制的边框颜色        /// </summary>        [Description("绘制的边框颜色")]        public Color LineColor        {            get            {                return _lineColor;            }            set            {                _lineColor = value;                this.Invalidate();            }        }        int _lineWidth;         /// <summary>        /// 绘制的边框宽度        /// </summary>        [Description("绘制的边框宽度")]        public int LineWidth        {            get            {                return _lineWidth;            }            set            {                _lineWidth = value;                this.Invalidate();            }        }        #endregion        [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);        /// <summary>        /// 重写消息处理        /// </summary>        /// <param name="m">消息</param>        protected override void WndProc(ref Message m)        {            base.WndProc(ref m);            IntPtr hDC = GetWindowDC(m.HWnd);            if (hDC.ToInt32() == 0) return;            if (this.BorderStyle == BorderStyle.None) return;            switch (m.Msg)            {                case 0xf:                case 0x85:                case 0x133:                    Graphics g = Graphics.FromHdc(hDC);                    Pen p = new Pen(LineColor);                    p.Width = LineWidth;                    Pen b = new Pen(BackColor);                    g.DrawLine(((LineType & BorderType.Bottom) == BorderType.Bottom) ? p : b, 0, this.Size.Height - 1, this.Size.Width - 1, this.Size.Height - 1);                    g.DrawLine(((LineType & BorderType.Left) == BorderType.Left) ? p : b, 0, 0, 0, this.Size.Height - 1);                    g.DrawLine(((LineType & BorderType.Right) == BorderType.Right) ? p : b, this.Size.Width - 1, 0, this.Size.Width - 1, this.Size.Height - 1);                    g.DrawLine(((LineType & BorderType.Top) == BorderType.Top) ? p : b, 0, 0, this.Size.Width - 1, 0);                    g.DrawRectangle(((LineType & BorderType.All) == BorderType.All) ? p : b, 0, 0, this.Size.Width - 1, this.Size.Height - 1);                    g.Dispose();                    m.Result = IntPtr.Zero;                    ReleaseDC(m.HWnd, hDC);                    break;            }        }    }}

原创粉丝点击