程序员的知识教程库

网站首页 > 教程分享 正文

C# 自定义Textbox控件(c# winform textbox)

henian88 2024-08-16 17:25:18 教程分享 6 ℃ 0 评论

自定义一个带圆角的Textbox,没有用win32 Api,相对比用Api要好一点。注意窗口AutoScaleMode设置为None.

  [DefaultEvent("TextChanged")]
    public partial class BzTextBox : UserControl
    {
        private Color borderColor = Color.MediumSlateBlue;
        private int borderSize = 2;
        private bool underlinedStyle = false;
        private Color borderFocuseColor = Color.HotPink;
        private bool isFocused = false;

        private int borderRadius = 0;

        private Color placeholderColor = Color.DarkGray;
        private string placeholderText = "";
        private bool isPlaceholder = false;
        private bool isPasswordChar = false;
        private string dbFiled = "";

        public BzTextBox()
        {
            InitializeComponent();
        }

        public new event EventHandler TextChanged;

        [Category("BZ Advance")]
        public Color BorderColor
        {
            get
            {
                return borderColor;
            }

            set
            {
                borderColor = value;
                this.Invalidate();
            }
        }
        [Category("BZ Advance")]
        public int BorderSize
        {
            get
            {
                return borderSize;
            }

            set
            {
                borderSize = value;
                this.Invalidate();
            }
        }
        [Category("BZ Advance")]
        public bool UnderlinedStyle
        {
            get
            {
                return underlinedStyle;
            }

            set
            {
                underlinedStyle = value;
                this.Invalidate();
            }
        }
        [Category("BZ Advance")]
        public bool PasswordChar
        {
            get
            {
                return isPasswordChar;
            }
            set
            {
                isPasswordChar = value;
                txt.UseSystemPasswordChar = value;
            }
        }
        [Category("BZ Advance")]
        public bool Multiline
        {
            get
            {
                return txt.Multiline;
            }
            set {
                txt.Multiline = value;
            }
        }
        [Category("BZ Advance")]
        public override Color BackColor
        {
            get
            {
                return base.BackColor;
            }

            set
            {
                base.BackColor = value;
                txt.BackColor = value;
            }
        }
        [Category("BZ Advance")]
        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }

            set
            {
                base.ForeColor = value;
                txt.ForeColor = value;
            }
        }

        [Category("BZ Advance")]
        public override Font Font
        {
            get
            {
                return base.Font;
            }

            set
            {
                base.Font = value;
                txt.Font = value;
                if (this.DesignMode)
                {
                    UpdateControlHeight();
                }
            }
        }
        [Category("BZ Advance")]
        public string Texts
        {
            get
            {
                if (isPlaceholder)
                {
                    return "";
                }
                else
                {
                    return txt.Text;
                }
            }
            set
            {
                txt.Text = value;
                SetPlaceHolder();
            }
        }

        [Category("BZ Advance")]
        public ScrollBars ScrollBars
        {
            get
            {
                return txt.ScrollBars;
            }
            set
            {
                if (Multiline)
                {
                    txt.ScrollBars = value;
                    UpdateControlHeight();
                }
            }
        }
        [Category("BZ Advance")]
        public Color BorderFocuseColor
        {
            get
            {
                return borderFocuseColor;
            }

            set
            {
                borderFocuseColor = value;
            }
        }

        [Category("BZ Advance")]
        public int BorderRadius
        {
            get
            {
                return borderRadius;
            }

            set
            {
                if (value >= 0)
                {
                    borderRadius = value;
                    this.Invalidate();
                }
            }
        }
        [Category("BZ Advance")]
        public Color PlaceHolderColor
        {
            get
            {
                return placeholderColor;
            }

            set
            {
                placeholderColor = value;
                if (isPasswordChar)
                {
                    txt.ForeColor = value;
                }
            }
        }
        [Category("BZ Advance")]
        public string PlaceholderText
        {
            get
            {
                return placeholderText;
            }

            set
            {
                placeholderText = value;
                txt.Text = "";
                SetPlaceHolder();

            }
        }

        [Category("BZ Advance")]
        public string DbFiled
        {
            get
            {
                return dbFiled;
            }

            set
            {
                dbFiled = value;
            }
        }

        private void SetPlaceHolder()
        {
            if (string.IsNullOrWhiteSpace(txt.Text) && placeholderText != "")
            {
                isPlaceholder = true;
                txt.Text = placeholderText;
                txt.ForeColor = placeholderColor;
                if (isPasswordChar)
                {
                    txt.UseSystemPasswordChar = false;
                }
            }
        }

        private void RemovePlaceholder()
        {
            if (isPlaceholder && placeholderText != "")
            {
                isPlaceholder = false;
                txt.Text = "";
                txt.ForeColor = this.ForeColor;
                if (isPasswordChar)
                {
                    txt.UseSystemPasswordChar = true;
                }
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics graphics = e.Graphics;

            if (borderRadius > 1)
            {
                var rectBorderSmooth = this.ClientRectangle;
                var rectBorder = Rectangle.Inflate(rectBorderSmooth, -BorderSize, -BorderSize);
                int smoothSize = BorderSize > 0 ? BorderSize : 1;
                using (GraphicsPath pathBorderSmooth = GetFigurePath(rectBorderSmooth, borderRadius))
                using (GraphicsPath pathBorder = GetFigurePath(rectBorder, borderRadius - BorderSize))
                using (Pen penBorderSmooth = new Pen(this.Parent.BackColor, smoothSize))
                using (Pen penBorder = new Pen(BorderColor, BorderSize))
                {
                    this.Region = new Region(pathBorderSmooth);
                    if (BorderRadius > 15)
                    {
                        SetTextBoxRoundedRegion();
                    }
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    penBorder.Alignment = PenAlignment.Center;
                    if (isFocused)
                    {
                        penBorder.Color = BorderFocuseColor;
                    }
                    if (underlinedStyle)
                    {
                        graphics.DrawPath(penBorderSmooth, pathBorderSmooth);
                        graphics.SmoothingMode = SmoothingMode.None;
                        graphics.DrawLine(penBorder, 0, this.Height - 1, this.Width, this.Height - 1);
                    }
                    else
                    {
                        graphics.DrawPath(penBorderSmooth, pathBorderSmooth);
                        graphics.DrawPath(penBorder, pathBorder);
                    }

                }

            }
            else
            {
                using (Pen penBorder = new Pen(borderColor, BorderSize))
                {
                    this.Region = new Region(this.ClientRectangle);
                    penBorder.Alignment = PenAlignment.Inset;

                    if (!isFocused)
                    {
                        if (underlinedStyle)
                        {
                            graphics.DrawLine(penBorder, 0, this.Height - 1, this.Width, this.Height - 1);
                        }
                        else
                        {
                            graphics.DrawRectangle(penBorder, 0, 0, this.Width - 0.5F, this.Height - 0.5F);
                        }
                    }
                    else
                    {
                        penBorder.Color = borderFocuseColor;
                        if (underlinedStyle)
                        {
                            graphics.DrawLine(penBorder, 0, this.Height - 1, this.Width, this.Height - 1);
                        }
                        else
                        {
                            graphics.DrawRectangle(penBorder, 0, 0, this.Width - 0.5F, this.Height - 0.5F);
                        }
                    }
                }
            }

         
        }

        private void SetTextBoxRoundedRegion()
        {
            GraphicsPath pathText;
            if (Multiline)
            {
                pathText = GetFigurePath(txt.ClientRectangle, borderRadius - borderSize);
                txt.Region = new Region(pathText);
            }
            else
            {
                pathText = GetFigurePath(txt.ClientRectangle, borderSize * 2);
                txt.Region = new Region(pathText);
            }
        }

        private GraphicsPath GetFigurePath(RectangleF rect, float radius)
        {
            GraphicsPath path = new GraphicsPath();
            float curveSize = radius * 2F;
            path.StartFigure();
            path.AddArc(rect.X, rect.Y, curveSize, curveSize, 180, 90);
            path.AddArc(rect.Right - curveSize, rect.Y, curveSize, curveSize, 270, 90);
            path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 90);
            path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 90, 90);
            path.CloseAllFigures();
            return path;
        }

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

        private void UpdateControlHeight()
        {
            if (txt.Multiline == false)
            {
                int txtHeight = TextRenderer.MeasureText("Text", this.Font).Height + 1;
                txt.Multiline = true;
                txt.MinimumSize = new Size(0, txtHeight);
                txt.Multiline = false;
                this.Height = txt.Height + this.Padding.Top + this.Padding.Bottom;
            }
        }

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

        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.OnKeyPress(e);
        }

        private void txt_Click(object sender, EventArgs e)
        {
            this.OnClick(e);
        }

        private void txt_MouseEnter(object sender, EventArgs e)
        {
            this.OnMouseEnter(e);
        }

        private void txt_MouseLeave(object sender, EventArgs e)
        {
            this.OnMouseLeave(e);
        }

        private void txt_Enter(object sender, EventArgs e)
        {
            isFocused = true;
            this.Invalidate();
            RemovePlaceholder();
        }

        private void txt_Leave(object sender, EventArgs e)
        {
            isFocused = false;
            this.Invalidate();
            SetPlaceHolder();
        }
    }

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表