How can I display the number of messages dynamically by using the desktop App icon (similar to the QQ icon on a mobile phone )?, App icon
Apps on mobile phones, such as QQ and others, can dynamically display the number of messages on the icons (up to 99). Have you ever wondered how these effects are achieved? Can traditional applications developed on the desktop implement similar functions?
1. Train of Thought
- The icon of the desktop shortcut is generated based on an image in essence. The first option is to dynamically generate the icon (however, it is quite troublesome and consumes resources ), the recommended method is to pre-define the 100 icons from 0 to 99 (0 means that the number of messages is not displayed, and 99 is used as the number of messages> = 99 );
- Obtain the number of unprocessed messages of the user (generated based on the business situation, which is not the focus here. You can directly use a numerical simulation );
- First, determine whether the App desktop icon exists. If yes, delete it first. Then, based on the number of messages, search for the icon file of the corresponding number in the directory specified by the program and assign the value to the method for creating the desktop icon.
2. Image Resource Creation
You can find a png image with a transparent background (if an artist can design it by himself, I will use the Twitter icon here), and then open it with the Snagit Editor software, add a value label in the upper-right corner of the graph and save it as ICO. as shown in:
3. Project Structure
Create a C # desktop project, and create an icons folder to store icons of different codes (there is no need to create all the icons in the demo, and there are 2 to 3 icons as the demo). It is worth noting that, do not forget to click the icon, and assign the value to the output directory in its attribute panel. Otherwise, the icon cannot be found.
In addition, we need to introduce a COM library Windows Script Host Object Model to help us create shortcuts.
4. Core code
Create a dynamic icon directly in the loading event of the default Form1 form. Check the Code:
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 AppWithMsgCount12 {13 public partial class Form1: Form14 {15 public Form1 () 16 {17 InitializeComponent (); 18} 19 20 private void Form1_Load (obj Ect sender, EventArgs e) 21 {22 // how to remove the desktop shortcut icon arrow 23 // not tested !!!!! Cmd/k reg delete "HKEY_CLASSES_ROOT \ lnkfile"/v IsShortcut/f & taskkill/f/im assumer.exe & start assumer.exe 24 ShowMsgCountOnIcon (2 ); 25 26} 27 /// <summary> 28 // display Desktop shortcuts with messages 29 /// </summary> 30 /// <param name = "msgNum "> </param> 31 // <returns> </returns> 32 private bool ShowMsgCountOnIcon (int msgNum) 33 {34 try35 {36 DeleteShortcut (); 37 // int msgNum = 99; 38 CreateShortcut (msgNum); 39 this. text = string. format ("You have {0} messages", msgNum); 40 return true; 41} 42 catch (Exception ex) 43 {44 this. text = string. format ("error: {0}", ex. message); 45 return false; 46} 47 finally48 {49 50} 51} 52 // <summary> 53 // if there is a shortcut, delete it and refresh it, otherwise, the icon is not updated 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 iwshw.cut; 64. targetPath = Application. executablePath; 65 shortcut cut. workingDirectory = Application. startupPath; 66 // string appPath = AppDomain. currentDomain. baseDirectory. toString (); 67 string fullPath = Application. startupPath + @ "\ icons \"; 68 69 cut. iconLocation = string. format ("{0} I {1 }. ico ", fullPath, msgNum. toString (); 70 // your cut... 71. Sort cut. save (); 72 73} 74 // <summary> 75 // delete an existing APP shortcut 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 results
In order to demonstrate better results, the code above is slightly modified so that he can accept parameters in the command line and dynamically pass in the number of messages.
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 /// main entry point of the application. 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 else28 {29 Application. run (new Form1 (args [0]); 30} 31} 32} 33} 34}View Code