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; }
|
}
|
}
|