2014/3/30
星期日
看完了书,看完了RSS订阅的新闻,刷完了微博,看完了电影,代码永远是有Bug的。好像没有事情可干了(嗯,其实还是有很多事情要做的,只是提不起兴致)
我的这个周末过得是有多索然无味
翻翻自己的电脑,无意中找出以前的一个电脑桌面日历小程序,是用C#做的,还可以显示温度哦,还蛮有意思的O(∩_∩)O~~
首先呢,新建一个Visual C#窗体应用程序frmBrowser.cs。在窗体中添加1个Tooltrip控件,如下图所示:
再按照上面的方法依次添加1个Panel控件、5个Label控件和6个LinkLabel控件。依次修改各个控件的Backcolor属性、
Name属性和Text属性,上述工作完成了之后就可以得到这样的界面啦:
接下来就是最关键的工作啦:给每个控件事件响应添加程序~~
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396 |
using
System; using
System.Collections.Generic; using
System.Drawing; using
System.IO; using
System.Net; using
System.Runtime.InteropServices; using
System.Text; using
System.Text.RegularExpressions; using
System.Windows.Forms; using
System.Xml; namespace
today { public
partial class frmBrowser : Form { public
frmBrowser() { InitializeComponent(); } private
DateTime currentDay = DateTime.Now; private
void frmBrowser_Load( object
sender, EventArgs e) { lklDownload.LinkColor = Color.FromKnownColor(KnownColor.GreenYellow); linkPreviousDay.LinkColor = Color.FromKnownColor(KnownColor.GreenYellow); linkNextDay.LinkColor = Color.FromKnownColor(KnownColor.GreenYellow); ShowDay(currentDay); Location = new
Point(Screen.PrimaryScreen.WorkingArea.Width - Width - 100, 80); this .ShowInTaskbar = false ; } private
void lklDownload_LinkClicked( object
sender, LinkLabelLinkClickedEventArgs e) { if
(!IsConnected()) { MessageBox.Show( this , "请检查网络后,重新点击下载" ); return ; } string
xmlPath = Path.Combine(Application.StartupPath, string .Concat(DateTime.Now.Year, ".xml" )); if
(!File.Exists(xmlPath)) { DownloadYiJi(); } List< string > ss = Get5Days(); XmlDocument doc = new
XmlDocument(); doc.Load(xmlPath); DateTime dt = DateTime.Now.AddDays(1); for
( int
i = 0; i < 5; i++) { XmlNode xmlNode = doc.SelectSingleNode( "days/day"
+ dt.ToString( "MMdd" )); if
(xmlNode == null ) return ; if
(FindAttribute(xmlNode, "temperature" )) { xmlNode.Attributes[ "temperature" ].Value = ss[i]; } else { XmlAttribute attr = doc.CreateAttribute( "temperature" ); attr.Value = ss[i]; xmlNode.Attributes.Append(attr); } dt = dt.AddDays(1); } doc.Save(xmlPath); } private
void DownloadYiJi() { try { DateTime dt = DateTime.Now; DateTime dt1 = new
DateTime(dt.Year, 1, 1); XmlDocument doc = new
XmlDocument(); doc.LoadXml( "<?xml version=\"1.0\" encoding=\"utf-8\" ?><days/>" ); while
( true ) { string
url = string .Format( "{3}/{0}/{0}-{1}-{2}.html" , urlBase, dt1.Year, dt1.Month, dt1.Day); XmlNode node = doc.CreateNode(XmlNodeType.Element, string .Concat( "day" , dt1.ToString( "MMdd" )), null ); string
a, b; GetYiJi( out
a, out
b, url); AddAttribute(doc, node, "yi" , a); AddAttribute(doc, node, "ji" , b); doc.DocumentElement.AppendChild(node); dt1 = dt1.AddDays(1); if
(dt1.Year > dt.Year) { break ; } } doc.Save( string .Concat(dt.Year, ".xml" )); } catch { //nothing } } private
static void AddAttribute(XmlDocument doc, XmlNode node, string
attrName, string
value) { XmlAttribute attr = doc.CreateAttribute(attrName); attr.Value = value; node.Attributes.Append(attr); } private
void GetYiJi( out
string yi, out
string ji, string
url) { WebClient webClient = new
WebClient(); string
str = webClient.DownloadString(url); MatchCollection matchs = Regex.Matches(str, "<td[^>]*?>(?<Text>.*?)</td>" , RegexOptions.IgnoreCase); List< string > contents = new
List< string >(); foreach
(Match m in
matchs) { if
(m.Success) { string
text = m.Groups[ "Text" ].Value; if
(! string .IsNullOrEmpty(text)) { contents.Add(text); } } } yi = string .Empty; ji = string .Empty; for
( int
i = 0; i < contents.Count; i++) { if
(contents[i].Contains( "【今日老黄历所宜】" )) { yi = contents[i + 1].Replace( "<br/>" , "\r\n" ); } else
if (contents[i].Contains( "【今日老黄历所忌】" )) { ji = contents[i + 1].Replace( "<br/>" , "\r\n" ); break ; } } } private
List< string > Get5Days() { WebClient webClient = new
WebClient(); webClient.Encoding = Encoding.UTF8; MatchCollection matchs = Regex.Matches(str, "<table class=\"yuBaoTable\"[^>]*?>(.|\n)*?</table>" ); DateTime dt = DateTime.Now.AddDays(1); List< string > ss = new
List< string >(); foreach
(Match m in
matchs) { if
(m.Success) { string
text = m.Value; text = Regex.Replace(text, "</?table[^>]*>" , "" , RegexOptions.IgnoreCase); text = Regex.Replace(text, "</?tr[^>]*>" , "" , RegexOptions.IgnoreCase); text = Regex.Replace(text, "</?td[^>]*>" , "" , RegexOptions.IgnoreCase); text = Regex.Replace(text, "</?a[^>]*>" , "" , RegexOptions.IgnoreCase); text = Regex.Replace(text, "</?img[^>]*>" , "" , RegexOptions.IgnoreCase); text = Regex.Replace(text, "</?span[^>]*>" , "" , RegexOptions.IgnoreCase); text = Regex.Replace(text, "</?strong[^>]*>" , "" , RegexOptions.IgnoreCase); text = Regex.Replace(text, "</?b[^>]*>" , "" , RegexOptions.IgnoreCase); text = Regex.Replace(text, "<script.*?>(.|\n)*?</script>" , "" , RegexOptions.IgnoreCase); text = Regex.Replace(text, "<!--(.|\n)*?-->" , "" , RegexOptions.IgnoreCase); text = Regex.Replace(text, "</?noscript[^>]*>" , "" , RegexOptions.IgnoreCase); text = text.Replace( "\r\n" , "" ); string [] texts = text.Split( " " .ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if
(texts[0] == dt.ToString( "dd日星期ddd" ) && (ss.Count < 5)) { string
t = texts[2]; string
t1 = texts[8]; if
(!t.Contains( "转" ) && !t1.Contains( "转" ) && t != t1) { t = string .Concat(t, "转" , t1); } ss.Add( string .Concat(t, " " , texts[4], "~" , texts[10])); dt = dt.AddDays(1); } } } return
ss; } private
void lklDownload_MouseMove( object
sender, MouseEventArgs e) { lklDownload.LinkColor = Color.FromArgb(0, 0, 255); } private
void lklDownload_MouseLeave( object
sender, EventArgs e) { lklDownload.LinkColor = Color.FromKnownColor(KnownColor.GreenYellow); } private
void linkYi_LinkClicked( object
sender, LinkLabelLinkClickedEventArgs e) { toolTip1.SetToolTip(linkYi, linkYi.Tag.ToString()); } private
void linkJi_LinkClicked( object
sender, LinkLabelLinkClickedEventArgs e) { toolTip1.SetToolTip(linkJi, linkJi.Tag.ToString()); } private
void linkPreviousDay_LinkClicked( object
sender, LinkLabelLinkClickedEventArgs e) { currentDay = currentDay.AddDays(-1); ShowDay(currentDay); } private
void linkNextDay_LinkClicked( object
sender, LinkLabelLinkClickedEventArgs e) { currentDay = currentDay.AddDays(1); ShowDay(currentDay); } private
void linkNextDay_MouseMove( object
sender, MouseEventArgs e) { linkNextDay.LinkColor = Color.FromArgb(0, 0, 255); } private
void linkNextDay_MouseLeave( object
sender, EventArgs e) { linkNextDay.LinkColor = Color.FromKnownColor(KnownColor.GreenYellow); } private
void linkPreviousDay_MouseMove( object
sender, MouseEventArgs e) { linkPreviousDay.LinkColor = Color.FromArgb(0, 0, 255); } private
void linkPreviousDay_MouseLeave( object
sender, EventArgs e) { linkPreviousDay.LinkColor = Color.FromKnownColor(KnownColor.GreenYellow); } void
ShowDay(DateTime dt) { label1.Text = dt.ToString( "yyyy年M月星期ddd" ); label2.Text = dt.Day.ToString(); label3.Text = Class1.GetLunisolarYear(dt); label6.Text = string .Concat(Class1.GetLunisolarMonth(dt), Class1.GetLunisolarDay(dt)); XmlNode xmlNode = GetDayNode(dt); string
temperature = string .Empty; if
(IsConnected()) { temperature = GetTemperature(dt); } if
(xmlNode != null ) { linkYi.Tag = xmlNode.Attributes[ "yi" ].Value; linkJi.Tag = xmlNode.Attributes[ "ji" ].Value; if
(FindAttribute(xmlNode, "temperature" ) && string .IsNullOrEmpty(temperature)) { temperature = xmlNode.Attributes[ "temperature" ].Value; } } label4.Text = string .IsNullOrEmpty(temperature) ? "未知"
: temperature; label1.Location = new
Point((Width - label1.Width) / 2, 10); label2.Location = new
Point((Width - label2.Width) / 2, 25); label3.Location = new
Point((Width - label3.Width) / 2, 79); label4.Location = new
Point((Width - label4.Width) / 2, 140); label6.Location = new
Point((Width - label6.Width) / 2, 96); } private
string GetTemperature(DateTime dt) { try { if
(dt.Date == DateTime.Now.Date) { WebClient webClient = new
WebClient(); webClient.Encoding = Encoding.UTF8; Match m = Regex.Match(str, "<div class=\"jxyb\" id=\"weather6h\">(.|\n)*?</div>" ); List< string > contents = new
List< string >(); if
(m.Success) { MatchCollection matchs = Regex.Matches(m.Value, "<td[^>]*?>(?<Text>(.|\n)*?)</td>" , RegexOptions.IgnoreCase); foreach
(Match m1 in
matchs) { if
(m1.Success) { string
text = Regex.Replace(m1.Value, "</?td[^>]*>" , "" ); text = Regex.Replace(text, "</?img[^>]*>" , "" ); text = Regex.Replace(text, "</?a[^>]*>" , "" ); text = Regex.Replace(text, "</?b[^>]*>" , "" ); text = Regex.Replace(text, "</?span[^>]*>" , "" ); text = Regex.Replace(text, "~" , "" ); text = Regex.Replace(text, "\r\n" , "" ); if
(! string .IsNullOrEmpty(text.Trim())) { contents.Add(text.Trim()); } } } } if
(contents.Count > 0) { string
weather = contents[0]; string
weather2 = contents[9]; string
sss = string .Empty; for
( int
i = 0; i < contents.Count; i++) { if
(i%3 == 1) { sss += contents[i].Replace( " " , "" ); } } string [] s = sss.Split( "℃" .ToCharArray(), StringSplitOptions.RemoveEmptyEntries); int [] arr = new
int [s.Length]; for
( int
i = 0; i < arr.Length; i++) { arr[i] = int .Parse(s[i]); } Array.Sort(arr); if
(!weather.Contains( "转" ) && !weather2.Contains( "转" ) && weather != weather2) { weather = string .Concat(weather, "转" ,weather2); } return
string .Concat(weather, " " , arr[arr.Length - 1], "℃" , "~" , arr[0], "℃" ); } } } catch { } return
string .Empty; } private
XmlNode GetDayNode(DateTime dt) { string
xmlPath = Path.Combine(Application.StartupPath, string .Concat(dt.Year, ".xml" )); if
(File.Exists(xmlPath)) { XmlDocument doc = new
XmlDocument(); doc.Load(xmlPath); XmlNode xmlNode = doc.SelectSingleNode( "days/day"
+ dt.ToString( "MMdd" )); if
(xmlNode != null
&& xmlNode.Attributes != null ) { return
xmlNode; } } return
null ; } private
static bool FindAttribute(XmlNode xmlNode, string
attrName) { if
(xmlNode.Attributes == null ) return
false ; for
( int
i = 0; i < xmlNode.Attributes.Count; i++) { if
(xmlNode.Attributes[i].Name == attrName) { return
true ; } } return
false ; } private
void linkToday_LinkClicked( object
sender, LinkLabelLinkClickedEventArgs e) { DateTime dt = DateTime.Now; currentDay = new
DateTime(dt.Ticks, dt.Kind); ShowDay(currentDay); } private
bool IsConnected() { try { WebClient webClient = new
WebClient(); webClient.DownloadString(uri); return
true ; } catch { return
false ; } } private
void label1_Click( object
sender, EventArgs e) { } private
void label2_Click( object
sender, EventArgs e) { } private
void toolTip2_Popup( object
sender, PopupEventArgs e) { } } } |
F5一键运行,搞定~~
原文:http://www.cnblogs.com/Kerinamoon/p/3634520.html