SunnyUI/SunnyUI/Controls/UITextBox.cs

1659 lines
50 KiB
C#
Raw Normal View History

2020-05-11 21:11:29 +08:00
/******************************************************************************
* SunnyUI
2025-01-07 22:19:21 +08:00
* CopyRight (C) 2012-2025 ShenYongHua().
2021-02-20 15:45:47 +08:00
* QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com
2020-05-11 21:11:29 +08:00
*
* Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI
*
* SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note.
* 使
******************************************************************************
* : UITextBox.cs
* :
2022-01-05 21:57:47 +08:00
* : V3.1
2020-05-11 21:11:29 +08:00
* : 2020-01-01
*
* 2020-01-01: V2.2.0
* 2020-06-03: V2.2.5
* 2020-09-03: V2.2.7 FocusedSelectAll属性
* 2021-04-15: V3.0.3
* 2021-04-17: V3.0.3
* 2021-04-18: V3.0.3 ShowScrollBar属性
* 2021-06-01: V3.0.4
* 2021-07-18: V3.0.5 Focus可用
* 2021-08-03: V3.0.5 GotFocus和LostFocus事件
* 2021-08-15: V3.0.6
2021-09-07 17:46:27 +08:00
* 2021-09-07: V3.0.6
2021-10-14 22:19:49 +08:00
* 2021-10-14: V3.0.8
2021-10-15 16:43:26 +08:00
* 2021-10-15: V3.0.8
* 2022-01-07: V3.1.0
* 2022-02-16: V3.1.1
* 2022-03-14: V3.1.1
* 2022-04-11: V3.1.3 ToolTip
* 2022-06-10: V3.1.9
* 2022-06-23: V3.2.0
* 2022-07-17: V3.2.1 SelectionChanged事件
* 2022-07-28: V3.2.2 Click和DoubleClick事件的问题
* 2022-09-05: V3.2.3
* 2022-09-16: V3.2.4
* 2022-09-16: V3.2.4 Button可能不显示的问题
* 2022-11-03: V3.2.6
* 2022-11-12: V3.2.8
* 2022-11-12: V3.2.8 MaximumEnabledMinimumEnabledHasMaximumHasMinimum属性
* 2022-11-26: V3.2.9 MouseClickMouseDoubleClick事件
* 2023-02-07: V3.3.1 Tips小红点
* 2023-02-10: V3.3.2 TouchPressClick属性
* 2023-06-14: V3.3.9
* 2023-07-03: V3.3.9 Enabled为false时
* 2023-07-16: V3.4.0 Enabled为false时PasswordChar失效的问题
* 2023-08-17: V3.4.1 Enabled为false时
* 2023-08-24: V3.4.2 Enabled为false时
* 2023-10-25: V3.5.1 DPI下
* 2023-10-25: V3.5.1 线
* 2023-10-26: V3.5.1 SymbolRotate
2023-11-16 22:53:00 +08:00
* 2023-11-16: V3.5.2
* 2023-12-18: V3.6.2
* 2023-12-18: V3.6.2 Tips小红点的位置
* 2023-12-25: V3.6.2 Text的属性编辑器
* 2024-01-13: V3.6.3 Radius时
2024-06-11 21:31:41 +08:00
* 2024-06-11: V3.6.6
* 2024-08-12: V3.6.8
* 2024-08-26: V3.6.9
* 2024-08-27: V3.6.9 AutoSize时根据字体自动调整控件高度
2020-05-11 21:11:29 +08:00
******************************************************************************/
using System;
using System.Collections;
2020-05-11 21:11:29 +08:00
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
2020-05-11 21:11:29 +08:00
using System.Windows.Forms;
namespace Sunny.UI
{
[DefaultEvent("TextChanged")]
[DefaultProperty("Text")]
2025-03-18 22:12:54 +08:00
[Description("输入框控件")]
2024-06-11 21:31:41 +08:00
public partial class UITextBox : UIPanel, ISymbol, IToolTip
2020-05-11 21:11:29 +08:00
{
private readonly UIEdit edit = new UIEdit();
private readonly UIScrollBar bar = new UIScrollBar();
2021-09-07 17:46:27 +08:00
private readonly UISymbolButton btn = new UISymbolButton();
2020-05-11 21:11:29 +08:00
public UITextBox()
{
InitializeComponent();
2022-02-07 10:59:32 +08:00
InitializeComponentEnd = true;
SetStyleFlags(true, true, true);
2022-01-29 16:30:00 +08:00
ShowText = false;
2021-10-14 22:19:49 +08:00
MinimumSize = new Size(1, 16);
2020-05-11 21:11:29 +08:00
edit.AutoSize = false;
edit.Top = (Height - edit.Height) / 2;
2021-05-12 14:44:39 +08:00
edit.Left = 4;
edit.Width = Width - 8;
2020-05-11 21:11:29 +08:00
edit.Text = String.Empty;
edit.BorderStyle = BorderStyle.None;
2021-08-23 13:27:57 +08:00
edit.TextChanged += Edit_TextChanged;
edit.KeyDown += Edit_OnKeyDown;
edit.KeyUp += Edit_OnKeyUp;
edit.KeyPress += Edit_OnKeyPress;
edit.MouseEnter += Edit_MouseEnter;
edit.Click += Edit_Click;
edit.DoubleClick += Edit_DoubleClick;
edit.Leave += Edit_Leave;
edit.Validated += Edit_Validated;
edit.Validating += Edit_Validating;
edit.GotFocus += Edit_GotFocus;
edit.LostFocus += Edit_LostFocus;
2021-08-23 13:27:57 +08:00
edit.MouseLeave += Edit_MouseLeave;
edit.MouseWheel += Edit_MouseWheel;
edit.MouseDown += Edit_MouseDown;
edit.MouseUp += Edit_MouseUp;
edit.MouseMove += Edit_MouseMove;
edit.SelectionChanged += Edit_SelectionChanged;
edit.MouseClick += Edit_MouseClick;
edit.MouseDoubleClick += Edit_MouseDoubleClick;
edit.SizeChanged += Edit_SizeChanged;
edit.FontChanged += Edit_FontChanged;
2021-09-07 17:46:27 +08:00
btn.Parent = this;
btn.Visible = false;
btn.Text = "";
2022-02-26 10:53:40 +08:00
btn.Symbol = 361761;
2021-09-07 17:46:27 +08:00
btn.Top = 1;
btn.Height = 25;
btn.Width = 29;
2021-10-15 16:43:26 +08:00
btn.BackColor = Color.Transparent;
2021-09-07 17:46:27 +08:00
btn.Click += Btn_Click;
2021-10-15 16:43:26 +08:00
btn.Radius = 3;
btn.SymbolOffset = new Point(-1, 1);
2021-09-07 17:46:27 +08:00
2020-05-11 21:11:29 +08:00
edit.Invalidate();
Controls.Add(edit);
fillColor = Color.White;
bar.Parent = this;
bar.Dock = DockStyle.None;
bar.Visible = false;
bar.ValueChanged += Bar_ValueChanged;
bar.MouseEnter += Bar_MouseEnter;
TextAlignment = ContentAlignment.MiddleLeft;
lastEditHeight = edit.Height;
Width = 150;
Height = 29;
editCursor = Cursor;
TextAlignmentChange += UITextBox_TextAlignmentChange;
}
2024-09-17 22:49:54 +08:00
[Browsable(false)]
2024-12-27 21:28:27 +08:00
public override string[] FormTranslatorProperties => null;
2024-09-17 22:49:54 +08:00
private void Edit_FontChanged(object sender, EventArgs e)
{
if (!edit.Multiline)
{
int height = edit.Font.Height;
edit.AutoSize = false;
edit.Height = height + 2;
SizeChange();
}
}
int lastEditHeight = -1;
private void Edit_SizeChanged(object sender, EventArgs e)
{
if (lastEditHeight != edit.Height)
{
lastEditHeight = edit.Height;
SizeChange();
}
}
public override void SetDPIScale()
{
base.SetDPIScale();
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;
edit.SetDPIScale();
}
[Description("开启后可响应某些触屏的点击事件"), Category("SunnyUI")]
[DefaultValue(false)]
public bool TouchPressClick
{
get => edit.TouchPressClick;
set => edit.TouchPressClick = value;
}
private bool _autoSize = false;
public new bool AutoSize
{
get => _autoSize;
set
{
_autoSize = value;
SizeChange();
}
}
private UIButton tipsBtn;
public void SetTipsText(ToolTip toolTip, string text)
{
if (tipsBtn == null)
{
tipsBtn = new UIButton();
tipsBtn.Cursor = System.Windows.Forms.Cursors.Hand;
tipsBtn.Size = new System.Drawing.Size(6, 6);
tipsBtn.Style = Sunny.UI.UIStyle.Red;
tipsBtn.StyleCustomMode = true;
tipsBtn.Text = "";
tipsBtn.Click += TipsBtn_Click;
Controls.Add(tipsBtn);
tipsBtn.Location = new System.Drawing.Point(Width - 8, 2);
tipsBtn.BringToFront();
}
toolTip.SetToolTip(tipsBtn, text);
}
public event EventHandler TipsClick;
private void TipsBtn_Click(object sender, EventArgs e)
{
TipsClick?.Invoke(this, EventArgs.Empty);
}
public void CloseTips()
{
if (tipsBtn != null)
{
tipsBtn.Click -= TipsBtn_Click;
tipsBtn.Dispose();
tipsBtn = null;
}
}
public new event EventHandler MouseDoubleClick;
public new event EventHandler MouseClick;
private void Edit_MouseDoubleClick(object sender, MouseEventArgs e)
{
MouseDoubleClick?.Invoke(this, e);
}
private void Edit_MouseClick(object sender, MouseEventArgs e)
{
MouseClick?.Invoke(this, e);
}
private int scrollBarWidth = 0;
[DefaultValue(0), Category("SunnyUI"), Description("垂直滚动条宽度,最小为原生滚动条宽度")]
public int ScrollBarWidth
{
get => scrollBarWidth;
set
{
scrollBarWidth = value;
SetScrollInfo();
}
}
private int scrollBarHandleWidth = 6;
[DefaultValue(6), Category("SunnyUI"), Description("垂直滚动条滑块宽度,最小为原生滚动条宽度")]
public int ScrollBarHandleWidth
{
get => scrollBarHandleWidth;
set
{
scrollBarHandleWidth = value;
if (bar != null) bar.FillWidth = value;
}
}
private void Edit_SelectionChanged(object sender, UITextBoxSelectionArgs e)
{
SelectionChanged?.Invoke(this, e);
}
public event OnSelectionChanged SelectionChanged;
public void SetButtonToolTip(ToolTip toolTip, string tipText)
{
toolTip.SetToolTip(btn, tipText);
}
protected override void OnContextMenuStripChanged(EventArgs e)
{
base.OnContextMenuStripChanged(e);
if (edit != null) edit.ContextMenuStrip = ContextMenuStrip;
}
2021-10-15 16:43:26 +08:00
/// <summary>
/// 填充颜色,当值为背景色或透明色或空值则不填充
/// </summary>
[Description("填充颜色,当值为背景色或透明色或空值则不填充"), Category("SunnyUI")]
[DefaultValue(typeof(Color), "White")]
public new Color FillColor
{
get
{
return fillColor;
}
set
{
if (fillColor != value)
{
fillColor = value;
Invalidate();
}
AfterSetFillColor(value);
}
}
/// <summary>
/// 字体只读颜色
/// </summary>
[DefaultValue(typeof(Color), "109, 109, 103")]
public Color ForeReadOnlyColor
{
get => foreReadOnlyColor;
set => SetForeReadOnlyColor(value);
}
/// <summary>
/// 边框只读颜色
/// </summary>
[DefaultValue(typeof(Color), "173, 178, 181")]
public Color RectReadOnlyColor
{
get => rectReadOnlyColor;
set => SetRectReadOnlyColor(value);
}
/// <summary>
/// 填充只读颜色
/// </summary>
[DefaultValue(typeof(Color), "244, 244, 244")]
public Color FillReadOnlyColor
{
get => fillReadOnlyColor;
set => SetFillReadOnlyColor(value);
}
2021-09-07 17:46:27 +08:00
private void Btn_Click(object sender, EventArgs e)
{
ButtonClick?.Invoke(this, e);
}
public event EventHandler ButtonClick;
[DefaultValue(29), Category("SunnyUI"), Description("按钮宽度")]
public int ButtonWidth { get => btn.Width; set { btn.Width = Math.Max(20, value); SizeChange(); } }
private bool showButton = false;
2021-09-07 17:46:27 +08:00
[DefaultValue(false), Category("SunnyUI"), Description("显示按钮")]
public bool ShowButton
{
get => showButton;
2021-09-07 17:46:27 +08:00
set
{
showButton = !multiline && value;
if (btn.IsValid()) btn.Visible = showButton;
2021-09-07 17:46:27 +08:00
SizeChange();
}
}
2021-08-23 13:27:57 +08:00
private void Edit_MouseMove(object sender, MouseEventArgs e)
{
MouseMove?.Invoke(this, e);
}
private void Edit_MouseUp(object sender, MouseEventArgs e)
{
MouseUp?.Invoke(this, e);
}
private void Edit_MouseDown(object sender, MouseEventArgs e)
{
MouseDown?.Invoke(this, e);
}
private void Edit_MouseLeave(object sender, EventArgs e)
{
MouseLeave?.Invoke(this, e);
}
2022-06-24 15:04:08 +08:00
/// <summary>
/// 需要额外设置ToolTip的控件
/// </summary>
/// <returns>控件</returns>
public Control ExToolTipControl()
{
return edit;
}
private void Edit_LostFocus(object sender, EventArgs e)
{
LostFocus?.Invoke(this, e);
}
private void Edit_GotFocus(object sender, EventArgs e)
{
GotFocus?.Invoke(this, e);
}
private void Edit_Validating(object sender, CancelEventArgs e)
{
Validating?.Invoke(this, e);
}
public new event MouseEventHandler MouseDown;
public new event MouseEventHandler MouseUp;
public new event MouseEventHandler MouseMove;
public new event EventHandler GotFocus;
public new event EventHandler LostFocus;
public new event CancelEventHandler Validating;
public new event EventHandler Validated;
public new event EventHandler MouseLeave;
public new event EventHandler DoubleClick;
public new event EventHandler Click;
[Browsable(true)]
public new event EventHandler TextChanged;
public new event KeyEventHandler KeyDown;
public new event KeyEventHandler KeyUp;
public new event KeyPressEventHandler KeyPress;
public new event EventHandler Leave;
private void Edit_Validated(object sender, EventArgs e)
{
Validated?.Invoke(this, e);
}
2021-07-18 15:24:32 +08:00
public new void Focus()
{
base.Focus();
edit.Focus();
}
[Browsable(false)]
public UIEdit TextBox => edit;
private void Edit_Leave(object sender, EventArgs e)
{
Leave?.Invoke(this, e);
}
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged(e);
edit.BackColor = GetFillColor();
edit.Visible = true;
edit.Enabled = Enabled;
if (!Enabled)
{
if (NeedDrawDisabledText) edit.Visible = false;
}
}
private bool NeedDrawDisabledText => !Enabled && StyleCustomMode && (ForeDisableColor != Color.FromArgb(109, 109, 103) || FillDisableColor != Color.FromArgb(244, 244, 244));
public override bool Focused => edit.Focused;
2020-09-21 22:12:36 +08:00
[DefaultValue(false)]
[Description("激活时选中全部文字"), Category("SunnyUI")]
public bool FocusedSelectAll
{
get => edit.FocusedSelectAll;
set => edit.FocusedSelectAll = value;
}
private void UITextBox_TextAlignmentChange(object sender, ContentAlignment alignment)
{
if (edit == null) return;
if (alignment == ContentAlignment.TopLeft || alignment == ContentAlignment.MiddleLeft ||
alignment == ContentAlignment.BottomLeft)
edit.TextAlign = HorizontalAlignment.Left;
if (alignment == ContentAlignment.TopCenter || alignment == ContentAlignment.MiddleCenter ||
alignment == ContentAlignment.BottomCenter)
edit.TextAlign = HorizontalAlignment.Center;
if (alignment == ContentAlignment.TopRight || alignment == ContentAlignment.MiddleRight ||
alignment == ContentAlignment.BottomRight)
edit.TextAlign = HorizontalAlignment.Right;
}
private void Edit_DoubleClick(object sender, EventArgs e)
{
DoubleClick?.Invoke(this, e);
}
private void Edit_Click(object sender, EventArgs e)
{
Click?.Invoke(this, e);
}
protected override void OnCursorChanged(EventArgs e)
{
base.OnCursorChanged(e);
edit.Cursor = Cursor;
}
private Cursor editCursor;
private void Bar_MouseEnter(object sender, EventArgs e)
{
editCursor = Cursor;
Cursor = Cursors.Default;
}
private void Edit_MouseEnter(object sender, EventArgs e)
{
Cursor = editCursor;
if (FocusedSelectAll)
{
SelectAll();
}
}
2021-08-23 13:27:57 +08:00
private void Edit_MouseWheel(object sender, MouseEventArgs e)
{
2021-10-16 22:33:10 +08:00
OnMouseWheel(e);
if (bar != null && bar.Visible && edit != null)
{
var si = ScrollBarInfo.GetInfo(edit.Handle);
if (e.Delta > 10)
{
if (si.nPos > 0)
{
ScrollBarInfo.ScrollUp(edit.Handle);
}
}
else if (e.Delta < -10)
{
if (si.nPos < si.ScrollMax)
{
ScrollBarInfo.ScrollDown(edit.Handle);
}
}
}
SetScrollInfo();
}
private void Bar_ValueChanged(object sender, EventArgs e)
{
if (edit != null)
{
ScrollBarInfo.SetScrollValue(edit.Handle, bar.Value);
}
2020-05-11 21:11:29 +08:00
}
private bool multiline = false;
2020-06-03 12:16:27 +08:00
[DefaultValue(false)]
public bool Multiline
{
get => multiline;
set
{
multiline = value;
edit.Multiline = value;
// edit.ScrollBars = value ? ScrollBars.Vertical : ScrollBars.None;
// bar.Visible = multiline;
if (value && Type != UIEditType.String)
{
Type = UIEditType.String;
}
2020-06-03 12:16:27 +08:00
SizeChange();
}
}
private bool showScrollBar;
[DefaultValue(false)]
[Description("显示垂直滚动条"), Category("SunnyUI")]
public bool ShowScrollBar
{
get => showScrollBar;
set
{
value = value && Multiline;
showScrollBar = value;
if (value)
{
edit.ScrollBars = ScrollBars.Vertical;
bar.Visible = true;
}
else
{
edit.ScrollBars = ScrollBars.None;
bar.Visible = false;
}
}
}
[DefaultValue(true)]
public bool WordWarp
{
get => edit.WordWrap;
set => edit.WordWrap = value;
}
public void Select(int start, int length)
{
edit.Focus();
edit.Select(start, length);
}
public void ScrollToCaret()
{
edit.ScrollToCaret();
}
2021-08-23 13:27:57 +08:00
private void Edit_OnKeyPress(object sender, KeyPressEventArgs e)
{
KeyPress?.Invoke(this, e);
}
2021-08-23 13:27:57 +08:00
private void Edit_OnKeyDown(object sender, KeyEventArgs e)
{
2020-06-20 21:00:15 +08:00
if (e.KeyCode == Keys.Enter)
{
DoEnter?.Invoke(this, e);
2020-06-20 21:00:15 +08:00
}
KeyDown?.Invoke(this, e);
}
2020-06-20 21:00:15 +08:00
public event EventHandler DoEnter;
2021-08-23 13:27:57 +08:00
private void Edit_OnKeyUp(object sender, KeyEventArgs e)
{
KeyUp?.Invoke(this, e);
}
[DefaultValue(null)]
[Description("水印文字"), Category("SunnyUI")]
public string Watermark
{
get => edit.Watermark;
set => edit.Watermark = value;
}
[DefaultValue(typeof(Color), "Gray")]
[Description("水印文字颜色"), Category("SunnyUI")]
public Color WatermarkColor
{
get => edit.WaterMarkColor;
set => edit.WaterMarkColor = value;
}
[DefaultValue(typeof(Color), "Gray")]
[Description("水印文字激活颜色"), Category("SunnyUI")]
public Color WatermarkActiveColor
{
get => edit.WaterMarkActiveForeColor;
set => edit.WaterMarkActiveForeColor = value;
}
2020-05-11 21:11:29 +08:00
public void SelectAll()
{
edit.Focus();
2020-05-11 21:11:29 +08:00
edit.SelectAll();
}
internal void CheckMaxMin()
2020-05-11 21:11:29 +08:00
{
edit.CheckMaxMin();
}
2021-08-23 13:27:57 +08:00
private void Edit_TextChanged(object s, EventArgs e)
2020-05-11 21:11:29 +08:00
{
2024-04-16 22:24:18 +08:00
if (IsDisposed) return;
2020-05-15 10:22:35 +08:00
TextChanged?.Invoke(this, e);
2024-11-03 23:24:23 +08:00
if (Multiline) SetScrollInfo();
2020-05-11 21:11:29 +08:00
}
2022-06-27 17:06:11 +08:00
/// <summary>
/// 重载字体变更
/// </summary>
/// <param name="e">参数</param>
2020-05-11 21:11:29 +08:00
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
if (DefaultFontSize < 0 && edit != null)
{
edit.Font = this.Font;
}
Invalidate();
2020-05-11 21:11:29 +08:00
}
2022-06-27 17:06:11 +08:00
/// <summary>
/// 重载控件尺寸变更
/// </summary>
/// <param name="e">参数</param>
2020-05-11 21:11:29 +08:00
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
SizeChange();
2020-05-11 21:11:29 +08:00
}
public void SetScrollInfo()
{
if (bar == null)
{
return;
}
var si = ScrollBarInfo.GetInfo(edit.Handle);
2024-11-03 23:24:23 +08:00
bar.ThreadSafeCall(() =>
{
2024-11-03 23:24:23 +08:00
if (si.ScrollMax > 0)
{
bar.Maximum = si.ScrollMax;
bar.Value = si.nPos;
}
else
{
bar.Maximum = si.ScrollMax;
}
});
}
protected override void OnRadiusChanged(int value)
{
base.OnRadiusChanged(value);
SizeChange();
}
private void SizeChange()
{
2022-02-07 10:59:32 +08:00
if (!InitializeComponentEnd) return;
2021-10-15 16:43:26 +08:00
if (edit == null) return;
if (btn == null) return;
2020-06-03 12:16:27 +08:00
if (!multiline)
{
//单行显示
//AutoSize自动设置高度
if (Dock == DockStyle.None && AutoSize)
{
if (Height != edit.Height + 5)
Height = edit.Height + 5;
}
//根据字体大小编辑框垂直居中
if (edit.Top != (Height - edit.Height) / 2 + 1)
{
edit.Top = (Height - edit.Height) / 2 + 1;
}
2021-10-14 22:19:49 +08:00
int added = Radius <= 5 ? 0 : (Radius - 5) / 2;
if (icon == null && Symbol == 0)
2021-05-12 14:44:39 +08:00
{
edit.Left = 4;
edit.Width = Width - 8;
edit.Left = edit.Left + added;
edit.Width = edit.Width - added * 2;
2021-05-12 14:44:39 +08:00
}
else
{
if (icon != null)
{
edit.Left = 4 + iconSize;
edit.Width = Width - 8 - iconSize - added;
}
else if (Symbol > 0)
{
edit.Left = 4 + SymbolSize;
edit.Width = Width - 8 - SymbolSize - added;
}
2021-05-12 14:44:39 +08:00
}
2021-09-07 17:46:27 +08:00
btn.Left = Width - 2 - ButtonWidth - added;
2021-10-15 16:43:26 +08:00
btn.Top = 2;
btn.Height = Height - 4;
2021-09-07 17:46:27 +08:00
if (ShowButton)
{
edit.Width = edit.Width - btn.Width - 3 - added;
2021-09-07 17:46:27 +08:00
}
if (tipsBtn != null)
{
if (ShowButton)
tipsBtn.Location = new System.Drawing.Point(Width - btn.Width - 10 - added, 2);
else
tipsBtn.Location = new System.Drawing.Point(Width - 8 - added, 2);
}
2020-06-03 12:16:27 +08:00
}
else
{
2021-09-07 17:46:27 +08:00
btn.Visible = false;
2020-06-03 12:16:27 +08:00
edit.Top = 3;
edit.Height = Height - 6;
edit.Left = 4;
edit.Width = Width - 8;
2021-09-07 17:46:27 +08:00
2023-11-16 22:53:00 +08:00
int barWidth = Math.Max(ScrollBarInfo.VerticalScrollBarWidth() + 2, ScrollBarWidth);
bar.Top = 2;
2023-11-16 22:53:00 +08:00
bar.Width = barWidth + 1;
bar.Left = Width - barWidth - 3;
bar.Height = Height - 4;
bar.BringToFront();
SetScrollInfo();
2020-06-03 12:16:27 +08:00
}
2020-05-11 21:11:29 +08:00
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
edit.Focus();
}
public void Clear()
{
edit.Clear();
}
[DefaultValue('\0')]
[Description("密码掩码"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public char PasswordChar
{
get => edit.PasswordChar;
set => edit.PasswordChar = value;
}
[DefaultValue(false)]
[Description("是否只读"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public bool ReadOnly
{
get => isReadOnly;
2020-05-11 21:11:29 +08:00
set
{
isReadOnly = value;
2020-05-11 21:11:29 +08:00
edit.ReadOnly = value;
edit.BackColor = GetFillColor();
Invalidate();
2020-05-11 21:11:29 +08:00
}
}
[Description("输入类型"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
[DefaultValue(UIEditType.String)]
public UIEditType Type
{
get => edit.Type;
set => edit.Type = value;
}
/// <summary>
/// 当InputType为数字类型时能输入的最大值
/// </summary>
[Description("当InputType为数字类型时能输入的最大值。"), Category("SunnyUI")]
[DefaultValue(2147483647D)]
2020-05-11 21:11:29 +08:00
public double Maximum
{
get => edit.MaxValue;
set => edit.MaxValue = value;
}
/// <summary>
/// 当InputType为数字类型时能输入的最小值
/// </summary>
[Description("当InputType为数字类型时能输入的最小值。"), Category("SunnyUI")]
[DefaultValue(-2147483648D)]
2020-05-11 21:11:29 +08:00
public double Minimum
{
get => edit.MinValue;
set => edit.MinValue = value;
}
[DefaultValue(0.00)]
[Description("浮点返回值"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public double DoubleValue
{
get => edit.DoubleValue;
set => edit.DoubleValue = value;
}
[DefaultValue(0)]
2022-08-01 15:33:00 +08:00
[Description("整型返回值"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public int IntValue
{
get => edit.IntValue;
set => edit.IntValue = value;
}
[Description("文本返回值"), Category("SunnyUI")]
[Browsable(true)]
2020-05-11 21:11:29 +08:00
[DefaultValue("")]
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
2020-05-11 21:11:29 +08:00
public override string Text
{
get => edit.Text;
set => edit.Text = value;
}
[Description("浮点数,显示文字小数位数"), Category("SunnyUI")]
[DefaultValue(2)]
public int DecimalPlaces
{
get => edit.DecLength;
set => edit.DecLength = Math.Max(value, 0);
}
2020-05-11 21:11:29 +08:00
[DefaultValue(false)]
2022-08-01 15:33:00 +08:00
[Description("整型或浮点输入时,是否可空显示"), Category("SunnyUI")]
2020-05-11 21:11:29 +08:00
public bool CanEmpty
{
get => edit.CanEmpty;
set => edit.CanEmpty = value;
}
public void Empty()
{
if (edit.CanEmpty)
edit.Text = "";
}
public bool IsEmpty => edit.Text == "";
2022-06-27 17:06:11 +08:00
/// <summary>
/// 重载鼠标按下事件
/// </summary>
/// <param name="e">鼠标参数</param>
2020-05-11 21:11:29 +08:00
protected override void OnMouseDown(MouseEventArgs e)
{
ActiveControl = edit;
}
[DefaultValue(32767)]
2020-05-11 21:11:29 +08:00
public int MaxLength
{
get => edit.MaxLength;
set => edit.MaxLength = Math.Max(value, 1);
}
2022-06-23 23:27:55 +08:00
/// <summary>
/// 设置主题样式
/// </summary>
/// <param name="uiColor">主题样式</param>
2020-05-11 21:11:29 +08:00
public override void SetStyleColor(UIBaseStyle uiColor)
{
base.SetStyleColor(uiColor);
2021-10-15 16:43:26 +08:00
fillColor = uiColor.EditorBackColor;
foreColor = UIFontColor.Primary;
edit.BackColor = GetFillColor();
edit.ForeColor = GetForeColor();
edit.ForeDisableColor = uiColor.ForeDisableColor;
2023-11-16 22:53:00 +08:00
if (bar != null && bar.Style == UIStyle.Inherited)
{
bar.ForeColor = uiColor.PrimaryColor;
bar.HoverColor = uiColor.ButtonFillHoverColor;
bar.PressColor = uiColor.ButtonFillPressColor;
2021-10-15 16:43:26 +08:00
bar.FillColor = fillColor;
scrollBarColor = uiColor.PrimaryColor;
scrollBarBackColor = fillColor;
}
2023-11-17 23:07:19 +08:00
if (btn != null && btn.Style == UIStyle.Inherited)
{
btn.ForeColor = uiColor.ButtonForeColor;
btn.FillColor = uiColor.ButtonFillColor;
btn.RectColor = uiColor.RectColor;
btn.FillHoverColor = uiColor.ButtonFillHoverColor;
btn.RectHoverColor = uiColor.ButtonRectHoverColor;
btn.ForeHoverColor = uiColor.ButtonForeHoverColor;
btn.FillPressColor = uiColor.ButtonFillPressColor;
btn.RectPressColor = uiColor.ButtonRectPressColor;
btn.ForePressColor = uiColor.ButtonForePressColor;
}
2020-05-11 21:11:29 +08:00
}
2023-11-16 22:53:00 +08:00
/// <summary>
/// 滚动条主题样式
/// </summary>
[DefaultValue(true), Description("滚动条主题样式"), Category("SunnyUI")]
public bool ScrollBarStyleInherited
{
get => bar != null && bar.Style == UIStyle.Inherited;
set
{
if (value)
{
if (bar != null) bar.Style = UIStyle.Inherited;
scrollBarColor = UIStyles.Blue.PrimaryColor;
scrollBarBackColor = UIStyles.Blue.EditorBackColor;
}
}
}
protected override void SetForeDisableColor(Color color)
{
base.SetForeDisableColor(color);
edit.ForeDisableColor = color;
}
private Color scrollBarColor = Color.FromArgb(80, 160, 255);
/// <summary>
/// 填充颜色,当值为背景色或透明色或空值则不填充
/// </summary>
2022-05-26 11:03:06 +08:00
[Description("滚动条填充颜色"), Category("SunnyUI")]
[DefaultValue(typeof(Color), "80, 160, 255")]
public Color ScrollBarColor
{
get => scrollBarColor;
set
{
scrollBarColor = value;
bar.HoverColor = bar.PressColor = bar.ForeColor = value;
2023-11-16 22:53:00 +08:00
bar.Style = UIStyle.Custom;
Invalidate();
}
}
private Color scrollBarBackColor = Color.White;
/// <summary>
/// 填充颜色,当值为背景色或透明色或空值则不填充
/// </summary>
2022-05-26 11:03:06 +08:00
[Description("滚动条背景颜色"), Category("SunnyUI")]
[DefaultValue(typeof(Color), "White")]
public Color ScrollBarBackColor
{
get => scrollBarBackColor;
set
{
scrollBarBackColor = value;
bar.FillColor = value;
2023-11-16 22:53:00 +08:00
bar.Style = UIStyle.Custom;
Invalidate();
}
}
2020-05-11 21:11:29 +08:00
protected override void AfterSetForeColor(Color color)
{
base.AfterSetForeColor(color);
edit.ForeColor = GetForeColor();
2020-05-11 21:11:29 +08:00
}
protected override void AfterSetFillColor(Color color)
{
base.AfterSetFillColor(color);
edit.BackColor = GetFillColor();
bar.FillColor = color;
}
protected override void AfterSetFillReadOnlyColor(Color color)
{
base.AfterSetFillReadOnlyColor(color);
edit.BackColor = GetFillColor();
}
protected override void AfterSetForeReadOnlyColor(Color color)
{
base.AfterSetForeReadOnlyColor(color);
edit.ForeColor = GetForeColor();
2020-05-11 21:11:29 +08:00
}
public enum UIEditType
{
/// <summary>
/// 字符串
/// </summary>
String,
/// <summary>
/// 整数
/// </summary>
Integer,
/// <summary>
/// 浮点数
/// </summary>
Double
}
[DefaultValue(false)]
public bool AcceptsReturn
{
get => edit.AcceptsReturn;
set => edit.AcceptsReturn = value;
}
[DefaultValue(AutoCompleteMode.None), Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public AutoCompleteMode AutoCompleteMode
{
get => edit.AutoCompleteMode;
set => edit.AutoCompleteMode = value;
}
[
DefaultValue(AutoCompleteSource.None),
TypeConverterAttribute(typeof(TextBoxAutoCompleteSourceConverter)),
Browsable(true),
EditorBrowsable(EditorBrowsableState.Always)
]
public AutoCompleteSource AutoCompleteSource
{
get => edit.AutoCompleteSource;
set => edit.AutoCompleteSource = value;
}
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Localizable(true),
Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)),
Browsable(true),
EditorBrowsable(EditorBrowsableState.Always)
]
public AutoCompleteStringCollection AutoCompleteCustomSource
{
get => edit.AutoCompleteCustomSource;
set => edit.AutoCompleteCustomSource = value;
}
[DefaultValue(CharacterCasing.Normal)]
public CharacterCasing CharacterCasing
{
get => edit.CharacterCasing;
set => edit.CharacterCasing = value;
}
public void Paste(string text)
{
edit.Paste(text);
}
internal class TextBoxAutoCompleteSourceConverter : EnumConverter
{
public TextBoxAutoCompleteSourceConverter(Type type) : base(type)
{
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
StandardValuesCollection values = base.GetStandardValues(context);
ArrayList list = new ArrayList();
int count = values.Count;
for (int i = 0; i < count; i++)
{
string currentItemText = values[i].ToString();
if (currentItemText != null && !currentItemText.Equals("ListItems"))
{
list.Add(values[i]);
}
}
return new StandardValuesCollection(list);
}
}
[DefaultValue(false)]
public bool AcceptsTab
{
get => edit.AcceptsTab;
set => edit.AcceptsTab = value;
}
[DefaultValue(false)]
public bool EnterAsTab
{
get => edit.EnterAsTab;
set => edit.EnterAsTab = value;
}
[DefaultValue(true)]
public bool ShortcutsEnabled
{
get => edit.ShortcutsEnabled;
set => edit.ShortcutsEnabled = value;
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool CanUndo
{
get => edit.CanUndo;
}
[DefaultValue(true)]
public bool HideSelection
{
get => edit.HideSelection;
set => edit.HideSelection = value;
}
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
MergableProperty(false),
Localizable(true),
Editor("System.Windows.Forms.Design.StringArrayEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))
]
public string[] Lines
{
get => edit.Lines;
set => edit.Lines = value;
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool Modified
{
get => edit.Modified;
set => edit.Modified = value;
}
[
Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public int PreferredHeight
{
get => edit.PreferredHeight;
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string SelectedText
{
get => edit.SelectedText;
set => edit.SelectedText = value;
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int SelectionLength
{
get => edit.SelectionLength;
set => edit.SelectionLength = value;
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int SelectionStart
{
get => edit.SelectionStart;
set => edit.SelectionStart = value;
}
[Browsable(false)]
public int TextLength
{
get => edit.TextLength;
}
public void AppendText(string text)
{
edit.AppendText(text);
}
public void ClearUndo()
{
edit.ClearUndo();
}
public void Copy()
{
edit.Copy();
}
public void Cut()
{
edit.Cut();
}
public void Paste()
{
edit.Paste();
}
public char GetCharFromPosition(Point pt)
{
return edit.GetCharFromPosition(pt);
}
public int GetCharIndexFromPosition(Point pt)
{
return edit.GetCharIndexFromPosition(pt);
}
public int GetLineFromCharIndex(int index)
{
return edit.GetLineFromCharIndex(index);
}
public Point GetPositionFromCharIndex(int index)
{
return edit.GetPositionFromCharIndex(index);
}
public int GetFirstCharIndexFromLine(int lineNumber)
{
return edit.GetFirstCharIndexFromLine(lineNumber);
}
public int GetFirstCharIndexOfCurrentLine()
{
return edit.GetFirstCharIndexOfCurrentLine();
}
public void DeselectAll()
{
edit.DeselectAll();
}
public void Undo()
{
edit.Undo();
}
2021-05-12 14:44:39 +08:00
private Image icon;
[Description("图标"), Category("SunnyUI")]
[DefaultValue(null)]
public Image Icon
{
get => icon;
set
{
icon = value;
SizeChange();
Invalidate();
}
}
private int iconSize = 24;
[Description("图标大小(方形)"), Category("SunnyUI"), DefaultValue(24)]
2021-05-12 14:44:39 +08:00
public int IconSize
{
get => iconSize;
set
{
iconSize = Math.Min(UIGlobal.EditorMinHeight, value);
2021-05-12 14:44:39 +08:00
SizeChange();
Invalidate();
}
}
2022-06-24 15:04:08 +08:00
/// <summary>
/// 重载绘图
/// </summary>
/// <param name="e">绘图参数</param>
2021-05-12 14:44:39 +08:00
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (multiline) return;
if (icon != null)
{
e.Graphics.DrawImage(icon, new Rectangle(4, (Height - iconSize) / 2, iconSize, iconSize), new Rectangle(0, 0, icon.Width, icon.Height), GraphicsUnit.Pixel);
}
else if (Symbol != 0)
{
e.Graphics.DrawFontImage(Symbol, SymbolSize, SymbolColor, new Rectangle(4 + symbolOffset.X, (Height - SymbolSize) / 2 + 1 + symbolOffset.Y, SymbolSize, SymbolSize), SymbolOffset.X, SymbolOffset.Y, SymbolRotate);
}
2023-11-06 14:08:09 +08:00
if (Text.IsValid() && NeedDrawDisabledText)
{
string text = Text;
if (PasswordChar > 0)
{
text = PasswordChar.ToString().Repeat(text.Length);
}
ContentAlignment textAlign = ContentAlignment.MiddleLeft;
if (TextAlignment == ContentAlignment.TopCenter || TextAlignment == ContentAlignment.MiddleCenter || TextAlignment == ContentAlignment.BottomCenter)
textAlign = ContentAlignment.MiddleCenter;
if (TextAlignment == ContentAlignment.TopRight || TextAlignment == ContentAlignment.MiddleRight || TextAlignment == ContentAlignment.BottomRight)
textAlign = ContentAlignment.MiddleRight;
e.Graphics.DrawString(text, edit.Font, ForeDisableColor, edit.Bounds, textAlign);
}
}
public Color _symbolColor = UIFontColor.Primary;
2022-06-23 23:27:55 +08:00
/// <summary>
/// 字体图标颜色
/// </summary>
[DefaultValue(typeof(Color), "48, 48, 48")]
[Description("字体图标颜色"), Category("SunnyUI")]
public Color SymbolColor
{
get => _symbolColor;
set
2021-05-12 14:44:39 +08:00
{
_symbolColor = value;
Invalidate();
}
}
2021-10-16 22:33:10 +08:00
private int _symbol;
2022-06-23 23:27:55 +08:00
/// <summary>
/// 字体图标
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Editor("Sunny.UI.UIImagePropertyEditor, " + AssemblyRefEx.SystemDesign, typeof(UITypeEditor))]
[DefaultValue(0)]
[Description("字体图标"), Category("SunnyUI")]
public int Symbol
{
get => _symbol;
set
{
_symbol = value;
SizeChange();
Invalidate();
}
}
private int _symbolSize = 24;
2022-06-23 23:27:55 +08:00
/// <summary>
/// 字体图标大小
/// </summary>
[DefaultValue(24)]
[Description("字体图标大小"), Category("SunnyUI")]
public int SymbolSize
{
get => _symbolSize;
set
{
_symbolSize = Math.Max(value, 16);
_symbolSize = Math.Min(value, UIGlobal.EditorMaxHeight);
SizeChange();
Invalidate();
2021-05-12 14:44:39 +08:00
}
}
2021-07-15 13:54:31 +08:00
private Point symbolOffset = new Point(0, 0);
2022-06-23 23:27:55 +08:00
/// <summary>
/// 字体图标的偏移位置
/// </summary>
2021-07-15 13:54:31 +08:00
[DefaultValue(typeof(Point), "0, 0")]
[Description("字体图标的偏移位置"), Category("SunnyUI")]
public Point SymbolOffset
{
get => symbolOffset;
set
{
symbolOffset = value;
Invalidate();
}
}
2021-09-07 17:46:27 +08:00
private int _symbolRotate = 0;
/// <summary>
/// 字体图标旋转角度
/// </summary>
[DefaultValue(0)]
[Description("字体图标旋转角度"), Category("SunnyUI")]
public int SymbolRotate
{
get => _symbolRotate;
set
{
if (_symbolRotate != value)
{
_symbolRotate = value;
Invalidate();
}
}
}
2021-09-07 17:46:27 +08:00
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Editor("Sunny.UI.UIImagePropertyEditor, " + AssemblyRefEx.SystemDesign, typeof(UITypeEditor))]
2022-02-26 10:53:40 +08:00
[DefaultValue(361761)]
2021-09-07 17:46:27 +08:00
[Description("按钮字体图标"), Category("SunnyUI")]
public int ButtonSymbol
{
get => btn.Symbol;
set => btn.Symbol = value;
}
[DefaultValue(24)]
[Description("按钮字体图标大小"), Category("SunnyUI")]
public int ButtonSymbolSize
{
get => btn.SymbolSize;
set => btn.SymbolSize = value;
}
[DefaultValue(typeof(Point), "-1, 1")]
2021-09-07 17:46:27 +08:00
[Description("按钮字体图标的偏移位置"), Category("SunnyUI")]
public Point ButtonSymbolOffset
{
get => btn.SymbolOffset;
set => btn.SymbolOffset = value;
}
/// <summary>
/// 字体图标旋转角度
/// </summary>
[DefaultValue(0)]
[Description("按钮字体图标旋转角度"), Category("SunnyUI")]
public int ButtonSymbolRotate
{
get => btn.SymbolRotate;
set => btn.SymbolRotate = value;
}
/// <summary>
/// 填充颜色,当值为背景色或透明色或空值则不填充
/// </summary>
[Description("按钮填充颜色"), Category("SunnyUI")]
[DefaultValue(typeof(Color), "80, 160, 255")]
public Color ButtonFillColor
{
get => btn.FillColor;
set
{
btn.FillColor = value;
2023-11-17 23:07:19 +08:00
btn.Style = UIStyle.Custom;
}
}
/// <summary>
/// 字体颜色
/// </summary>
[Description("按钮字体颜色"), Category("SunnyUI")]
[DefaultValue(typeof(Color), "White")]
public Color ButtonForeColor
{
get => btn.ForeColor;
set
{
btn.SymbolColor = btn.ForeColor = value;
2023-11-17 23:07:19 +08:00
btn.Style = UIStyle.Custom;
}
}
/// <summary>
/// 边框颜色
/// </summary>
[Description("按钮边框颜色"), Category("SunnyUI")]
[DefaultValue(typeof(Color), "80, 160, 255")]
public Color ButtonRectColor
{
get => btn.RectColor;
set
{
btn.RectColor = value;
2023-11-17 23:07:19 +08:00
btn.Style = UIStyle.Custom;
}
}
[DefaultValue(typeof(Color), "115, 179, 255"), Category("SunnyUI")]
[Description("按钮鼠标移上时填充颜色")]
public Color ButtonFillHoverColor
{
get => btn.FillHoverColor;
set
{
btn.FillHoverColor = value;
2023-11-17 23:07:19 +08:00
btn.Style = UIStyle.Custom;
}
}
[DefaultValue(typeof(Color), "White"), Category("SunnyUI")]
[Description("按钮鼠标移上时字体颜色")]
public Color ButtonForeHoverColor
{
get => btn.ForeHoverColor;
set
{
btn.SymbolHoverColor = btn.ForeHoverColor = value;
2023-11-17 23:07:19 +08:00
btn.Style = UIStyle.Custom;
}
}
[DefaultValue(typeof(Color), "115, 179, 255"), Category("SunnyUI")]
[Description("鼠标移上时边框颜色")]
public Color ButtonRectHoverColor
{
get => btn.RectHoverColor;
set
{
btn.RectHoverColor = value;
2023-11-17 23:07:19 +08:00
btn.Style = UIStyle.Custom;
}
}
[DefaultValue(typeof(Color), "64, 128, 204"), Category("SunnyUI")]
[Description("按钮鼠标按下时填充颜色")]
public Color ButtonFillPressColor
{
get => btn.FillPressColor;
set
{
btn.FillPressColor = value;
2023-11-17 23:07:19 +08:00
btn.Style = UIStyle.Custom;
}
}
[DefaultValue(typeof(Color), "White"), Category("SunnyUI")]
[Description("按钮鼠标按下时字体颜色")]
public Color ButtonForePressColor
{
get => btn.ForePressColor;
set
{
btn.SymbolPressColor = btn.ForePressColor = value;
2023-11-17 23:07:19 +08:00
btn.Style = UIStyle.Custom;
}
}
[DefaultValue(typeof(Color), "64, 128, 204"), Category("SunnyUI")]
[Description("按钮鼠标按下时边框颜色")]
public Color ButtonRectPressColor
{
get => btn.RectPressColor;
set
{
btn.RectPressColor = value;
2023-11-17 23:07:19 +08:00
btn.Style = UIStyle.Custom;
}
}
/// <summary>
/// 滚动条主题样式
/// </summary>
[DefaultValue(true), Description("滚动条主题样式"), Category("SunnyUI")]
public bool ButtonStyleInherited
{
get => btn != null && btn.Style == UIStyle.Inherited;
set
{
if (value && btn != null)
{
btn.Style = UIStyle.Inherited;
}
}
}
2020-05-11 21:11:29 +08:00
}
}