UCRollText.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-31-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCRollText.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Windows.Forms;
  21. using System.Drawing;
  22. using System.Drawing.Drawing2D;
  23. using System.ComponentModel;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCRollText.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public class UCRollText : UserControl
  32. {
  33. /// <summary>
  34. /// 获取或设置控件显示的文字的字体。
  35. /// </summary>
  36. /// <value>The font.</value>
  37. /// <PermissionSet>
  38. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  39. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  40. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  41. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  42. /// </PermissionSet>
  43. public override Font Font
  44. {
  45. get
  46. {
  47. return base.Font;
  48. }
  49. set
  50. {
  51. base.Font = value;
  52. if (!string.IsNullOrEmpty(Text))
  53. {
  54. Graphics g = this.CreateGraphics();
  55. var size = g.MeasureString(Text, this.Font);
  56. rectText = new Rectangle(0, (this.Height - rectText.Height) / 2 + 1, (int)size.Width, (int)size.Height);
  57. rectText.Y = (this.Height - rectText.Height) / 2 + 1;
  58. }
  59. }
  60. }
  61. /// <summary>
  62. /// 获取或设置控件的前景色。
  63. /// </summary>
  64. /// <value>The color of the fore.</value>
  65. /// <PermissionSet>
  66. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  67. /// </PermissionSet>
  68. public override Color ForeColor
  69. {
  70. get
  71. {
  72. return base.ForeColor;
  73. }
  74. set
  75. {
  76. base.ForeColor = value;
  77. }
  78. }
  79. /// <summary>
  80. /// Gets or sets the text.
  81. /// </summary>
  82. /// <value>The text.</value>
  83. [Bindable(true)]
  84. [Browsable(true)]
  85. [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  86. [EditorBrowsable(EditorBrowsableState.Always)]
  87. public override string Text
  88. {
  89. get
  90. {
  91. return base.Text;
  92. }
  93. set
  94. {
  95. base.Text = value;
  96. if (!string.IsNullOrEmpty(value))
  97. {
  98. Graphics g = this.CreateGraphics();
  99. var size = g.MeasureString(value, this.Font);
  100. rectText = new Rectangle(0, (this.Height - rectText.Height) / 2 + 1, (int)size.Width, (int)size.Height);
  101. }
  102. else
  103. {
  104. rectText = Rectangle.Empty;
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// The roll style
  110. /// </summary>
  111. private RollStyle _RollStyle = RollStyle.LeftToRight;
  112. /// <summary>
  113. /// Gets or sets the roll style.
  114. /// </summary>
  115. /// <value>The roll style.</value>
  116. [Description("滚动样式"), Category("自定义")]
  117. public RollStyle RollStyle
  118. {
  119. get { return _RollStyle; }
  120. set
  121. {
  122. _RollStyle = value;
  123. switch (value)
  124. {
  125. case RollStyle.LeftToRight:
  126. m_intdirection = 1;
  127. break;
  128. case RollStyle.RightToLeft:
  129. m_intdirection = -1;
  130. break;
  131. }
  132. }
  133. }
  134. /// <summary>
  135. /// The move step
  136. /// </summary>
  137. private int _moveStep = 5;
  138. /// <summary>
  139. /// The move sleep time
  140. /// </summary>
  141. private int _moveSleepTime = 100;
  142. /// <summary>
  143. /// Gets or sets the move sleep time.
  144. /// </summary>
  145. /// <value>The move sleep time.</value>
  146. [Description("每次滚动间隔时间,越小速度越快"), Category("自定义")]
  147. public int MoveSleepTime
  148. {
  149. get { return _moveSleepTime; }
  150. set
  151. {
  152. if (value <= 0)
  153. return;
  154. _moveSleepTime = value;
  155. m_timer.Interval = value;
  156. }
  157. }
  158. /// <summary>
  159. /// The m intdirection
  160. /// </summary>
  161. int m_intdirection = 1;
  162. /// <summary>
  163. /// The rect text
  164. /// </summary>
  165. Rectangle rectText;
  166. /// <summary>
  167. /// The m timer
  168. /// </summary>
  169. Timer m_timer;
  170. /// <summary>
  171. /// Initializes a new instance of the <see cref="UCRollText" /> class.
  172. /// </summary>
  173. public UCRollText()
  174. {
  175. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  176. this.SetStyle(ControlStyles.DoubleBuffer, true);
  177. this.SetStyle(ControlStyles.ResizeRedraw, true);
  178. this.SetStyle(ControlStyles.Selectable, true);
  179. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  180. this.SetStyle(ControlStyles.UserPaint, true);
  181. this.SizeChanged += UCRollText_SizeChanged;
  182. this.Size = new Size(450, 30);
  183. Text = "滚动文字";
  184. m_timer = new Timer();
  185. m_timer.Interval = 100;
  186. m_timer.Tick += m_timer_Tick;
  187. this.Load += UCRollText_Load;
  188. this.VisibleChanged += UCRollText_VisibleChanged;
  189. this.ForeColor = Color.FromArgb(255, 77, 59);
  190. if (rectText != Rectangle.Empty)
  191. {
  192. rectText.Y = (this.Height - rectText.Height) / 2 + 1;
  193. }
  194. }
  195. /// <summary>
  196. /// Handles the Tick event of the m_timer control.
  197. /// </summary>
  198. /// <param name="sender">The source of the event.</param>
  199. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  200. void m_timer_Tick(object sender, EventArgs e)
  201. {
  202. if (rectText == Rectangle.Empty)
  203. return;
  204. if (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth && rectText.Width >= this.Width)
  205. {
  206. return;
  207. }
  208. rectText.X = rectText.X + _moveStep * m_intdirection;
  209. if (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth)
  210. {
  211. if (rectText.X <= 0)
  212. {
  213. m_intdirection = 1;
  214. }
  215. else if (rectText.Right >= this.Width)
  216. {
  217. m_intdirection = -1;
  218. }
  219. }
  220. else if (_RollStyle == HZH_Controls.Controls.RollStyle.LeftToRight)
  221. {
  222. if (rectText.X > this.Width)
  223. {
  224. rectText.X = -1 * rectText.Width - 1;
  225. }
  226. }
  227. else
  228. {
  229. if (rectText.Right < 0)
  230. {
  231. rectText.X = this.Width + rectText.Width + 1;
  232. }
  233. }
  234. Refresh();
  235. }
  236. /// <summary>
  237. /// Handles the VisibleChanged event of the UCRollText control.
  238. /// </summary>
  239. /// <param name="sender">The source of the event.</param>
  240. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  241. void UCRollText_VisibleChanged(object sender, EventArgs e)
  242. {
  243. m_timer.Enabled = this.Visible;
  244. }
  245. /// <summary>
  246. /// Handles the Load event of the UCRollText control.
  247. /// </summary>
  248. /// <param name="sender">The source of the event.</param>
  249. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  250. void UCRollText_Load(object sender, EventArgs e)
  251. {
  252. if (_RollStyle == HZH_Controls.Controls.RollStyle.LeftToRight)
  253. {
  254. m_intdirection = 1;
  255. if (rectText != Rectangle.Empty)
  256. {
  257. rectText.X = -1 * rectText.Width - 1;
  258. }
  259. }
  260. else if (_RollStyle == HZH_Controls.Controls.RollStyle.RightToLeft)
  261. {
  262. m_intdirection = -1;
  263. if (rectText != Rectangle.Empty)
  264. {
  265. rectText.X = this.Width + rectText.Width + 1;
  266. }
  267. }
  268. else
  269. {
  270. m_intdirection = 1;
  271. if (rectText != Rectangle.Empty)
  272. {
  273. rectText.X = 0;
  274. }
  275. }
  276. if (rectText != Rectangle.Empty)
  277. {
  278. rectText.Y = (this.Height - rectText.Height) / 2 + 1;
  279. }
  280. }
  281. /// <summary>
  282. /// Handles the SizeChanged event of the UCRollText control.
  283. /// </summary>
  284. /// <param name="sender">The source of the event.</param>
  285. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  286. void UCRollText_SizeChanged(object sender, EventArgs e)
  287. {
  288. if (rectText != Rectangle.Empty)
  289. {
  290. rectText.Y = (this.Height - rectText.Height) / 2 + 1;
  291. }
  292. }
  293. /// <summary>
  294. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  295. /// </summary>
  296. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  297. protected override void OnPaint(PaintEventArgs e)
  298. {
  299. base.OnPaint(e);
  300. if (rectText != Rectangle.Empty)
  301. {
  302. e.Graphics.SetGDIHigh();
  303. e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), rectText.Location);
  304. }
  305. }
  306. }
  307. /// <summary>
  308. /// Enum RollStyle
  309. /// </summary>
  310. public enum RollStyle
  311. {
  312. /// <summary>
  313. /// The left to right
  314. /// </summary>
  315. LeftToRight,
  316. /// <summary>
  317. /// The right to left
  318. /// </summary>
  319. RightToLeft,
  320. /// <summary>
  321. /// The back and forth
  322. /// </summary>
  323. BackAndForth
  324. }
  325. }