diff --git a/Bin/net40/SunnyUI.dll b/Bin/net40/SunnyUI.dll index 4319e8fe..36d05f2b 100644 Binary files a/Bin/net40/SunnyUI.dll and b/Bin/net40/SunnyUI.dll differ diff --git a/Bin/net462/SunnyUI.dll b/Bin/net462/SunnyUI.dll index 98d989e6..9ce2f3f0 100644 Binary files a/Bin/net462/SunnyUI.dll and b/Bin/net462/SunnyUI.dll differ diff --git a/Bin/net5.0-windows/SunnyUI.dll b/Bin/net5.0-windows/SunnyUI.dll index 94d183e4..2d857f95 100644 Binary files a/Bin/net5.0-windows/SunnyUI.dll and b/Bin/net5.0-windows/SunnyUI.dll differ diff --git a/Bin/net5.0-windows/ref/SunnyUI.dll b/Bin/net5.0-windows/ref/SunnyUI.dll index 82b65078..a9f45616 100644 Binary files a/Bin/net5.0-windows/ref/SunnyUI.dll and b/Bin/net5.0-windows/ref/SunnyUI.dll differ diff --git a/Bin/netcoreapp3.1/SunnyUI.dll b/Bin/netcoreapp3.1/SunnyUI.dll index c1efa907..203fda07 100644 Binary files a/Bin/netcoreapp3.1/SunnyUI.dll and b/Bin/netcoreapp3.1/SunnyUI.dll differ diff --git a/SunnyUI/Controls/UIEdit.cs b/SunnyUI/Controls/UIEdit.cs index ca3a34f3..fe4df317 100644 --- a/SunnyUI/Controls/UIEdit.cs +++ b/SunnyUI/Controls/UIEdit.cs @@ -21,6 +21,7 @@ using System; using System.ComponentModel; +using System.Drawing; using System.Windows.Forms; namespace Sunny.UI @@ -45,6 +46,7 @@ namespace Sunny.UI base.ForeColor = UIFontColor.Primary; Width = 150; base.MaxLength = 32767; + JoinEvents(true); } private string watermark; @@ -56,7 +58,87 @@ namespace Sunny.UI set { watermark = value; - Win32.User.SendMessage(Handle, 0x1501, (int)IntPtr.Zero, value); + Invalidate(); + //Win32.User.SendMessage(Handle, 0x1501, (int)IntPtr.Zero, value); + } + } + + private Font oldFont; + private Boolean waterMarkTextEnabled; + + private Color _waterMarkColor = Color.Gray; + public Color WaterMarkColor + { + get => _waterMarkColor; + set + { + _waterMarkColor = value; + Invalidate(); + } + } + + protected override void OnCreateControl() + { + base.OnCreateControl(); + WaterMark_Toggle(null, null); + } + + protected override void OnPaint(PaintEventArgs args) + { + // Use the same font that was defined in base class + Font drawFont = new Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit); + //Create new brush with gray color or + SolidBrush drawBrush = new SolidBrush(WaterMarkColor);//use Water mark color + //Draw Text or WaterMark + args.Graphics.DrawString((waterMarkTextEnabled ? Watermark : Text), drawFont, drawBrush, new PointF(0.0F, 0.0F)); + base.OnPaint(args); + } + + private void JoinEvents(Boolean join) + { + if (join) + { + TextChanged += WaterMark_Toggle; + LostFocus += WaterMark_Toggle; + FontChanged += WaterMark_FontChanged; + } + } + + private void WaterMark_Toggle(object sender, EventArgs args) + { + if (Text.Length <= 0) + EnableWaterMark(); + else + DisableWaterMark(); + } + + private void EnableWaterMark() + { + //Save current font until returning the UserPaint style to false (NOTE: It is a try and error advice) + oldFont = new Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit); + //Enable OnPaint event handler + SetStyle(ControlStyles.UserPaint, true); + waterMarkTextEnabled = true; + //OnPaint right now + Refresh(); + } + + private void DisableWaterMark() + { + //Disable OnPaint event handler + waterMarkTextEnabled = false; + SetStyle(ControlStyles.UserPaint, false); + //Return back oldFont if existed + if (oldFont != null) + Font = new Font(oldFont.FontFamily, oldFont.Size, oldFont.Style, oldFont.Unit); + } + + private void WaterMark_FontChanged(object sender, EventArgs args) + { + if (waterMarkTextEnabled) + { + oldFont = new Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit); + Refresh(); } } diff --git a/SunnyUI/Controls/UITextBox.cs b/SunnyUI/Controls/UITextBox.cs index 42061f78..3ad11fe4 100644 --- a/SunnyUI/Controls/UITextBox.cs +++ b/SunnyUI/Controls/UITextBox.cs @@ -132,10 +132,7 @@ namespace Sunny.UI edit.Enabled = Enabled; } - public override bool Focused - { - get => edit.Focused; - } + public override bool Focused => edit.Focused; [DefaultValue(false)] [Description("激活时选中全部文字"), Category("SunnyUI")] @@ -324,6 +321,14 @@ namespace Sunny.UI set => edit.Watermark = value; } + [DefaultValue(typeof(Color), "Gray")] + [Description("水印文字颜色"), Category("SunnyUI")] + public Color WatermarkColor + { + get => edit.WaterMarkColor; + set => edit.WaterMarkColor = value; + } + public void SelectAll() { edit.Focus();