2014/3/30 星期日
看完了书,看完了RSS订阅的新闻,刷完了微博,看完了电影,代码永远是有Bug的。好像没有事情可干了(嗯,其实还是有很多事情要做的,只是提不起兴致)
我的这个周末过得是有多索然无味
翻翻自己的电脑,无意中找出以前的一个电脑桌面日历小程序,是用C#做的,还可以显示温度哦,还蛮有意思的O(∩_∩)O~~
首先呢,新建一个Visual C#窗体应用程序frmBrowser.cs。在窗体中添加1个Tooltrip控件,如下图所示:
再按照上面的方法依次添加1个Panel控件、5个Label控件和6个LinkLabel控件。依次修改各个控件的Backcolor属性、
Name属性和Text属性,上述工作完成了之后就可以得到这样的界面啦:
接下来就是最关键的工作啦:给每个控件事件响应添加程序~~
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/>"); const string urlBase = "http://www.laohuangli.net/"; 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; string str = webClient.DownloadString("http://www.weather.com.cn/html/weather/101010100.shtml"); 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; string str = webClient.DownloadString("http://www.weather.com.cn/html/weather/101010100.shtml"); 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(); Uri uri = new Uri("http://www.baidu.com", UriKind.Absolute); 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一键运行,搞定~~
请发表评论