(五十三)c#Winform自定义控件-滚动文字

官网

http://www.hzhcontrols.com

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

麻烦博客下方点个【推荐】,谢谢

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

准备工作

依然是GDI+画的,如果不了解可以百度下先

开始

添加一个枚举,用以控制移动方向

1   public enum RollStyle
2     {
3         LeftToRight,
4         RightToLeft,
5         BackAndForth
6     }

 

添加一个类UCRollText,继承UserControl

添加几个控制属性

 1  public override Font Font
 2         {
 3             get
 4             {
 5                 return base.Font;
 6             }
 7             set
 8             {
 9                 base.Font = value;
10                 if (!string.IsNullOrEmpty(Text))
11                 {
12                     Graphics g = this.CreateGraphics();
13                     var size = g.MeasureString(Text, this.Font);
14                     rectText = new Rectangle(0, (this.Height - rectText.Height) / 2 + 1, (int)size.Width, (int)size.Height);
15                     rectText.Y = (this.Height - rectText.Height) / 2 + 1;
16                 }
17             }
18         }
19 
20         public override Color ForeColor
21         {
22             get
23             {
24                 return base.ForeColor;
25             }
26             set
27             {
28                 base.ForeColor = value;
29             }
30         }
31 
32         public override string Text
33         {
34             get
35             {
36                 return base.Text;
37             }
38             set
39             {
40                 base.Text = value;
41                 if (!string.IsNullOrEmpty(value))
42                 {
43                     Graphics g = this.CreateGraphics();
44                     var size = g.MeasureString(value, this.Font);
45                     rectText = new Rectangle(0, (this.Height - rectText.Height) / 2 + 1, (int)size.Width, (int)size.Height);
46                 }
47                 else
48                 {
49                     rectText = Rectangle.Empty;
50                 }
51             }
52         }
53 
54         private RollStyle _RollStyle = RollStyle.LeftToRight;
55 
56         [Description("滚动样式"), Category("自定义")]
57         public RollStyle RollStyle
58         {
59             get { return _RollStyle; }
60             set
61             {
62                 _RollStyle = value;
63                 switch (value)
64                 {
65                     case RollStyle.LeftToRight:
66                         m_intdirection = 1;
67                         break;
68                     case RollStyle.RightToLeft:
69                         m_intdirection = -1;
70                         break;
71                 }
72             }
73         }
74 
75         private int _moveStep = 5;
76 
77         private int _moveSleepTime = 100;
78 
79         [Description("每次滚动间隔时间,越小速度越快"), Category("自定义")]
80         public int MoveSleepTime
81         {
82             get { return _moveSleepTime; }
83             set
84             {
85                 if (value <= 0)
86                     return;
87 
88                 _moveSleepTime = value;
89                 m_timer.Interval = value;
90             }
91         }
92 
93         int m_intdirection = 1;
94 
95         Rectangle rectText;
96         Timer m_timer;

构造函数处理一些事情

 1  public UCRollText()
 2         {
 3             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
 4             this.SetStyle(ControlStyles.DoubleBuffer, true);
 5             this.SetStyle(ControlStyles.ResizeRedraw, true);
 6             this.SetStyle(ControlStyles.Selectable, true);
 7             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
 8             this.SetStyle(ControlStyles.UserPaint, true);
 9 
10             this.SizeChanged += UCRollText_SizeChanged;
11             this.Size = new Size(450, 30);
12             Text = "滚动文字";
13             m_timer = new Timer();
14             m_timer.Interval = 100;
15             m_timer.Tick += m_timer_Tick;
16             this.Load += UCRollText_Load;
17             this.VisibleChanged += UCRollText_VisibleChanged;
18             this.ForeColor = Color.FromArgb(255, 77, 59);
19             if (rectText != Rectangle.Empty)
20             {
21                 rectText.Y = (this.Height - rectText.Height) / 2 + 1;
22             }
23         }

加载的时候处理一下初始位置

 1  void UCRollText_Load(object sender, EventArgs e)
 2         {
 3             if (_RollStyle == HZH_Controls.Controls.RollStyle.LeftToRight)
 4             {
 5                 m_intdirection = 1;
 6                 if (rectText != Rectangle.Empty)
 7                 {
 8                     rectText.X = -1 * rectText.Width - 1;
 9                 }
10             }
11             else if (_RollStyle == HZH_Controls.Controls.RollStyle.RightToLeft)
12             {
13                 m_intdirection = -1;
14                 if (rectText != Rectangle.Empty)
15                 {
16                     rectText.X = this.Width + rectText.Width + 1;
17                 }
18             }
19             else
20             {
21                 m_intdirection = 1;
22                 if (rectText != Rectangle.Empty)
23                 {
24                     rectText.X = 0;
25                 }
26             }
27             if (rectText != Rectangle.Empty)
28             {
29                 rectText.Y = (this.Height - rectText.Height) / 2 + 1;
30             }
31         }

