* UITextBox: 重写水印文字,解决不同背景色下泛白的问题

This commit is contained in:
Sunny 2022-06-23 10:54:12 +08:00
parent da85d3ee34
commit 06f7dfcc0f
3 changed files with 137 additions and 5 deletions

View File

@ -140,6 +140,14 @@ namespace Sunny.UI
set => edit.WaterMarkColor = value; set => edit.WaterMarkColor = value;
} }
[DefaultValue(typeof(Color), "Gray")]
[Description("水印文字激活颜色"), Category("SunnyUI")]
public Color WatermarkActiveColor
{
get => edit.WaterMarkActiveForeColor;
set => edit.WaterMarkActiveForeColor = value;
}
private UIDropDown itemForm; private UIDropDown itemForm;
protected UIDropDown ItemForm protected UIDropDown ItemForm

View File

@ -46,6 +46,108 @@ namespace Sunny.UI
base.ForeColor = UIFontColor.Primary; base.ForeColor = UIFontColor.Primary;
Width = 150; Width = 150;
base.MaxLength = 32767; base.MaxLength = 32767;
waterMarkBrush = new SolidBrush(_waterMarkActiveColor);
waterMarkContainer = null;
DrawWaterMark();
this.Enter += new EventHandler(ThisHasFocus);
this.Leave += new EventHandler(ThisWasLeaved);
this.TextChanged += new EventHandler(ThisTextChanged);
}
private void DrawWaterMark()
{
if (this.waterMarkContainer == null && this.TextLength <= 0)
{
waterMarkContainer = new Panel();
waterMarkContainer.Paint += new PaintEventHandler(waterMarkContainer_Paint);
waterMarkContainer.Invalidate();
waterMarkContainer.Click += new EventHandler(waterMarkContainer_Click);
this.Controls.Add(waterMarkContainer);
}
}
private void waterMarkContainer_Paint(object sender, PaintEventArgs e)
{
waterMarkContainer.Location = new Point(2, 0);
waterMarkContainer.Height = this.Height;
waterMarkContainer.Width = this.Width;
waterMarkContainer.Anchor = AnchorStyles.Left | AnchorStyles.Right;
if (this.ContainsFocus)
{
waterMarkBrush = new SolidBrush(this._waterMarkActiveColor);
}
else
{
waterMarkBrush = new SolidBrush(this._waterMarkColor);
}
Graphics g = e.Graphics;
g.DrawString(this._waterMarkText, Font, waterMarkBrush, new PointF(-2f, 1f));//Take a look at that point
}
private void RemoveWaterMark()
{
if (waterMarkContainer != null)
{
Controls.Remove(waterMarkContainer);
waterMarkContainer = null;
}
}
private void ThisHasFocus(object sender, EventArgs e)
{
waterMarkBrush = new SolidBrush(this._waterMarkActiveColor);
if (this.TextLength <= 0)
{
RemoveWaterMark();
DrawWaterMark();
}
}
private void ThisWasLeaved(object sender, EventArgs e)
{
if (this.TextLength > 0)
{
RemoveWaterMark();
}
else
{
Invalidate();
}
}
private void ThisTextChanged(object sender, EventArgs e)
{
if (this.TextLength > 0)
{
RemoveWaterMark();
}
else
{
DrawWaterMark();
}
}
private void waterMarkContainer_Click(object sender, EventArgs e)
{
this.Focus();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawWaterMark();
}
protected override void OnInvalidated(InvalidateEventArgs e)
{
base.OnInvalidated(e);
if (waterMarkContainer != null)
waterMarkContainer.Invalidate();
} }
[Browsable(false), DefaultValue(false)] [Browsable(false), DefaultValue(false)]
@ -60,18 +162,19 @@ namespace Sunny.UI
} }
} }
private string watermark; private Panel waterMarkContainer;
private SolidBrush waterMarkBrush;
private string _waterMarkText = "";
[DefaultValue(null)] [DefaultValue(null)]
public string Watermark public string Watermark
{ {
get => watermark; get => _waterMarkText;
set set
{ {
watermark = value; _waterMarkText = value;
//WaterMark_Toggle(null, null);
Invalidate(); Invalidate();
//Win32.User.SendMessage(Handle, 0x1501, (int)IntPtr.Zero, value);
} }
} }
@ -86,6 +189,18 @@ namespace Sunny.UI
} }
} }
private Color _waterMarkActiveColor = Color.Gray;
public Color WaterMarkActiveForeColor
{
get => _waterMarkActiveColor;
set
{
_waterMarkActiveColor = value;
Invalidate();
}
}
protected override void OnKeyDown(KeyEventArgs e) protected override void OnKeyDown(KeyEventArgs e)
{ {
if (!Multiline) if (!Multiline)

View File

@ -34,6 +34,7 @@
* 2022-03-14: V3.1.1 * 2022-03-14: V3.1.1
* 2022-04-11: V3.1.3 ToolTip * 2022-04-11: V3.1.3 ToolTip
* 2022-06-10: V3.1.9 * 2022-06-10: V3.1.9
* 2022-06-23: V3.2.0
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -486,6 +487,14 @@ namespace Sunny.UI
set => edit.WaterMarkColor = value; set => edit.WaterMarkColor = value;
} }
[DefaultValue(typeof(Color), "Gray")]
[Description("水印文字激活颜色"), Category("SunnyUI")]
public Color WatermarkActiveColor
{
get => edit.WaterMarkActiveForeColor;
set => edit.WaterMarkActiveForeColor = value;
}
public void SelectAll() public void SelectAll()
{ {
edit.Focus(); edit.Focus();