SurTextBox.cs 10.1 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Asa.Face
{
    /// <summary>
    /// 文本控件
    /// </summary>
    [DefaultValue("Text")]
    [DefaultEvent("TextChanged")]
    public partial class SurTextBox : BaseCtl
    {
        private TextBox _txt = new TextBox();
        private bool change = false;
        private string _textHint = "";
        private bool _textExist;

        /// <summary>
        /// 文本控件
        /// </summary>
        public SurTextBox()
        {
            InitializeComponent();
            _txt.BorderStyle = BorderStyle.None;
            _txt.BackColor = Common.BACK_COLOR;
            _txt.ForeColor = Common.FORE_COLOR;
            _txt.TextChanged += Txt_TextChanged;
            _txt.GotFocus += Txt_GotFocus;
            _txt.LostFocus += Txt_LostFocus;
            _txt.KeyDown += Txt_KeyDown;
            _txt.KeyPress += Txt_KeyPress;
            _txt.KeyUp += Txt_KeyUp;
            Controls.Add(_txt);
            SurTextBox_Resize(null, EventArgs.Empty);

            if (_txt.Text.Length == 0)
            {
                _textExist = false;
                _txt.Text = _textHint;
                _txt.ForeColor = Common.BORDER_COLOR;
            }
        }




        //==========事件==========
        /// <summary>
        /// 显示的文本改变时发生
        /// </summary>
        [Browsable(true), Category(""), Description("显示的文本改变时发生")]
        public new event MyEvent.Changed TextChanged;

        [Browsable(true), Category(""), Description("")]
        public new event KeyEventHandler KeyDown;

        [Browsable(true), Category(""), Description("")]
        public new event KeyEventHandler KeyUp;

        [Browsable(true), Category(""), Description("")]
        public new event KeyPressEventHandler KeyPress;



        //==========属性==========
        /// <summary>
        /// 选中的文本起始位置
        /// </summary>
        [Browsable(false), Category(""), Description("选中的文本起始位置")]
        public int SelectionStart { set => _txt.SelectionStart = value; get => _txt.SelectionStart; }

        /// <summary>
        /// 选中的文本长度
        /// </summary>
        [Browsable(false), Category(""), Description("选中的文本长度")]
        public int SelectionLength { set => _txt.SelectionLength = value; get => _txt.SelectionLength; }

        /// <summary>
        /// 选中的文本
        /// </summary>
        [Browsable(false), Category(""), Description("选中的文本")]
        public string SelectedText { set => _txt.SelectedText = value; get => _txt.SelectedText; }

        /// <summary>
        /// 控件最大字符数
        /// </summary>
        [Browsable(false), Category(""), Description("控件最大字符数")]
        public int MaxLength { set => _txt.MaxLength = value; get => _txt.MaxLength; }

        /// <summary>
        /// 显示文本只读不可更改
        /// </summary>
        [Browsable(true), Category(""), Description("显示文本只读不可更改"), DefaultValue(false)]
        public bool ReadOnly { set => _txt.ReadOnly = value; get => _txt.ReadOnly; }

        /// <summary>
        /// 显示文本的对齐方式
        /// </summary>
        [Browsable(true), Category(""), Description("显示文本的对齐方式"), DefaultValue(HorizontalAlignment.Left)]
        public HorizontalAlignment TextAlign { set => _txt.TextAlign = value; get => _txt.TextAlign; }

        /// <summary>
        /// 是否为多行文本
        /// </summary>
        [Browsable(true), Category(""), Description("是否为多行文本"), DefaultValue(false)]
        public bool Multiline
        {
            set
            {
                _txt.Multiline = value;
                SurTextBox_Resize(null, EventArgs.Empty);
            }
            get
            {
                return _txt.Multiline;
            }
        }

        /// <summary>
        /// 显示的文本
        /// </summary>
        [Browsable(true), Category(""), Description("显示的文本"), DefaultValue("")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get => base.Text;
            set
            {
                base.Text = value;
                if (value.Length == 0 && Focused)
                {
                    _textExist = false;
                    _txt.Text = _textHint;
                    _txt.ForeColor = Common.BORDER_COLOR;
                }
                else
                {
                    if (!_textExist)
                    {
                        _textExist = true;
                        _txt.ForeColor = Common.FORE_COLOR;
                    }
                }
                if (!change)
                {
                    change = true;
                    _txt.Text = value;
                    change = false;
                }
            }
        }

        /// <summary>
        /// 文本提示
        /// </summary>
        [Browsable(true), Category(""), Description("文本提示"), DefaultValue("")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string TextHint
        {
            get
            {
                return _textHint;
            }
            set
            {
                _textHint = value;
                if (!_textExist)
                    _txt.Text = value;
            }
        }

        /// <summary>
        /// 控件中文本的字体
        /// </summary>
        [Browsable(true), Category(""), Description("控件中文本的字体")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override Font Font
        {
            get => base.Font;
            set
            {
                base.Font = value;
                _txt.Top = BorderWidth + (Height - BorderWidth - BorderWidth - _txt.Height) / 2;
            }
        }

        /// <summary>
        /// 控件是否可用
        /// </summary>
        [Browsable(true), Category(""), Description("控件是否可用"), DefaultValue(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public new bool Enabled
        {
            set
            {
                base.Enabled = value;
                _txt.Enabled = value;
                Refresh();
            }
            get
            {
                return base.Enabled;
            }
        }

        /// <summary>
        /// 用于密码字符
        /// </summary>
        [Browsable(true), Category(""), Description("用于密码字符"), DefaultValue(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public char PasswordChar
        {
            set
            {
                _txt.PasswordChar = value;
            }
            get
            {
                return _txt.PasswordChar;
            }
        }





        /// <summary>
        /// 选中所有文本
        /// </summary>
        public void SelectAll()
        {
            _txt.SelectAll();
        }







        //==========私有==========
        /// <summary>
        /// 计算位置大小
        /// </summary>
        protected override void CalcSize()
        {
            base.CalcSize();

        }

        private void Txt_GotFocus(object sender, EventArgs e)
        {

            _txt.Text = Text;
            _textExist = true;
            _txt.ForeColor = Common.FORE_COLOR;
        }

        private void Txt_LostFocus(object sender, EventArgs e)
        {
            if (_txt.Text.Length == 0)
            {
                _textExist = false;
                _txt.Text = _textHint;
                _txt.ForeColor = Common.BORDER_COLOR;
            }
        }

        private void Txt_TextChanged(object sender, EventArgs e)
        {
            if (!_textExist) return;

            if (!change)
            {
                change = true;
                Text = _txt.Text;
                change = false;
            }
            TextChanged?.Invoke(this);
        }

        private void Txt_KeyDown(object sender, KeyEventArgs e)
        {
            KeyDown?.Invoke(this, e);
        }

        private void Txt_KeyUp(object sender, KeyEventArgs e)
        {
            KeyUp?.Invoke(this, e);
        }

        private void Txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            KeyPress?.Invoke(this, e);
        }

        private void SurTextBox_Paint(object sender, PaintEventArgs e)
        {
            BufferedGraphicsContext current = BufferedGraphicsManager.Current;
            BufferedGraphics bg = current.Allocate(e.Graphics, e.ClipRectangle);
            Graphics g = bg.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.Clear(Common.BACK_COLOR);  //背景

            if (Inside && Enabled)
            {
                if (BorderWidth > 0)
                    g.DrawRectangle(BorderInsidePen, BorderRect);
            }
            else
            {
                if (BorderWidth > 0)
                    g.DrawRectangle(BorderOutsidePen, BorderRect);
            }

            bg.Render();
            bg.Dispose();
        }

        private void SurTextBox_Resize(object sender, EventArgs e)
        {
            if (_txt.Multiline)
            {
                _txt.Left = BorderWidth + 3;
                _txt.Top = BorderWidth + 3;
                _txt.Width = Width - BorderWidth - BorderWidth - 3 - 3;
                _txt.Height = Height - BorderWidth - BorderWidth - 3 - 3;
            }
            else
            {
                _txt.Left = BorderWidth + 3;
                _txt.Width = Width - BorderWidth - BorderWidth - 3 - 3;
                _txt.Top = BorderWidth + (Height - BorderWidth - BorderWidth - _txt.Height) / 2;
            }
        }
    }
}