手機上的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 }