定时器里面处理位置

 1 void m_timer_Tick(object sender, EventArgs e)
 2         {
 3             if (rectText == Rectangle.Empty)
 4                 return;
 5             if (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth && rectText.Width >= this.Width)
 6             {
 7                 return;
 8             }
 9             rectText.X = rectText.X + _moveStep * m_intdirection;
10             if (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth)
11             {
12                 if (rectText.X <= 0)
13                 {
14                     m_intdirection = 1;
15                 }
16                 else if (rectText.Right >= this.Width)
17                 {
18                     m_intdirection = -1;
19                 }
20             }
21             else if (_RollStyle == HZH_Controls.Controls.RollStyle.LeftToRight)
22             {
23                 if (rectText.X > this.Width)
24                 {
25                     rectText.X = -1 * rectText.Width - 1;
26                 }
27             }
28             else
29             {
30                 if (rectText.Right < 0)
31                 {
32                     rectText.X = this.Width + rectText.Width + 1;
33                 }
34             }
35             Refresh();
36         }

重绘

1  protected override void OnPaint(PaintEventArgs e)
2         {
3             base.OnPaint(e);
4             if (rectText != Rectangle.Empty)
5             {
6                 e.Graphics.SetGDIHigh();
7                 e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), rectText.Location);
8             }
9         }

