您的位置:首页 > 其它

[原]Windows mobile 上自定义的一个滚动字幕效果的控件

2010-04-30 11:14 381 查看
根据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>
/// <param name="movePace">Scroll move pace of each interval.</param>
public ScrollMsg(Form parentForm,
string text,
int interval,
int movePace,
Font font)
: this(parentForm, text, interval, movePace, font, new SolidBrush(Color.Blue))
{
}
/// <summary>
///
/// </summary>
/// <param name="parentForm">The parent form that contains this control.</param>
/// <param name="text">The text to scroll.</param>
/// <param name="interval">Scroll interval.</param>
/// <param name="movePace">Scroll move pace of each interval.</param>
public ScrollMsg(Form parentForm,
string text,
int interval,
int movePace)
: this(parentForm, text, interval, movePace, new Font("宋体", 13F, FontStyle.Regular))
{
}
/// <summary>
///
/// </summary>
/// <param name="parentForm">The parent form that contains this control.</param>
/// <param name="text">The text to scroll.</param>
/// <param name="interval">Scroll interval.</param>
public ScrollMsg(Form parentForm,
string text,
int interval)
: this(parentForm, text,interval,DEFAULT_MOVE_PACE)
{
}
/// <summary>
///
/// </summary>
/// <param name="parentForm">The parent form that contains this control.</param>
/// <param name="text">The text to scroll.</param>
public ScrollMsg(Form parentForm,
string text)
: this(parentForm, text, DEFAULT_INTERVAL)
{
}

#endregion

#region Dispoer.
/// <summary>
/// Dispose all unmanaged resources.
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
/// If it's the first time of disposing
if (!m_isDisposed)
{
/// True if called by programmer, else false if called
/// by GC.
if (disposing)
{
base.Dispose(disposing);
}
/// Release unmanaged resources.
if (m_timer != null)
{
m_timer.Dispose();
m_timer = null;
}
if (m_font != null)
{
m_font.Dispose();
m_font = null;
}
if (m_brush != null)
{
m_brush.Dispose();
m_brush = null;
}
m_isDisposed = true;
}
}
/// <summary>
/// The entry for programmer to dispose.
/// </summary>
public new void Dispose()
{
GC.SuppressFinalize(this);
Dispose(true);
}
/// <summary>
/// Finalizer.
/// </summary>
~ScrollMsg()
{
Dispose(false);
}
#endregion

#region Attributes and fucntions.
/// <summary>
/// Enable scrolling.
/// </summary>
public void EnableScroll()
{
m_timer.Enabled = true;
}

/// <summary>
/// Disable scrolling.
/// </summary>
public void DisableScroll()
{
m_timer.Enabled = false;
}
/// <summary>
///
/// </summary>
public Font TextFont
{
set
{
m_font = value;
refresh_Members();
}
}
/// <summary>
///
/// </summary>
public override string Text
{
set
{
m_text = value;
refresh_Members();
}
}
/// <summary>
///
/// </summary>
public int Interval
{
set
{
if (value <= 0)
{
throw new ArgumentException("Refresh interval can not be negative.");
}
m_interval = value;
refresh_Members();
}
}
/// <summary>
///
/// </summary>
public int MovePace
{
set
{
if (value <= 0)
{
throw new ArgumentException("Move pace can not be negative.");
}
m_movePace = value;
refresh_Members();
}
}
public Brush Brush
{
set
{
m_brush = value;
refresh_Members();
}
}
/// <summary>
///
/// </summary>
private void refresh_Members()
{
using (Graphics g = m_parentForm.CreateGraphics())
{
m_str_height = (int)g.MeasureString(m_text, m_font).Height + 1;
m_str_width = (int)g.MeasureString(m_text, m_font).Width + 1;
}
this.Invalidate();
}
/// <summary>
/// When timer tick, invalidate
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void m_timer_Tick(object sender, EventArgs e)
{
if (m_timer.Enabled)
{
Invalidate();
}
}
/// <summary>
/// Override when redraw.
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{

Graphics g = e.Graphics;
if (m_timer.Enabled)
{
/// Disable timer first in case of paint loop.
m_timer.Enabled = false;
m_x -= m_movePace;
m_x = m_x <= -m_str_width ? Width : m_x;
g.DrawString(m_text, m_font, m_brush, m_x, m_y);
/// Recover timer to tick.
m_timer.Enabled = true;
}
else
{
g.DrawString(m_text, m_font, m_brush, m_x, m_y);
}
base.OnPaint(e);

}
#endregion
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: