根据http://blog.163.com/gsrwsh@126/blog/static/75794506201002284439690/ 实现滚动字幕控件,贴出来备以后参考。
1 /// ###################################
2 /// ###Author: eocape #A.T gmail.com. #####
3 /// #######################################
4 /// #######################################
5 using System;
6
7 using System.Collections.Generic;
8 using System.Text;
9 using System.Windows.Forms;
10 using System.Drawing;
11 namespace XXX
12 {
13
14 /// <summary>
15 ///
16 /// </summary>
17 class ScrollMsg : PictureBox
18 {
19 const int DEFAULT_INTERVAL = 500;
20 const int DEFAULT_MOVE_PACE = 10;
21 private int m_movePace;
22 /// <summary>
23 /// Scroll interval.
24 /// </summary>
25 private int m_interval;
26 /// <summary>
27 /// Timer
28 /// </summary>
29 private Timer m_timer;
30 /// <summary>
31 /// Font to display.
32 /// </summary>
33 private Font m_font;
34 /// <summary>
35 /// Text to scroll.
36 /// </summary>
37 private string m_text;
38 /// <summary>
39 /// The brush to draw the text.
40 /// </summary>
41 private Brush m_brush;
42 /// <summary>
43 /// The position to draw text.
44 /// </summary>
45 private float m_x, m_y;
46
47 private Form m_parentForm;
48 /// <summary>
49 /// The height and width of the text to scroll.
50 /// </summary>
51 private float m_str_width, m_str_height;
52 private bool m_isDisposed = false;
53
54 #region Constructrors
55
56 public ScrollMsg(Form parentForm,
57 string text,
58 int interval,
59 int movePace,
60 Font font,
61 Brush brush)
62 : base()
63 {
64 if (interval <= 0)
65 {
66 throw new ArgumentException("Refresh interval can not be negative.");
67 }
68 if (movePace <= 0)
69 {
70 throw new ArgumentException("Move pace can not be negative.");
71 }
72 m_parentForm = parentForm;
73 m_text = text;
74 m_interval = interval;
75 m_movePace = movePace;
76 m_font = font;
77 m_brush = brush;
78 /// The initial draw position.
79 m_x = base.Width;
80 m_y = 0;
81 m_timer = new Timer();
82 m_timer.Enabled = true;
83 m_timer.Interval = m_interval;
84 m_timer.Tick += new EventHandler(m_timer_Tick);
85 /// The initial position to draw text.
86 using (Graphics g = m_parentForm.CreateGraphics())
87 {
88 m_str_height = (int)g.MeasureString(m_text, m_font).Height + 1;
89 m_str_width = (int)g.MeasureString(m_text, m_font).Width + 1;
90 }
91 }
92
93 /// <summary>
94 /// Constructor.
95 /// </summary>
96 /// <param name="parentForm">The parent form that contains this control.</param>
97 /// <param name="text">The text to scroll.</param>
98 /// <param name="font">The font of the text.</param>
99 /// <param name="interval">Scroll interval.</param>
100 /// <param name="movePace">Scroll move pace of each interval.</param>
101 public ScrollMsg(Form parentForm,
102 string text,
103 int interval,
104 int movePace,
105 Font font)
106 : this(parentForm, text, interval, movePace, font, new SolidBrush(Color.Blue))
107 {
108 }
109 /// <summary>
110 ///
111 /// </summary>
112 /// <param name="parentForm">The parent form that contains this control.</param>
113 /// <param name="text">The text to scroll.</param>
114 /// <param name="interval">Scroll interval.</param>
115 /// <param name="movePace">Scroll move pace of each interval.</param>
116 public ScrollMsg(Form parentForm,
117 string text,
118 int interval,
119 int movePace)
120 : this(parentForm, text, interval, movePace, new Font("宋体", 13F, FontStyle.Regular))
121 {
122 }
123 /// <summary>
124 ///
125 /// </summary>
126 /// <param name="parentForm">The parent form that contains this control.</param>
127 /// <param name="text">The text to scroll.</param>
128 /// <param name="interval">Scroll interval.</param>
129 public ScrollMsg(Form parentForm,
130 string text,
131 int interval)
132 : this(parentForm, text,interval,DEFAULT_MOVE_PACE)
133 {
134 }
135 /// <summary>
136 ///
137 /// </summary>
138 /// <param name="parentForm">The parent form that contains this control.</param>
139 /// <param name="text">The text to scroll.</param>
140 public ScrollMsg(Form parentForm,
141 string text)
142 : this(parentForm, text, DEFAULT_INTERVAL)
143 {
144 }
145
146 #endregion
147
148
149
150 #region Dispoer.
151 /// <summary>
152 /// Dispose all unmanaged resources.
153 /// </summary>
154 /// <param name="disposing"></param>
155 protected override void Dispose(bool disposing)
156 {
157 /// If it's the first time of disposing
158 if (!m_isDisposed)
159 {
160 /// True if called by programmer, else false if called
161 /// by GC.
162 if (disposing)
163 {
164 base.Dispose(disposing);
165 }
166 /// Release unmanaged resources.
167 if (m_timer != null)
168 {
169 m_timer.Dispose();
170 m_timer = null;
171 }
172 if (m_font != null)
173 {
174 m_font.Dispose();
175 m_font = null;
176 }
177 if (m_brush != null)
178 {
179 m_brush.Dispose();
180 m_brush = null;
181 }
182 m_isDisposed = true;
183 }
184 }
185 /// <summary>
186 /// The entry for programmer to dispose.
187 /// </summary>
188 public new void Dispose()
189 {
190 GC.SuppressFinalize(this);
191 Dispose(true);
192 }
193 /// <summary>
194 /// Finalizer.
195 /// </summary>
196 ~ScrollMsg()
197 {
198 Dispose(false);
199 }
200 #endregion
201
202 #region Attributes and fucntions.
203 /// <summary>
204 /// Enable scrolling.
205 /// </summary>
206 public void EnableScroll()
207 {
208 m_timer.Enabled = true;
209 }
210
211 /// <summary>
212 /// Disable scrolling.
213 /// </summary>
214 public void DisableScroll()
215 {
216 m_timer.Enabled = false;
217 }
218 /// <summary>
219 ///
220 /// </summary>
221 public Font TextFont
222 {
223 set
224 {
225 m_font = value;
226 refresh_Members();
227 }
228 }
229 /// <summary>
230 ///
231 /// </summary>
232 public override string Text
233 {
234 set
235 {
236 m_text = value;
237 refresh_Members();
238 }
239 }
240 /// <summary>
241 ///
242 /// </summary>
243 public int Interval
244 {
245 set
246 {
247 if (value <= 0)
248 {
249 throw new ArgumentException("Refresh interval can not be negative.");
250 }
251 m_interval = value;
252 refresh_Members();
253 }
254 }
255 /// <summary>
256 ///
257 /// </summary>
258 public int MovePace
259 {
260 set
261 {
262 if (value <= 0)
263 {
264 throw new ArgumentException("Move pace can not be negative.");
265 }
266 m_movePace = value;
267 refresh_Members();
268 }
269 }
270 public Brush Brush
271 {
272 set
273 {
274 m_brush = value;
275 refresh_Members();
276 }
277 }
278 /// <summary>
279 ///
280 /// </summary>
281 private void refresh_Members()
282 {
283 using (Graphics g = m_parentForm.CreateGraphics())
284 {
285 m_str_height = (int)g.MeasureString(m_text, m_font).Height + 1;
286 m_str_width = (int)g.MeasureString(m_text, m_font).Width + 1;
287 }
288 this.Invalidate();
289 }
290 /// <summary>
291 /// When timer tick, invalidate
292 /// </summary>
293 /// <param name="sender"></param>
294 /// <param name="e"></param>
295 void m_timer_Tick(object sender, EventArgs e)
296 {
297 if (m_timer.Enabled)
298 {
299 Invalidate();
300 }
301 }
302 /// <summary>
303 /// Override when redraw.
304 /// </summary>
305 /// <param name="e"></param>
306 protected override void OnPaint(PaintEventArgs e)
307 {
308
309 Graphics g = e.Graphics;
310 if (m_timer.Enabled)
311 {
312 /// Disable timer first in case of paint loop.
313 m_timer.Enabled = false;
314 m_x -= m_movePace;
315 m_x = m_x <= -m_str_width ? Width : m_x;
316 g.DrawString(m_text, m_font, m_brush, m_x, m_y);
317 /// Recover timer to tick.
318 m_timer.Enabled = true;
319 }
320 else
321 {
322 g.DrawString(m_text, m_font, m_brush, m_x, m_y);
323 }
324 base.OnPaint(e);
325
326 }
327 #endregion
328 }
329 }
330
3 /// #######################################
4 /// #######################################
5 using System;
6
7 using System.Collections.Generic;
8 using System.Text;
9 using System.Windows.Forms;
10 using System.Drawing;
11 namespace XXX
12 {
13
14 /// <summary>
15 ///
16 /// </summary>
17 class ScrollMsg : PictureBox
18 {
19 const int DEFAULT_INTERVAL = 500;
20 const int DEFAULT_MOVE_PACE = 10;
21 private int m_movePace;
22 /// <summary>
23 /// Scroll interval.
24 /// </summary>
25 private int m_interval;
26 /// <summary>
27 /// Timer
28 /// </summary>
29 private Timer m_timer;
30 /// <summary>
31 /// Font to display.
32 /// </summary>
33 private Font m_font;
34 /// <summary>
35 /// Text to scroll.
36 /// </summary>
37 private string m_text;
38 /// <summary>
39 /// The brush to draw the text.
40 /// </summary>
41 private Brush m_brush;
42 /// <summary>
43 /// The position to draw text.
44 /// </summary>
45 private float m_x, m_y;
46
47 private Form m_parentForm;
48 /// <summary>
49 /// The height and width of the text to scroll.
50 /// </summary>
51 private float m_str_width, m_str_height;
52 private bool m_isDisposed = false;
53
54 #region Constructrors
55
56 public ScrollMsg(Form parentForm,
57 string text,
58 int interval,
59 int movePace,
60 Font font,
61 Brush brush)
62 : base()
63 {
64 if (interval <= 0)
65 {
66 throw new ArgumentException("Refresh interval can not be negative.");
67 }
68 if (movePace <= 0)
69 {
70 throw new ArgumentException("Move pace can not be negative.");
71 }
72 m_parentForm = parentForm;
73 m_text = text;
74 m_interval = interval;
75 m_movePace = movePace;
76 m_font = font;
77 m_brush = brush;
78 /// The initial draw position.
79 m_x = base.Width;
80 m_y = 0;
81 m_timer = new Timer();
82 m_timer.Enabled = true;
83 m_timer.Interval = m_interval;
84 m_timer.Tick += new EventHandler(m_timer_Tick);
85 /// The initial position to draw text.
86 using (Graphics g = m_parentForm.CreateGraphics())
87 {
88 m_str_height = (int)g.MeasureString(m_text, m_font).Height + 1;
89 m_str_width = (int)g.MeasureString(m_text, m_font).Width + 1;
90 }
91 }
92
93 /// <summary>
94 /// Constructor.
95 /// </summary>
96 /// <param name="parentForm">The parent form that contains this control.</param>
97 /// <param name="text">The text to scroll.</param>
98 /// <param name="font">The font of the text.</param>
99 /// <param name="interval">Scroll interval.</param>
100 /// <param name="movePace">Scroll move pace of each interval.</param>
101 public ScrollMsg(Form parentForm,
102 string text,
103 int interval,
104 int movePace,
105 Font font)
106 : this(parentForm, text, interval, movePace, font, new SolidBrush(Color.Blue))
107 {
108 }
109 /// <summary>
110 ///
111 /// </summary>
112 /// <param name="parentForm">The parent form that contains this control.</param>
113 /// <param name="text">The text to scroll.</param>
114 /// <param name="interval">Scroll interval.</param>
115 /// <param name="movePace">Scroll move pace of each interval.</param>
116 public ScrollMsg(Form parentForm,
117 string text,
118 int interval,
119 int movePace)
120 : this(parentForm, text, interval, movePace, new Font("宋体", 13F, FontStyle.Regular))
121 {
122 }
123 /// <summary>
124 ///
125 /// </summary>
126 /// <param name="parentForm">The parent form that contains this control.</param>
127 /// <param name="text">The text to scroll.</param>
128 /// <param name="interval">Scroll interval.</param>
129 public ScrollMsg(Form parentForm,
130 string text,
131 int interval)
132 : this(parentForm, text,interval,DEFAULT_MOVE_PACE)
133 {
134 }
135 /// <summary>
136 ///
137 /// </summary>
138 /// <param name="parentForm">The parent form that contains this control.</param>
139 /// <param name="text">The text to scroll.</param>
140 public ScrollMsg(Form parentForm,
141 string text)
142 : this(parentForm, text, DEFAULT_INTERVAL)
143 {
144 }
145
146 #endregion
147
148
149
150 #region Dispoer.
151 /// <summary>
152 /// Dispose all unmanaged resources.
153 /// </summary>
154 /// <param name="disposing"></param>
155 protected override void Dispose(bool disposing)
156 {
157 /// If it's the first time of disposing
158 if (!m_isDisposed)
159 {
160 /// True if called by programmer, else false if called
161 /// by GC.
162 if (disposing)
163 {
164 base.Dispose(disposing);
165 }
166 /// Release unmanaged resources.
167 if (m_timer != null)
168 {
169 m_timer.Dispose();
170 m_timer = null;
171 }
172 if (m_font != null)
173 {
174 m_font.Dispose();
175 m_font = null;
176 }
177 if (m_brush != null)
178 {
179 m_brush.Dispose();
180 m_brush = null;
181 }
182 m_isDisposed = true;
183 }
184 }
185 /// <summary>
186 /// The entry for programmer to dispose.
187 /// </summary>
188 public new void Dispose()
189 {
190 GC.SuppressFinalize(this);
191 Dispose(true);
192 }
193 /// <summary>
194 /// Finalizer.
195 /// </summary>
196 ~ScrollMsg()
197 {
198 Dispose(false);
199 }
200 #endregion
201
202 #region Attributes and fucntions.
203 /// <summary>
204 /// Enable scrolling.
205 /// </summary>
206 public void EnableScroll()
207 {
208 m_timer.Enabled = true;
209 }
210
211 /// <summary>
212 /// Disable scrolling.
213 /// </summary>
214 public void DisableScroll()
215 {
216 m_timer.Enabled = false;
217 }
218 /// <summary>
219 ///
220 /// </summary>
221 public Font TextFont
222 {
223 set
224 {
225 m_font = value;
226 refresh_Members();
227 }
228 }
229 /// <summary>
230 ///
231 /// </summary>
232 public override string Text
233 {
234 set
235 {
236 m_text = value;
237 refresh_Members();
238 }
239 }
240 /// <summary>
241 ///
242 /// </summary>
243 public int Interval
244 {
245 set
246 {
247 if (value <= 0)
248 {
249 throw new ArgumentException("Refresh interval can not be negative.");
250 }
251 m_interval = value;
252 refresh_Members();
253 }
254 }
255 /// <summary>
256 ///
257 /// </summary>
258 public int MovePace
259 {
260 set
261 {
262 if (value <= 0)
263 {
264 throw new ArgumentException("Move pace can not be negative.");
265 }
266 m_movePace = value;
267 refresh_Members();
268 }
269 }
270 public Brush Brush
271 {
272 set
273 {
274 m_brush = value;
275 refresh_Members();
276 }
277 }
278 /// <summary>
279 ///
280 /// </summary>
281 private void refresh_Members()
282 {
283 using (Graphics g = m_parentForm.CreateGraphics())
284 {
285 m_str_height = (int)g.MeasureString(m_text, m_font).Height + 1;
286 m_str_width = (int)g.MeasureString(m_text, m_font).Width + 1;
287 }
288 this.Invalidate();
289 }
290 /// <summary>
291 /// When timer tick, invalidate
292 /// </summary>
293 /// <param name="sender"></param>
294 /// <param name="e"></param>
295 void m_timer_Tick(object sender, EventArgs e)
296 {
297 if (m_timer.Enabled)
298 {
299 Invalidate();
300 }
301 }
302 /// <summary>
303 /// Override when redraw.
304 /// </summary>
305 /// <param name="e"></param>
306 protected override void OnPaint(PaintEventArgs e)
307 {
308
309 Graphics g = e.Graphics;
310 if (m_timer.Enabled)
311 {
312 /// Disable timer first in case of paint loop.
313 m_timer.Enabled = false;
314 m_x -= m_movePace;
315 m_x = m_x <= -m_str_width ? Width : m_x;
316 g.DrawString(m_text, m_font, m_brush, m_x, m_y);
317 /// Recover timer to tick.
318 m_timer.Enabled = true;
319 }
320 else
321 {
322 g.DrawString(m_text, m_font, m_brush, m_x, m_y);
323 }
324 base.OnPaint(e);
325
326 }
327 #endregion
328 }
329 }
330