yangyin
2025-02-28 baa80d650adebcce70f1113cc1020c6039c159a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace DPumpHydr.WinFrmUI.WenSkin.Controls
{
    public partial class WenLableClickToTextBox : WenControl
    {
        public WenLableClickToTextBox() : base()
        {
            InitializeComponent();
            textBox = new TextBox()
            {
                Text = this.Text,
                Width = this.Width,
                Visible = false
            };
            textBox.KeyDown += (s, e) =>
            {
                if (Keys.Enter == e.KeyCode)
                {
                    textBox.Visible = false;
                    OnTextUpdateBefore(textBox.Text);
                }
            };
            textBox.LostFocus += (s, e) =>
            {
                textBox.Visible = false;
            };
            this.Controls.Add(textBox);
        }
 
        public delegate void TextUpdateEventHandler(object sender, TextUpdateEventArgs e);
        public event TextUpdateEventHandler TextUpdateBefore;
        public event TextUpdateEventHandler TextUpdateChanged;
        private TextBox textBox { get; set; }
 
        [Category("Wen")]
        [Description("是否允许多行输入")]
        [DefaultValue(false)]
        public bool Multiline
        {
            get => textBox.Multiline;
            set
            {
                if (value)
                {
                    textBox.Dock = DockStyle.Fill;
                    textBox.Multiline = true;
                }
                else
                {
                    textBox.Dock = DockStyle.None;
                    textBox.Multiline = false;
                }
            }
        }
        [Category("Wen")]
        [Description("能否弹出输入内容框")]
        [DefaultValue(false)]
        public bool ReadOnly { get => textBox.ReadOnly; set => textBox.ReadOnly = value; }
        [Category("Wen")]
        [Description("获取或设置字符串的垂直对齐方式")]
        [DefaultValue(StringAlignment.Center)]
        public StringAlignment LineAlignment { get; set; } = StringAlignment.Center;
        [Category("Wen")]
        [Description("获取或设置字符串的水平对齐方式")]
        [DefaultValue(StringAlignment.Near)]
        public StringAlignment Alignment { get; set; } = StringAlignment.Near;
        [Category("Wen")]
        [Description("对象绘制的文本超出布局矩形的边缘时被剪裁的方式")]
        [DefaultValue(StringTrimming.EllipsisCharacter)]
        public StringTrimming Trimming { get; set; } = StringTrimming.EllipsisCharacter;
        [Category("Wen")]
        [Description("获取或设置包含格式化信息")]
        [DefaultValue(StringFormatFlags.NoWrap)]
        public StringFormatFlags FormatFlags { get; set; } = StringFormatFlags.NoWrap;
        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle rec = new Rectangle(0, 0, this.Width, this.Height);
            using Brush brush = new SolidBrush(ForeColor);
            e.Graphics.DrawString(this.Text, Font, brush, rec
                    , new StringFormat()
                    {
                        LineAlignment = this.LineAlignment,
                        Alignment = this.Alignment,
                        Trimming = this.Trimming,
                        FormatFlags = this.FormatFlags
                    });
            base.OnPaint(e);
        }
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            if (ReadOnly)
                return;
 
            if (textBox.Visible)
            {
                OnTextUpdateBefore(textBox.Text);
                textBox.Visible = false;
            }
            else
            {
                textBox.Text = this.Text;
                textBox.Width = this.Width - 2;
                textBox.Location = new Point(1, (this.Height - textBox.Height) / 2);
                textBox.Visible = true;
                textBox.Focus();
            }
        }
 
        private void OnTextUpdateBefore(string text)
        {
            TextUpdateEventArgs e = new TextUpdateEventArgs()
            {
                Text = text,
                Continue = true
            };
            TextUpdateBefore?.Invoke(this, e);
            if (e.Continue == true)
            {
                this.Text = textBox.Text;
                TextUpdateChanged?.Invoke(this, e);
            }
        }
    }
    public class TextUpdateEventArgs : EventArgs
    {
        public string Text { get; set; }
        public bool Continue { get; set; }
    }
}