FlatText.cs 5.8 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.Theme
{
    [DefaultValue("Text")]
    [DefaultEvent("TextChanged")]
    public partial class FlatText : FlatBase
    {
        private bool _find;
        private bool _down;
        private bool _over;
        private Rectangle _borderRect = new Rectangle();
        private Rectangle _findRect = new Rectangle();

        public delegate void FindClickDelegate(object sender, string str);


        private const int BORDER_W = 2;
        //private const int FIND_WIDTH = 24;
        private readonly SolidBrush FIND_OVER = new SolidBrush(Common.OVER_COLOR);
        private readonly SolidBrush FIND_DOWN = new SolidBrush(Common.BLUE_COLOR);
        private readonly Pen FIND_FORE = new Pen(Common.FORE_COLOR, 2);

        public FlatText()
        {
            InitializeComponent();
            Font = new Font("宋体", 9f);
            Txt.BackColor = Common.BACK_COLOR;
            Txt.ForeColor = Common.FORE_COLOR;
            CalcSize();
        }


        [Browsable(true), Category(""), Description("查找按钮"), DefaultValue(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool FindButton
        {
            set
            {
                _find = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return _find;
            }
        }

        [Browsable(true), Category(""), Description("显示的文本"), DefaultValue("")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get => base.Text;
            set
            {
                base.Text = value;
                Txt.Text = value;
            }
        }

        [Browsable(true), Category(""), Description("显示的文本的字体")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override Font Font
        {
            set
            {
                base.Font = value;
                Txt.Font = value;
                CalcSize();
                Refresh();
            }
            get
            {
                return base.Font;
            }
        }

        [Browsable(true), Category(""), Description("查找按钮单击事件")]
        public event FindClickDelegate FindClick;

        [Browsable(true), Category(""), Description("文本改变事件")]
        public new event EventHandler TextChanged;







        protected override void CalcSize()
        {
            Txt.Left = BORDER_W + 3;
            Txt.Width = Width - BORDER_W * 2 - 6 - (_find ? Common.RBUTTON_W : 0);
            Txt.Top = (Height - Txt.Height) / 2;
            _borderRect = new Rectangle(BORDER_W / 2, BORDER_W / 2, Width - BORDER_W, Height - BORDER_W);

            if (_find)
            {
                _findRect.X = Width - BORDER_W - Common.RBUTTON_W;
                _findRect.Y = BORDER_W;
                _findRect.Width = Common.RBUTTON_W;
                _findRect.Height = Height - BORDER_W * 2;
            }
        }

        private void FlatText_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.Clear(Common.BACK_COLOR);

            if (Inside)
            {
                if(_down)
                    e.Graphics.FillRectangle(FIND_DOWN, _findRect);
                else if (_over)
                    e.Graphics.FillRectangle(FIND_OVER, _findRect);
                e.Graphics.DrawRectangle(Common.BLUE_PEN, _borderRect);
                e.Graphics.DrawLine(Common.BLUE_PEN, _findRect.X, _findRect.Y, _findRect.X, _findRect.Bottom);

                //e.Graphics.DrawEllipse(FIND_FOCUS, _find.X + 10, _find.Y + _find.Height / 2f - 8, 9, 9);
                //e.Graphics.DrawLine(FIND_FOCUS, _find.X + 11, _find.Y + _find.Height / 2f, _find.X + 5, _find.Y + _find.Height / 2f + 6);
            }
            else
            {
                e.Graphics.DrawRectangle(Common.BORDER_PEN, _borderRect);
            }

            if (_find)
            {
                e.Graphics.DrawEllipse(FIND_FORE, _findRect.X + 10, _findRect.Y + _findRect.Height / 2f - 8, 9, 9);
                e.Graphics.DrawLine(FIND_FORE, _findRect.X + 11, _findRect.Y + _findRect.Height / 2f, _findRect.X + 5, _findRect.Y + _findRect.Height / 2f + 6);
            }

        }

        private void FlatText_MouseDown(object sender, MouseEventArgs e)
        {
            if (_find && _over)
            {
                _down = true;
                Refresh();
            }
        }

        private void FlatText_MouseUp(object sender, MouseEventArgs e)
        {
            if (_find)
            {
                _down = false;
                Refresh();
                if (_over) FindClick?.Invoke(this, Text);
            }
        }

        private void FlatText_MouseMove(object sender, MouseEventArgs e)
        {
            bool bln = _findRect.Contains(e.Location);
            if (bln != _over)
            {
                _over = bln;
                Refresh();
            }
        }

        private void Txt_MouseEnter(object sender, EventArgs e)
        {
            Inside = true;
            Refresh();
        }

        private void Txt_TextChanged(object sender, EventArgs e)
        {
            if (Text == Txt.Text) return;
            Text = Txt.Text;

            TextChanged?.Invoke(this, new EventArgs());
        }


    }
}