完整代码

  1 // 版权所有  黄正辉  交流群:568015492   QQ:623128629
  2 // 文件名称:UCRollText.cs
  3 // 作  者:HZH
  4 // 创建日期:2019-09-03 09:59:12
  5 // 功能描述:UCRollText    English:UCRollText
  6 // 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
  7 // 项目地址:https://github.com/kwwwvagaa/NetWinformControl
  8 // 如果你使用了此类,请保留以上说明
  9 using System;
 10 using System.Collections.Generic;
 11 using System.Linq;
 12 using System.Text;
 13 using System.Windows.Forms;
 14 using System.Drawing;
 15 using System.Drawing.Drawing2D;
 16 using System.ComponentModel;
 17 
 18 namespace HZH_Controls.Controls
 19 {
 20     public class UCRollText : UserControl
 21     {
 22         public override Font Font
 23         {
 24             get
 25             {
 26                 return base.Font;
 27             }
 28             set
 29             {
 30                 base.Font = value;
 31                 if (!string.IsNullOrEmpty(Text))
 32                 {
 33                     Graphics g = this.CreateGraphics();
 34                     var size = g.MeasureString(Text, this.Font);
 35                     rectText = new Rectangle(0, (this.Height - rectText.Height) / 2 + 1, (int)size.Width, (int)size.Height);
 36                     rectText.Y = (this.Height - rectText.Height) / 2 + 1;
 37                 }
 38             }
 39         }
 40 
 41         public override Color ForeColor
 42         {
 43             get
 44             {
 45                 return base.ForeColor;
 46             }
 47             set
 48             {
 49                 base.ForeColor = value;
 50             }
 51         }
 52 
 53         public override string Text
 54         {
 55             get
 56             {
 57                 return base.Text;
 58             }
 59             set
 60             {
 61                 base.Text = value;
 62                 if (!string.IsNullOrEmpty(value))
 63                 {
 64                     Graphics g = this.CreateGraphics();
 65                     var size = g.MeasureString(value, this.Font);
 66                     rectText = new Rectangle(0, (this.Height - rectText.Height) / 2 + 1, (int)size.Width, (int)size.Height);
 67                 }
 68                 else
 69                 {
 70                     rectText = Rectangle.Empty;
 71                 }
 72             }
 73         }
 74 
 75         private RollStyle _RollStyle = RollStyle.LeftToRight;
 76 
 77         [Description("滚动样式"), Category("自定义")]
 78         public RollStyle RollStyle
 79         {
 80             get { return _RollStyle; }
 81             set
 82             {
 83                 _RollStyle = value;
 84                 switch (value)
 85                 {
 86                     case RollStyle.LeftToRight:
 87                         m_intdirection = 1;
 88                         break;
 89                     case RollStyle.RightToLeft:
 90                         m_intdirection = -1;
 91                         break;
 92                 }
 93             }
 94         }
 95 
 96         private int _moveStep = 5;
 97 
 98         private int _moveSleepTime = 100;
 99 
100         [Description("每次滚动间隔时间,越小速度越快"), Category("自定义")]
101         public int MoveSleepTime
102         {
103             get { return _moveSleepTime; }
104             set
105             {
106                 if (value <= 0)
107                     return;
108 
109                 _moveSleepTime = value;
110                 m_timer.Interval = value;
111             }
112         }
113 
114         int m_intdirection = 1;
115 
116         Rectangle rectText;
117         Timer m_timer;
118         public UCRollText()
119         {
120             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
121             this.SetStyle(ControlStyles.DoubleBuffer, true);
122             this.SetStyle(ControlStyles.ResizeRedraw, true);
123             this.SetStyle(ControlStyles.Selectable, true);
124             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
125             this.SetStyle(ControlStyles.UserPaint, true);
126 
127             this.SizeChanged += UCRollText_SizeChanged;
128             this.Size = new Size(450, 30);
129             Text = "滚动文字";
130             m_timer = new Timer();
131             m_timer.Interval = 100;
132             m_timer.Tick += m_timer_Tick;
133             this.Load += UCRollText_Load;
134             this.VisibleChanged += UCRollText_VisibleChanged;
135             this.ForeColor = Color.FromArgb(255, 77, 59);
136             if (rectText != Rectangle.Empty)
137             {
138                 rectText.Y = (this.Height - rectText.Height) / 2 + 1;
139             }
140         }
141 
142         void m_timer_Tick(object sender, EventArgs e)
143         {
144             if (rectText == Rectangle.Empty)
145                 return;
146             if (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth && rectText.Width >= this.Width)
147             {
148                 return;
149             }
150             rectText.X = rectText.X + _moveStep * m_intdirection;
151             if (_RollStyle == HZH_Controls.Controls.RollStyle.BackAndForth)
152             {
153                 if (rectText.X <= 0)
154                 {
155                     m_intdirection = 1;
156                 }
157                 else if (rectText.Right >= this.Width)
158                 {
159                     m_intdirection = -1;
160                 }
161             }
162             else if (_RollStyle == HZH_Controls.Controls.RollStyle.LeftToRight)
163             {
164                 if (rectText.X > this.Width)
165                 {
166                     rectText.X = -1 * rectText.Width - 1;
167                 }
168             }
169             else
170             {
171                 if (rectText.Right < 0)
172                 {
173                     rectText.X = this.Width + rectText.Width + 1;
174                 }
175             }
176             Refresh();
177         }
178 
179         void UCRollText_VisibleChanged(object sender, EventArgs e)
180         {
181             m_timer.Enabled = this.Visible;
182         }
183 
184         void UCRollText_Load(object sender, EventArgs e)
185         {
186             if (_RollStyle == HZH_Controls.Controls.RollStyle.LeftToRight)
187             {
188                 m_intdirection = 1;
189                 if (rectText != Rectangle.Empty)
190                 {
191                     rectText.X = -1 * rectText.Width - 1;
192                 }
193             }
194             else if (_RollStyle == HZH_Controls.Controls.RollStyle.RightToLeft)
195             {
196                 m_intdirection = -1;
197                 if (rectText != Rectangle.Empty)
198                 {
199                     rectText.X = this.Width + rectText.Width + 1;
200                 }
201             }
202             else
203             {
204                 m_intdirection = 1;
205                 if (rectText != Rectangle.Empty)
206                 {
207                     rectText.X = 0;
208                 }
209             }
210             if (rectText != Rectangle.Empty)
211             {
212                 rectText.Y = (this.Height - rectText.Height) / 2 + 1;
213             }
214         }
215 
216         void UCRollText_SizeChanged(object sender, EventArgs e)
217         {
218             if (rectText != Rectangle.Empty)
219             {
220                 rectText.Y = (this.Height - rectText.Height) / 2 + 1;
221             }
222         }
223 
224         protected override void OnPaint(PaintEventArgs e)
225         {
226             base.OnPaint(e);
227             if (rectText != Rectangle.Empty)
228             {
229                 e.Graphics.SetGDIHigh();
230                 e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), rectText.Location);
231             }
232         }
233     }
234 
235     public enum RollStyle
236     {
237         LeftToRight,
238         RightToLeft,
239         BackAndForth
240     }
241 }
View Code

 

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

posted @ 2019-09-03 10:00  冰封一夏  阅读(...)  评论(... 编辑 收藏
HZHControls控件库官网:http://hzhcontrols.com