• [工具]类QQ消息通知,提示博客园发布新文章(二)——托盘实现


    引言

     上一篇简单介绍了,该工具的实现目标,这里先添加托盘功能,鉴于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 }
    View Code

    总结

    如果您有比较好的解决思路,请留言,或者加我的qq群,如果有比较好的UI也可以,不是做winform的,UI设计.....呵呵 希望以后这个小工具可以帮助更多的园友吧。
    这里只是先简单的实现了一部分功能,有些处理逻辑,并没认真考虑,还是那句话,如果您有比较好的思路,请留言.....,没认真考虑项目结构,也许以后功能都实现了,然后认真考虑一下,将其重构。

    如果您觉得,小工具,能帮到你,不妨推荐一下,您的支持,是我坚持下去的动力.....

    下次目标,有新文章时,窗口弹出。

  • 相关阅读:
    CSS hack
    字符串中常用的方法
    排序算法
    拾遗
    数组类型检测
    数组常用的方法
    go 文件服务器(标准库) 添加关机,睡眠,退出功能
    go cmd 交互 初始化执行某些命令
    go 内网IP及外网IP获取
    go 快排实现
  • 原文地址:https://www.cnblogs.com/wolf-sun/p/3504600.html
Copyright © 2020-2023  润新知