UILink.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Text;
  4. using System.Windows.Forms;
  5. using Microsoft.Drawing;
  6. namespace Microsoft.Windows.Forms
  7. {
  8. /// <summary>
  9. /// 虚拟超链接控件
  10. /// </summary>
  11. public class UILink : UIControl
  12. {
  13. //暂存进入控件前的光标
  14. private Cursor m_Cursor;
  15. private Color m_HoveredForeColor = DefaultTheme.ForeColor;
  16. /// <summary>
  17. /// 鼠标悬停背景色
  18. /// </summary>
  19. public virtual Color HoveredForeColor
  20. {
  21. get
  22. {
  23. return this.m_HoveredForeColor;
  24. }
  25. set
  26. {
  27. if (value != this.m_HoveredForeColor)
  28. {
  29. this.m_HoveredForeColor = value;
  30. this.Invalidate();
  31. }
  32. }
  33. }
  34. private Color m_PresseddForeColor = DefaultTheme.ForeColor;
  35. /// <summary>
  36. /// 鼠标按下背景色
  37. /// </summary>
  38. public virtual Color PresseddForeColor
  39. {
  40. get
  41. {
  42. return this.m_PresseddForeColor;
  43. }
  44. set
  45. {
  46. if (value != this.m_PresseddForeColor)
  47. {
  48. this.m_PresseddForeColor = value;
  49. this.Invalidate();
  50. }
  51. }
  52. }
  53. private TextRenderingHint m_TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  54. /// <summary>
  55. /// 文本呈现质量
  56. /// </summary>
  57. public virtual TextRenderingHint TextRenderingHint
  58. {
  59. get
  60. {
  61. return this.m_TextRenderingHint;
  62. }
  63. set
  64. {
  65. if (value != this.m_TextRenderingHint)
  66. {
  67. this.m_TextRenderingHint = value;
  68. this.Invalidate();
  69. }
  70. }
  71. }
  72. private ContentAlignment m_TextAlign = ContentAlignment.MiddleCenter;
  73. /// <summary>
  74. /// 文本对齐方式
  75. /// </summary>
  76. public virtual ContentAlignment TextAlign
  77. {
  78. get
  79. {
  80. return this.m_TextAlign;
  81. }
  82. set
  83. {
  84. if (value != this.m_TextAlign)
  85. {
  86. this.m_TextAlign = value;
  87. this.Invalidate();
  88. }
  89. }
  90. }
  91. /// <summary>
  92. ///
  93. /// </summary>
  94. /// <returns></returns>
  95. protected override State GetState()
  96. {
  97. if (this.Enabled)
  98. {
  99. if (this.Capture)
  100. {
  101. if ((Control.MouseButtons & MouseButtons.Left) != 0)//左键按下
  102. return State.Pressed;
  103. else
  104. return State.Hovered;
  105. }
  106. else
  107. {
  108. return State.Normal;
  109. }
  110. }
  111. else
  112. {
  113. return State.Disabled;
  114. }
  115. }
  116. /// <summary>
  117. /// 构造函数
  118. /// </summary>
  119. public UILink()
  120. {
  121. }
  122. /// <summary>
  123. /// 渲染控件
  124. /// </summary>
  125. /// <param name="e">数据</param>
  126. protected override void RenderSelf(PaintEventArgs e)
  127. {
  128. //准备
  129. Graphics g = e.Graphics;
  130. Rectangle rect = RectangleEx.Subtract(this.ClientRectangle, this.Padding);
  131. //渲染
  132. this.Sprite.BorderVisibleStyle = BorderVisibleStyle.All;
  133. this.Sprite.Font = this.Font;
  134. this.Sprite.ForeColor = this.ForeColor;
  135. this.Sprite.ForeColorHovered = this.HoveredForeColor;
  136. this.Sprite.ForeColorPressed = this.PresseddForeColor;
  137. this.Sprite.ForeColorDisabled = this.ForeColor;
  138. this.Sprite.Text = this.Text;
  139. this.Sprite.TextRenderingHint = this.TextRenderingHint;
  140. this.Sprite.TextAlign = this.TextAlign;
  141. this.Sprite.State = this.State;
  142. this.Sprite.BeginRender(g);
  143. this.Sprite.RenderText(rect);
  144. this.Sprite.EndRender();
  145. }
  146. /// <summary>
  147. /// 触发鼠标进入事件
  148. /// </summary>
  149. /// <param name="e"></param>
  150. protected override void OnEnter(EventArgs e)
  151. {
  152. this.m_Cursor = Cursor.Current;
  153. Control control = this.FindUIWindow() as Control;
  154. if (control != null)
  155. control.Cursor = Cursors.Hand;
  156. base.OnEnter(e);
  157. }
  158. /// <summary>
  159. /// 触发鼠标离开事件
  160. /// </summary>
  161. /// <param name="e">数据</param>
  162. protected override void OnLeave(EventArgs e)
  163. {
  164. Control control = this.FindUIWindow() as Control;
  165. if (control != null)
  166. control.Cursor = this.m_Cursor;
  167. base.OnLeave(e);
  168. }
  169. }
  170. }