当前位置:主页   - 电脑 - 网站开发 - ASP.Net
Office 2007 风格的TextBox的实现
来源:网络   作者:   更新时间:2012-08-21
收藏此页】    【字号    】    【打印】    【关闭

  简单实现一个Office 2007风格的TextBox。样式如下图。

Office 2007 风格的TextBox的实现

  实现的关键是,在UserControl上面放一个TextBox,TextBox没有Border,Border在UserControl上画出来,处理鼠标事件处理TextBox的颜色变化。

  直接贴上实现代码,有问题大家一起讨论。

public partial class SkinTextBox : UserControl
    {
        private Color borderColor = Color.Black;
        private Color activeColor = Color.White;
        private Color backColorBackup = Color.White;

        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        public event EventHandler TextChanged;

        public Color  BorderColor
        {
            get { return this.borderColor; }
            set
            {
                this.borderColor = value;
                this.ResizeControl();
                this.Invalidate();
            }
        }

        public Color ActiveColor
        {
            get { return this.activeColor; }
            set
            {
                this.activeColor = value;
                this.Invalidate();
            }
        }

        public new Color BackColor
        {
            set
            {
                base.BackColor = value;
                this.backColorBackup = value;
                this.textBox.BackColor = value;
            }
            get
            {
                return base.BackColor;
            }
        }

        public bool Multiline
        {
            get { return this.textBox.Multiline; }
            set { this.textBox.Multiline = value; }
        }

        public TextBox TextBox
        {
            get { return this.textBox; }
        }

        public bool UseSystemPasswordChar
        {
            get { return this.textBox.UseSystemPasswordChar; }
            set { this.textBox.UseSystemPasswordChar = value; }
        }

        public new string Text
        {
            get { return this.textBox.Text; }
            set { this.textBox.Text = value; }
        }

        public SkinTextBox()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
            this.BorderStyle = BorderStyle.None;
            this.ResizeControl();

            this.textBox.LostFocus += textBox_LostFocus;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            this.ResizeControl();
        }

        public void ResizeControl()
        {
            if (!this.textBox.Multiline)
        D   {
                Point location = new Point(1, 1);
                if (!this.AutoSize)
                {
                    location.Y = (this.Height - this.textBox.Height) / 2;
                }
                else
                {
                    if (this.AutoSize)
                        this.Height = this.textBox.Height - 2;
                }

                this.textBox.Location = location;
                this.textBox.Width = this.Width - 3;
            }
            else
            {
                this.textBox.Location = new Point(1, 1);
                this.textBox.Height = this.Height - 2;
                this.textBox.Width = this.Width - 3;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, this.borderColor, ButtonBorderStyle.Solid);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.ResizeControl();
        }

        private void textBox_MouseLeave(object sender, EventArgs e)
        {
            if (!this.textBox.Focused)
            {
                this.textBox.BackColor = this.backColorBackup;
                base.BackColor = this.backColorBackup;
            }
        }

        private void textBox_MouseMove(object sender, MouseEventArgs e)
        {
            this.textBox.BackColor = this.activeColor;
            base.BackColor = this.activeColor;
        }

        private void textBox_LostFocus(object sender, EventArgs e)
        {
            this.textBox.BackColor = this.backColorBackup;
            base.BackColor = this.backColorBackup;
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (this.TextChanged != null)
                this.TextChanged(sender, e);
        }
    }

其它资源
来源声明

版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明