引言
上一篇簡單介紹了,該工具的實現目標,這里先添加托盤功能,鑒於lz是做b/s開發,有很多不完美的地方,希望作為大師班的你,可以指點一二,不勝感激。
上一篇地址:http://www.cnblogs.com/wolf-sun/p/3504214.html
托盤實現
1.設置winform的ShowInTaskBar bool類型 為false 該屬性決定窗體是否出現在windows任務欄中 這里設置為false
2.添加托盤控件 NotifyIcon並修改名為notifyIconCnblogs,為控件notifyIconCnblogs的屬性Icon添加一個icon圖標。
3.在單擊關閉的時候,添加如下代碼,讓窗體隱藏,並托盤顯示。
1 private void lblClose_Click(object sender, EventArgs e) 2 { 3 this.Hide(); 4 this.notifyIconCnblogs.Visible = true; 5 }
4.為托盤圖標添加單擊事件
1 private void notifyIconCnblogs_Click(object sender, EventArgs e) 2 { 3 this.Visible = true; 4 this.WindowState = FormWindowState.Normal; 5 this.notifyIconCnblogs.Visible = false; 6 }
測試:
將窗體的TopMost屬性設置為True,這樣該窗體就會置頂。
托盤右鍵菜單
在主窗體拖入ContexMenuScript控件,命名為ContexMenuCnblog,選擇該控件,在上下文菜單中添加菜單,notifyIcon1的ContextMenu行為中選中NicontextMenu 作為上下文菜單。
托盤的右鍵菜單測試:
功能代碼

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Net; 8 using System.Runtime.InteropServices; 9 using System.Text; 10 using System.Text.RegularExpressions; 11 using System.Threading.Tasks; 12 using System.Windows.Forms; 13 14 namespace Wolfy.Cnblogs 15 { 16 public partial class MainForm : Form 17 { 18 public MainForm() 19 { 20 InitializeComponent(); 21 } 22 private event GetBlogsEventHandler GetBlogs; 23 log4net.ILog myLogger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 24 List<Blog> list; 25 Timer timer; 26 private void MainForm_Load(object sender, EventArgs e) 27 { 28 //確定窗體位置 右下角 29 Rectangle rectangle = Screen.GetWorkingArea(this); 30 this.Location = new Point(rectangle.Right - this.Width, rectangle.Bottom - this.Height); 31 panelContent.MouseDown += panelContent_MouseDown; 32 33 34 GetBlogs += MainForm_GetBlogs; 35 list = new List<Blog>(); 36 timer = new Timer(); 37 timer.Interval = 10000; 38 timer.Start(); 39 timer.Tick += timer_Tick; 40 } 41 42 void MainForm_GetBlogs(object sender, GetBlogsEventArgs e) 43 { 44 if (e != null) 45 { 46 Blog blog = e.Blog; 47 list.Add(blog); 48 SendMessage(); 49 } 50 } 51 private void SendMessage() 52 { 53 list.Sort(); 54 Blog blog = list.FirstOrDefault(); 55 //頭像地址 56 pbHeader.ImageLocation = blog.Header; 57 linkLabelTitle.Text = blog.Title.Length >= 23 ? blog.Title.Substring(0, 23) : blog.Title;//達到換行的目的 用兩個linklabel 58 linkLabelBreakWord.Text = blog.Title.Length >= 23 ? blog.Title.Substring(23) : ""; 59 linkLabelBreakWord.Visible = blog.Title.Length >= 23 ? true : false; 60 linkLabelBreakWord.VisitedLinkColor = Color.Red; 61 linkLabelBreakWord.Tag = blog.Url; 62 linkLabelBreakWord.LinkClicked += linkLabelTitle_LinkClicked; 63 linkLabelTitle.VisitedLinkColor = Color.Red; 64 linkLabelTitle.Tag = blog.Url;//存放url 65 linkLabelTitle.LinkClicked += linkLabelTitle_LinkClicked; 66 lblAuthor.Text = blog.Author; 67 lblDate.Text = blog.Time.ToString("yyyy-MM-dd hh:mm"); 68 } 69 void linkLabelTitle_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 70 { 71 LinkLabel link = sender as LinkLabel; 72 System.Diagnostics.Process.Start("explorer.exe", link.Tag.ToString()); 73 } 74 75 private void timer_Tick(object sender, EventArgs e) 76 { 77 try 78 { 79 WebClient client = new WebClient(); 80 byte[] buffer = client.DownloadData("http://www.cnblogs.com/"); 81 //博客園采用的是utf-8 否則會出現亂碼 82 string html = Encoding.UTF8.GetString(buffer, 0, buffer.Length); 83 //cnblog簡單正則表達式 84 string regex = "<div\\s*class=\"post_item\">\\s*.*\\s*.*\\s*.*\\s*.*\\s*.*\\s*.*\\s*.*\\s*<div\\s*class=\"post_item_body\">\\s*<h3><a\\s*class=\"titlelnk\"\\s*href=\"(?<href>.*)\"\\s*target=\"_blank\">(?<title>.*)</a>.*\\s*<p\\s*class=\"post_item_summary\">\\s*(?<content>.*)\\s*</p>\\s*<div\\s*class=\"post_item_foot\">\\s*<a\\s*href=\".+?\"\\s*class=\"lightblue\">(?<author>.+?)</a>\\s*發布於\\s*(?<time>\\d{4}\\-\\d{2}\\-\\d{2}\\s*\\d{2}:\\d{2})"; 85 //過濾有標簽的content內容 將頭像src和內容過濾出來 86 string regex2 = "<a\\s+href=\"http://.+?\"\\s*target=\"_blank\"><img\\s*width=\"48\"\\s*height=\"48\"\\s*class=\"pfs\"\\s* src=\"(?<imgSrc>.+?)\".+?/></a>(?<content>.+)\\s*"; 87 MatchCollection matches = Regex.Matches(html, regex, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.CultureInvariant); 88 89 foreach (Match match in matches) 90 { 91 if (match.Success) 92 { 93 94 Blog blog = new Blog(); 95 blog.Url = match.Groups["href"].Value; 96 blog.Title = match.Groups["title"].Value; 97 //其中content中 如果用戶有頭像 則content包含img標簽 98 blog.Content = match.Groups["content"].Value; 99 Match m = Regex.Match(blog.Content, regex2); 100 if (m.Success) 101 { 102 if (!string.IsNullOrEmpty(m.Groups["imgSrc"].Value)) 103 { 104 blog.Header = m.Groups["imgSrc"].Value; 105 } 106 blog.Content = m.Groups["content"].Value; 107 } 108 blog.Author = match.Groups["author"].Value; 109 blog.Time = Convert.ToDateTime(match.Groups["time"].Value); 110 if (blog != null) 111 { 112 113 this.GetBlogs(this, new GetBlogsEventArgs(blog)); 114 } 115 } 116 } 117 } 118 catch (Exception ex) 119 { 120 myLogger.Error("錯誤信息", ex); 121 } 122 } 123 void panelContent_MouseDown(object sender, MouseEventArgs e) 124 { 125 //撲捉事件 126 WindowsHelper.ReleaseCapture(); 127 //發送消息給window Api 來實現 128 WindowsHelper.SendMessage(this.Handle, WindowsHelper.WM_SYSCOMMAND, WindowsHelper.SC_MOVE + WindowsHelper.HTCAPTION, 0);// 129 } 130 131 private void lblClose_Click(object sender, EventArgs e) 132 { 133 this.Hide(); 134 this.notifyIconCnblogs.Visible = true; 135 } 136 137 private void notifyIconCnblogs_Click(object sender, EventArgs e) 138 { 139 this.Visible = true; 140 this.WindowState = FormWindowState.Normal; 141 this.notifyIconCnblogs.Visible = true; 142 } 143 144 private void menuItem_Hide_Click(object sender, EventArgs e) 145 { 146 147 } 148 149 private void menuItem_Show_Click(object sender, EventArgs e) 150 { 151 152 } 153 154 private void menuItem_Aubot_Click(object sender, EventArgs e) 155 { 156 157 } 158 159 private void menuItem_Exit_Click(object sender, EventArgs e) 160 { 161 this.Close(); 162 } 163 } 164 }
總結
如果您有比較好的解決思路,請留言,或者加我的qq群,如果有比較好的UI也可以,不是做winform的,UI設計.....呵呵 希望以后這個小工具可以幫助更多的園友吧。
這里只是先簡單的實現了一部分功能,有些處理邏輯,並沒認真考慮,還是那句話,如果您有比較好的思路,請留言.....,沒認真考慮項目結構,也許以后功能都實現了,然后認真考慮一下,將其重構。
如果您覺得,小工具,能幫到你,不妨推薦一下,您的支持,是我堅持下去的動力.....
下次目標,有新文章時,窗口彈出。