Winform:滚动文字提示
做成一个用户控件,需要一个Label、Timer基础控件,让计时器不断移动Label的位置就可以了。
Code
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8
9namespace pcd.WinControl
10{
11 public partial class LabelTextMoving : UserControl
12 {
13 public LabelTextMoving()
14 {
15 InitializeComponent();
16 }
17
18 private void timer1_Tick(object sender, EventArgs e)
19 {
20 if (labMsg.Location.X < 0 && Math.Abs(labMsg.Location.X) - labMsg.Width >= 0)
21 {
22 labMsg.Location = new Point(panel1.Width, labMsg.Location.Y);
23 return;
24 }
25 labMsg.Location = new Point(labMsg.Location.X - 5, labMsg.Location.Y);
26 }
27
28
29 Properties#region Properties
30 [Category("Appearance"),
31 Description("Label前景色")]
32 public System.Drawing.Color LabelForeColor
33 {
34 get { return labMsg.ForeColor; }
35 set { labMsg.ForeColor = value; }
36 }
37
38
39
40 [Category("Appearance"),
41 Description("LabelText")]
42 public string LabelText
43 {
44 get { return labMsg.Text; }
45 set
46 {
47
48 labMsg.Text = value;
49 if (value == "")
50 timer1.Enabled = false;
51 else timer1.Enabled = true;
52 }
53 }
54
55 #endregion
56 }
57}
58
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8
9namespace pcd.WinControl
10{
11 public partial class LabelTextMoving : UserControl
12 {
13 public LabelTextMoving()
14 {
15 InitializeComponent();
16 }
17
18 private void timer1_Tick(object sender, EventArgs e)
19 {
20 if (labMsg.Location.X < 0 && Math.Abs(labMsg.Location.X) - labMsg.Width >= 0)
21 {
22 labMsg.Location = new Point(panel1.Width, labMsg.Location.Y);
23 return;
24 }
25 labMsg.Location = new Point(labMsg.Location.X - 5, labMsg.Location.Y);
26 }
27
28
29 Properties#region Properties
30 [Category("Appearance"),
31 Description("Label前景色")]
32 public System.Drawing.Color LabelForeColor
33 {
34 get { return labMsg.ForeColor; }
35 set { labMsg.ForeColor = value; }
36 }
37
38
39
40 [Category("Appearance"),
41 Description("LabelText")]
42 public string LabelText
43 {
44 get { return labMsg.Text; }
45 set
46 {
47
48 labMsg.Text = value;
49 if (value == "")
50 timer1.Enabled = false;
51 else timer1.Enabled = true;
52 }
53 }
54
55 #endregion
56 }
57}
58