WinForm制作定时显示基金净值的桌面小工具
时间:2014-03-07 09:51:24
收藏:0
阅读:1665
基金网每隔一分钟会更新净值估算,每次要打开网站看挺麻烦的,于是制作了一个桌面小工具,在桌面顶层并且透明显示,每隔一分钟抓取数据显示。
净值估算例子如下:
http://fund.eastmoney.com/000167.html

<div id="statuspzgz" class="fundpz"><span class="green bold">1.1843</span><div id="statuszdf" class="fundzf"><p class="green">-0.0067</p><p class="green">-0.56%</p></div><p class="time">2014-03-06 15:00</p></div>
解析用到的路径为:
//div[@id=‘statuspzgz‘]/span
//div[@id=‘statuszdf‘]/p[1]
//div[@id=‘statuszdf‘]/p[2]
//div[@id=‘statuspzgz‘]/p[@class=‘time‘]
净值估算例子如下:
http://fund.eastmoney.com/000167.html
开发完成后的结果如下:
开发过程:
http://www.cnblogs.com/echosong/p/3500563.html
2、在中间放一个Label显示信息、在工具栏拉一个ContextMenuStrip和NotifyIcon作为最小化到托盘时的显示和退出菜单操作,其中notifyIcon1的ContextMenuStrip属性设为contextMenuStrip1,Icon属性设为本地一个IOCN图标文件。
<div id="statuspzgz" class="fundpz"><span class="green bold">1.1843</span><div id="statuszdf" class="fundzf"><p class="green">-0.0067</p><p class="green">-0.56%</p></div><p class="time">2014-03-06 15:00</p></div>
解析用到的路径为:
//div[@id=‘statuspzgz‘]/span
//div[@id=‘statuszdf‘]/p[1]
//div[@id=‘statuszdf‘]/p[2]
//div[@id=‘statuspzgz‘]/p[@class=‘time‘]
完整代码如下:
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(() => Dispatch(cts));
}
CancellationTokenSource cts = new CancellationTokenSource();
private void Dispatch(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
WinForm制作定时显示基金净值的桌面小工具,布布扣,bubuko.com
原文:http://blog.csdn.net/gdjlc/article/details/20662491
评论(0)