开发完成后的结果如下:
开发过程:
http://www.cnblogs.com/echosong/p/3500563.html
2、在中间放一个Label显示信息、在工具栏拉一个ContextMenuStrip和NotifyIcon作为最小化到托盘时的显示和退出菜单操作,其中notifyIcon1的ContextMenuStrip属性设为contextMenuStrip1,Icon属性设为本地一个IOCN图标文件。
完整代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using HtmlAgilityPack; using System.Threading.Tasks; using System.Threading; using System.Runtime.InteropServices; namespace Demo { public partial class Form8 : Form { const string url = "http://fund.eastmoney.com/000167.html"; public Form8() { InitializeComponent(); lblInfo.Text = ""; this.ShowInTaskbar = false;//任务栏不显示 this.Opacity = 0.60;//透明度 this.TopMost = true;//顶层显示 Task.Factory.StartNew(() => Do(cts)); } CancellationTokenSource cts = new CancellationTokenSource(); private void Do(CancellationTokenSource cts) { while (!cts.IsCancellationRequested) { string html = Utils.GetHtmlSource(url, Encoding.UTF8); if (string.IsNullOrEmpty(html)) return; HtmlNode rootNode = null; HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument(); document.LoadHtml(html); rootNode = document.DocumentNode; string s1 = GetNodeText(rootNode, "//div[@id='statuspzgz']/span"); string s2 = GetNodeText(rootNode, "//div[@id='statuszdf']/p[1]"); string s3 = GetNodeText(rootNode, "//div[@id='statuszdf']/p[2]"); string s4 = GetNodeText(rootNode, "//div[@id='statuspzgz']/p[@class='time']"); this.SafeCall(() => { lblInfo.Text = s1 + "\r\n" + s2 + "\r\n" + s3 + "\r\n" + s4; }); Thread.Sleep(1000 * 60); //每隔60s查询 } } private string GetNodeText(HtmlNode rootNode, string path) { HtmlNode temp = rootNode.SelectSingleNode(path); if (temp != null) return temp.InnerText; return ""; } [DllImport("user32.dll")] //方法扑捉 public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; void Panel2MouseDown(object sender, MouseEventArgs e) { //扑捉事件 ReleaseCapture(); //发送消息给window Api 来实现 SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);// } private void label2_Click(object sender, EventArgs e) { Application.Exit(); } /// <summary> /// 隐藏窗体,并显示托盘图标 /// </summary> private void HideForm() { this.Visible = false; this.WindowState = FormWindowState.Minimized; notifyIcon1.Visible = true; } /// <summary> /// 显示窗体 /// </summary> private void ShowForm() { this.Visible = true; this.WindowState = FormWindowState.Normal; notifyIcon1.Visible = false; } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { ShowForm(); } private void ToolStripMenuItemShow_Click(object sender, EventArgs e) { ShowForm(); } private void toolStripMenuItemExit_Click(object sender, EventArgs e) { Application.Exit(); } private void label1_Click(object sender, EventArgs e) { HideForm(); } } }其中GetHtmlSource方法可以参考另一文章:
C#采集CSDN单个博客所有文章
http://blog.csdn.net/gdjlc/article/details/11620915