You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.6 KiB

  1. #if NETFX || NETCORE
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. namespace Apewer.Surface
  8. {
  9. /// <summary></summary>
  10. public class BlockLabel : Label
  11. {
  12. private IntPtr _formptr;
  13. private bool _movable = false;
  14. private bool _hover = false;
  15. private bool _locked = false;
  16. private bool _dynamic = false;
  17. private Color _wallnormal = FormsUtility.Transparent;
  18. private Color _wallhover = FormsUtility.Transparent;
  19. private Color _walllocked = FormsUtility.Transparent;
  20. private Color _textnormal = FormsUtility.Black;
  21. private Color _texthover = FormsUtility.Black;
  22. private Color _textlocked = FormsUtility.Black;
  23. /// <summary>界面线程调用器。</summary>
  24. protected delegate void Invoker();
  25. private void _init(string argText, Form argForm)
  26. {
  27. AutoEllipsis = true;
  28. AutoSize = false;
  29. Font = FormsUtility.DefaultFont;
  30. TextAlign = ContentAlignment.MiddleLeft;
  31. BackColor = _wallnormal;
  32. ForeColor = FormsUtility.Black;
  33. AutoEllipsis = true;
  34. Text = string.IsNullOrEmpty(argText) ? "" : argText;
  35. if (argForm != null)
  36. {
  37. _formptr = argForm.Handle;
  38. _movable = true;
  39. MouseDown += Event_Caption_MouseDown;
  40. }
  41. }
  42. /// <summary></summary>
  43. public BlockLabel()
  44. {
  45. _init("", null);
  46. this.MouseMove += Event_This_MouseMove;
  47. this.MouseLeave += Event_This_MouseLeave;
  48. }
  49. private void Event_This_MouseLeave(object sender, EventArgs e)
  50. {
  51. Hover = false;
  52. }
  53. private void Event_This_MouseMove(object sender, MouseEventArgs e)
  54. {
  55. Hover = true;
  56. }
  57. /// <summary></summary>
  58. /// <param name="argText"></param>
  59. public BlockLabel(string argText)
  60. {
  61. _init(argText, null);
  62. }
  63. /// <summary></summary>
  64. /// <param name="argForm"></param>
  65. public BlockLabel(Form argForm)
  66. {
  67. _init("", argForm);
  68. }
  69. /// <summary></summary>
  70. /// <param name="argForm"></param>
  71. /// <param name="argText"></param>
  72. public BlockLabel(Form argForm, string argText)
  73. {
  74. _init(argText, argForm);
  75. }
  76. /// <summary></summary>
  77. /// <param name="argText"></param>
  78. /// <param name="argForm"></param>
  79. public BlockLabel(string argText, Form argForm)
  80. {
  81. _init(argText, argForm);
  82. }
  83. private void Event_Caption_MouseDown(object sender, MouseEventArgs e)
  84. {
  85. if (_movable) FormsUtility.MoveForm(_formptr);
  86. }
  87. /// <summary></summary>
  88. public string Caption
  89. {
  90. get { return Text; }
  91. set
  92. {
  93. if (this.IsHandleCreated)
  94. {
  95. try
  96. {
  97. this.Invoke(new Invoker(delegate ()
  98. {
  99. Text = string.IsNullOrEmpty(value) ? "" : value.Trim();
  100. }));
  101. }
  102. catch { }
  103. }
  104. else
  105. {
  106. try
  107. {
  108. Text = string.IsNullOrEmpty(value) ? "" : value.Trim();
  109. }
  110. catch { }
  111. }
  112. }
  113. }
  114. /// <summary></summary>
  115. public bool Locked
  116. {
  117. get { return _locked; }
  118. set
  119. {
  120. var vchanged = (value != _locked);
  121. _locked = value;
  122. if (vchanged) UpdateColor();
  123. }
  124. }
  125. private bool Hover
  126. {
  127. get { return _hover; }
  128. set
  129. {
  130. var vchanged = (value != _hover);
  131. _hover = value;
  132. if (vchanged) UpdateColor();
  133. }
  134. }
  135. private void UpdateColor()
  136. {
  137. if (!_dynamic) return;
  138. if (Locked)
  139. {
  140. BackColor = _walllocked;
  141. ForeColor = _textlocked;
  142. }
  143. else if (_hover)
  144. {
  145. BackColor = _wallhover;
  146. ForeColor = _texthover;
  147. }
  148. else
  149. {
  150. BackColor = _wallnormal;
  151. ForeColor = _textnormal;
  152. }
  153. }
  154. /// <summary>启用鼠标悬停和锁定样式。</summary>
  155. public bool DynamicStyle { get { return _dynamic; } set { _dynamic = value; } }
  156. /// <summary></summary>
  157. public Color WallNormal { get { return _wallnormal; } set { _wallnormal = value; UpdateColor(); } }
  158. /// <summary></summary>
  159. public Color WallHover { get { return _wallhover; } set { _wallhover = value; UpdateColor(); } }
  160. /// <summary></summary>
  161. public Color WallLocked { get { return _walllocked; } set { _walllocked = value; UpdateColor(); } }
  162. /// <summary></summary>
  163. public Color TextNormal { get { return _textnormal; } set { _textnormal = value; UpdateColor(); } }
  164. /// <summary></summary>
  165. public Color TextHover { get { return _texthover; } set { _texthover = value; UpdateColor(); } }
  166. /// <summary></summary>
  167. public Color TextLocked { get { return _textlocked; } set { _textlocked = value; UpdateColor(); } }
  168. }
  169. }
  170. #endif