Fork me on GitHub

如何实现桌面App图标可以动态显示消息数(类似手机上的QQ图标)?

  手机上的APP , 像QQ和微信等都可以在图标上动态显示消息数(最大99) , 那么你有没有想过这些效果是如何实现的?桌面上开发的传统应用程序能否也实现类似的功能?

 1 思路

  •  桌面快捷方式的图标本质上就是基于一个图片产生的 , 第一种是动态生成图标(不过感觉比较费事且也消耗资源) , 建议方式是预先定义从0到99这100个图标(0就是不显示消息数 , >=99的就用99代替);
  •  获取用户的未处理消息数(根据业务情况产生 , 这里不是重点 , 直接用一个数值模拟即可);
  •  先判断该App桌面图标是否存在,存在先删除,然后根据消息数,到程序指定目录下搜寻对应编号的图标文件 , 赋值到创建桌面图标的方法中即可.

2 图片资源制作

   可以找一个透明背景的png图(如果有美工可以进行自行设计 , 我这里用的Twitter的图标) , 然后用Snagit Editor软件打开 , 在图的右上角添加一个数值标注 , 然后另存为ICO格式.如下图所示:

3 项目结构

     新建一个C#桌面项目 , 然后创建一个icons文件夹来存放不同编码的图标(演示没必要创建所有 , 有2 到3个作为演示即可) , 值得注意的是 , 一定不要忘了在图标上单击 , 然后在其属性面板中设置将赋值到输出目录 , 否则找不到该图标.

  另外就是要引入一个COM库Windows Script Host Object Model , 来帮助我们创建快捷方式.

4 核心代码

  直接在默认的Form1窗体加载事件中进行动态图标创建处理 , 看代码:

 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.Text;
 8 using System.Windows.Forms;
 9 using IWshRuntimeLibrary;
10 using System.IO;
11 namespace AppWithMsgCount
12 {
13     public partial class Form1 : Form
14     {
15         public Form1()
16         {
17             InitializeComponent();
18         }
19 
20         private void Form1_Load(object sender, EventArgs e)
21         {
22             //如何去除桌面快捷方式图标箭头
23             //未测试!!!!!   cmd /k reg delete "HKEY_CLASSES_ROOT\lnkfile" /v IsShortcut /f & taskkill /f /im explorer.exe & start explorer.exe
24             ShowMsgCountOnIcon(2);
25           
26         }
27         /// <summary>
28         /// 显示带消息数目的桌面快捷方式
29         /// </summary>
30         /// <param name="msgNum"></param>
31         /// <returns></returns>
32         private bool ShowMsgCountOnIcon(int msgNum)
33         {
34             try
35             {
36                 DeleteShortcut();
37                // int msgNum = 99;
38                 CreateShortcut(msgNum);
39                 this.Text = string.Format("您有{0}个消息", msgNum);
40                 return true;
41             }
42             catch (Exception ex)
43             {
44                 this.Text = string.Format("error:{0}", ex.Message);
45                 return false;
46             }
47             finally
48             {
49                 
50             }
51         }
52         /// <summary>
53         /// 如果之前有快捷方式应该先删除再刷新,否则图标不更新
54         /// </summary>
55         /// <param name="path"></param>
56         /// <param name="iconNum"></param>
57         private static void CreateShortcut(int msgNum)
58         {
59             // using IWshRuntimeLibrary; // > Ref > COM > Windows Script Host Object  
60             string link = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
61                 + Path.DirectorySeparatorChar + Application.ProductName + ".lnk";
62             var shell = new WshShell();
63             var shortcut = shell.CreateShortcut(link) as IWshShortcut;
64             shortcut.TargetPath = Application.ExecutablePath;
65             shortcut.WorkingDirectory = Application.StartupPath;
66             //  string appPath = AppDomain.CurrentDomain.BaseDirectory.ToString();
67             string fullPath = Application.StartupPath + @"\icons\";
68 
69             shortcut.IconLocation = string.Format("{0}i{1}.ico", fullPath, msgNum.ToString());
70             //shortcut...
71             shortcut.Save();
72 
73         }
74         /// <summary>
75         /// 删除已有的APP快捷方式
76         /// </summary>
77         private void DeleteShortcut()
78         {
79             var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
80             var app = Application.ProductName + ".lnk";
81             if(System.IO.File.Exists(Path.Combine(desktop, app)))
82             {
83                 System.IO.File.Delete(Path.Combine(desktop, app));
84             }
85         }
86     }
87 }

5 效果

  为了演示的效果更好 , 对上面的代码稍作修改 , 让他可以在命令行接受参数 , 动态传入消息数.

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Windows.Forms;
 5 
 6 namespace AppWithMsgCount
 7 {
 8     static class Program
 9     {
10         /// <summary>
11         /// 应用程序的主入口点。
12         /// </summary>
13         [STAThread]
14         static void Main(String[] args)
15         {
16             Application.EnableVisualStyles();
17             Application.SetCompatibleTextRenderingDefault(false);
18             if (args == null)
19             {
20                 Application.Run(new Form1("99"));
21             }
22             else {
23                 if (args.Length == 0)
24                 {
25                     Application.Run(new Form1("99"));
26                 }
27                 else
28                 {
29                     Application.Run(new Form1(args[0]));
30                 }
31         }
32         }
33     }
34 }
View Code

 

posted @ 2015-12-05 06:21  JackWang-CUMT  阅读(6685)  评论(3编辑  收藏  举报