How to implement the desktop app icon to dynamically display the number of messages (similar to the QQ icon on the phone)?

Source: Internet
Author: User

The original: How to implement the desktop app icon to dynamically display the number of messages (similar to the QQ icon on the phone)?

The app on the phone, like QQ and so on can dynamically display the number of messages on the icon (up to 99), so have you ever wondered how these effects are implemented? Can a traditional application developed on the desktop also implement similar functionality?

1 Ideas
    • Desktop shortcut icons are essentially based on a picture, the first is the dynamic generation of icons (but it feels more trouble and consumes resources), the recommended way is predefined from 0 to 99 of these 100 icons (0 is not to display the number of messages, >=99 99 instead);
    • Gets the number of unhandled messages the user has (according to the business situation, here is not the focus, directly with a numerical simulation can be);
    • First determine whether the app desktop icon exists, the existence of first delete, and then according to the number of messages, to the program specified directory to search for the corresponding number of icon file, assigned to the method of creating desktop icons.
2 Image Resource Creation

You can find a PNG map with a transparent background (if you have artwork that you can design yourself, I use the twitter icon here), then open it with the SnagIt editor software, add a value callout in the upper-right corner of the diagram, and save it as an ICO format. As shown:

3 Project Structure

Create a new C # desktop project, and then make a icons folder to hold the different encoded icons (demo doesn't have to create all, there are 2 to 3 as a demo), it's worth noting that you must not forget to click on the icon, and then set the value in its properties panel to be assigned to the output directory, otherwise the icon cannot be found.

The other is to introduce a COM library, Windows Script Host Object Model, to help us create shortcuts.

4 Core Code

Dynamic icon creation processing directly in the default Form1 form Load event, see Code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Windows.Forms;9 usingiwshruntimelibrary;Ten usingSystem.IO; One namespaceAppwithmsgcount A { -      Public Partial classForm1:form -     { the          PublicForm1 () -         { - InitializeComponent (); -         } +  -         Private voidForm1_Load (Objectsender, EventArgs e) +         { A             //How to remove the desktop shortcut icon Arrows at             //!!!!! Not tested cmd/k reg Delete "Hkey_classes_root\lnkfile"/V isshortcut/f & taskkill/f/im Explorer.exe & Start Explorer.exe  -Showmsgcountonicon (2); -            -         } -         /// <summary> -         ///Show desktop shortcuts with number of messages in         /// </summary> -         /// <param name= "Msgnum" ></param> to         /// <returns></returns> +         Private BOOLShowmsgcountonicon (intmsgnum) -         { the             Try *             { $ deleteshortcut ();Panax Notoginseng                //int msgnum =; - createshortcut (msgnum); the                  This. Text =string. Format ("you have {0} messages", msgnum); +                 return true; A             } the             Catch(Exception ex) +             { -                  This. Text =string. Format ("error:{0}", ex. Message); $                 return false; $             } -             finally -             { the                  -             }Wuyi         } the         /// <summary> -         ///If previous shortcuts should be deleted and then refreshed, otherwise the icon will not be updated Wu         /// </summary> -         /// <param name= "path" ></param> About         /// <param name= "Iconnum" ></param> $         Private Static voidCreateShortcut (intmsgnum) -         { -             //using Iwshruntimelibrary;//> Ref > COM > Windows Script Host Object -             stringlink =Environment.getfolderpath (Environment.SpecialFolder.Desktop) A+ Path.directoryseparatorchar + application.productname +". lnk"; +             varShell =NewWshShell (); the             varShortcut = Shell. CreateShortcut (link) asIwshshortcut; -Shortcut. TargetPath =Application.executablepath; $Shortcut. WorkingDirectory =Application.startuppath; the             //string appPath = AppDomain.CurrentDomain.BaseDirectory.ToString (); the             stringFullPath = Application.startuppath +@"\icons\"; the  theShortcut. IconLocation =string. Format ("{0}i{1}.ico", FullPath, msgnum.tostring ()); -             //Shortcut ... in shortcut. Save (); the  the         } About         /// <summary> the         ///Delete an existing app shortcut the         /// </summary> the         Private voiddeleteshortcut () +         { -             varDesktop =Environment.getfolderpath (Environment.SpecialFolder.Desktop); the             varApp = Application.productname +". lnk";Bayi             if(System.IO.File.Exists (path.combine (desktop, app )) the             { the System.IO.File.Delete (path.combine (desktop, app)); -             } -         } the     } the}
5 Effects

To demonstrate a better effect, make a slight modification to the above code so that he can accept parameters at the command line and dynamically pass in the number of messages.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Windows.Forms;5 6 namespaceAppwithmsgcount7 {8     Static class Program9     {Ten         /// <summary> One         ///The main entry point for the application.  A         /// </summary> - [STAThread] -         Static voidMain (string[] args) the         { - application.enablevisualstyles (); -Application.setcompatibletextrenderingdefault (false); -             if(args = =NULL) +             { -Application.Run (NewForm1 (" About")); +             } A             Else { at                 if(args. Length = =0) -                 { -Application.Run (NewForm1 (" About")); -                 } -                 Else -                 { inApplication.Run (NewForm1 (args[0])); -                 } to         } +         } -     } the}
View Code

How to implement the desktop app icon to dynamically display the number of messages (similar to the QQ icon on the phone)?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.