ucErrorDisplay.cs
6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace MyControlLib
{
public partial class ucErrorDisplay : UserControl
{
public ucErrorDisplay()
{
InitializeComponent();
//原点
EF = label2.Location;
this.FontChanged += new EventHandler(ucErrorDisplay_FontChanged);
this.ForeColorChanged += new EventHandler(ucErrorDisplay_ForeColorChanged);
this.BackColorChanged += new EventHandler(ucErrorDisplay_BackColorChanged);
timer1.Start();
}
void ucErrorDisplay_BackColorChanged(object sender, EventArgs e)
{
label1.BackColor = this.BackColor;
//throw new NotImplementedException();
}
void ucErrorDisplay_ForeColorChanged(object sender, EventArgs e)
{
label1.ForeColor = this.ForeColor;
//throw new NotImplementedException();
}
void ucErrorDisplay_FontChanged(object sender, EventArgs e)
{
label1.Font = this.Font;
//throw new NotImplementedException();
}
/// <summary>
/// 移动的速度
/// </summary>
private int _SpeedMove=40;
public int SpeedMove
{
get
{
return _SpeedMove;
}
set
{
if (value >= 0&&value<=50)
{
if (_SpeedMove != value)
{
_SpeedMove = value;
}
}
}
}
/// <summary>
/// 移动的时间间隔
/// </summary>
private int _Interval = 500;
public int Interval
{
get
{
return _Interval;
}
set
{
if (value > 100 && value < 5000)
{
if (_Interval != value)
{
_Interval = value;
timer1.Interval = _Interval;
}
}
}
}
/// <summary>
/// 定时器移动
/// </summary>
private bool _Enable = true;
public bool MoveEnable
{
get
{
return _Enable;
}
set
{
if (_Enable != value)
{
_Enable = value;
timer1.Enabled = _Enable;
if (_Enable == false)
{
label1.Location = PF;
}
else
{
label1.Location = EF;
}
}
}
}
/// <summary>
/// 原点
/// </summary>
private Point PF = new Point(1,0);
/// <summary>
/// 移动起始点
/// </summary>
private Point EF = new Point();
/// <summary>
/// 文件名称
/// </summary>
private string _strPathName;
private string PreTime = "";
/// <summary>
/// 错误信息
/// </summary>
private string _ErrText;
public string ErrText
{
get
{
return _ErrText;
}
set
{
if (value!=null && value != "" && value.Length <= 65535)
{
if (_ErrText != value)
{
_ErrText = value;
PreTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
if (_ErrText != "")
{
label1.Text = PreTime + ":" + _ErrText;
writeErrorLog(label1.Text);
}
}
}
else if (value=="")
{
label1.Text = "";
}
}
}
private int x;
private void timer1_Tick(object sender, EventArgs e)
{
EF = label2.Location;
if (label1.Location.X > PF.X - label1.Width && label1.Location.X< this.Width - label2.Width)
{
x -= SpeedMove;
if (x >= 0 && x <= EF.X)
{
label1.Location = new Point(x, PF.Y);
}
else
{
x = EF.X;
}
}
}
/// <summary>
/// 错误日志记录
/// </summary>
/// <param name="errorTxt"></param>
private void writeErrorLog(string errorTxt)
{
try
{
string strPath = "D:\\SunEast\\Log\\LogRecord\\";
string strPathName = strPath + string.Format("\\{0}.txt", DateTime.Now.ToString("yyyy-MM-dd"));
_strPathName = strPathName;
if (!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
}
if (errorTxt != "")
{
StreamWriter sw = new StreamWriter(_strPathName,true);
sw.WriteLine(errorTxt);
sw.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 错误日志查看
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label2_Click(object sender, EventArgs e)
{
try
{
if (_strPathName.Length > 0)
{
System.Diagnostics.Process.Start(_strPathName);
ErrText = "";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}