#if NETFX || NETCORE using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Apewer.Surface { /// public class BlockLabel : Label { private IntPtr _formptr; private bool _movable = false; private bool _hover = false; private bool _locked = false; private bool _dynamic = false; private Color _wallnormal = FormsUtility.Transparent; private Color _wallhover = FormsUtility.Transparent; private Color _walllocked = FormsUtility.Transparent; private Color _textnormal = FormsUtility.Black; private Color _texthover = FormsUtility.Black; private Color _textlocked = FormsUtility.Black; /// 界面线程调用器。 protected delegate void Invoker(); private void _init(string text, Form form) { AutoEllipsis = true; AutoSize = false; Font = FormsUtility.DefaultFont; TextAlign = ContentAlignment.MiddleLeft; BackColor = _wallnormal; ForeColor = FormsUtility.Black; AutoEllipsis = true; Text = string.IsNullOrEmpty(text) ? "" : text; if (form != null) { _formptr = form.Handle; _movable = true; MouseDown += Event_Caption_MouseDown; } } /// public BlockLabel() { _init("", null); this.MouseMove += Event_This_MouseMove; this.MouseLeave += Event_This_MouseLeave; } private void Event_This_MouseLeave(object sender, EventArgs e) { Hover = false; } private void Event_This_MouseMove(object sender, MouseEventArgs e) { Hover = true; } /// /// public BlockLabel(string text) { _init(text, null); } /// /// public BlockLabel(Form form) => _init("", form); /// /// /// public BlockLabel(Form form, string text) => _init(text, form); /// /// /// public BlockLabel(string text, Form form) => _init(text, form); private void Event_Caption_MouseDown(object sender, MouseEventArgs e) { if (_movable) FormsUtility.MoveForm(_formptr); } /// public string Caption { get { return Text; } set { if (this.IsHandleCreated) { try { this.Invoke(new Invoker(delegate () { Text = string.IsNullOrEmpty(value) ? "" : value.Trim(); })); } catch { } } else { try { Text = string.IsNullOrEmpty(value) ? "" : value.Trim(); } catch { } } } } /// public bool Locked { get { return _locked; } set { var vchanged = (value != _locked); _locked = value; if (vchanged) UpdateColor(); } } private bool Hover { get { return _hover; } set { var vchanged = (value != _hover); _hover = value; if (vchanged) UpdateColor(); } } private void UpdateColor() { if (!_dynamic) return; if (Locked) { BackColor = _walllocked; ForeColor = _textlocked; } else if (_hover) { BackColor = _wallhover; ForeColor = _texthover; } else { BackColor = _wallnormal; ForeColor = _textnormal; } } /// 启用鼠标悬停和锁定样式。 public bool DynamicStyle { get { return _dynamic; } set { _dynamic = value; } } /// public Color WallNormal { get { return _wallnormal; } set { _wallnormal = value; UpdateColor(); } } /// public Color WallHover { get { return _wallhover; } set { _wallhover = value; UpdateColor(); } } /// public Color WallLocked { get { return _walllocked; } set { _walllocked = value; UpdateColor(); } } /// public Color TextNormal { get { return _textnormal; } set { _textnormal = value; UpdateColor(); } } /// public Color TextHover { get { return _texthover; } set { _texthover = value; UpdateColor(); } } /// public Color TextLocked { get { return _textlocked; } set { _textlocked = value; UpdateColor(); } } } } #endif