转载

C# 如何实现带消息数的App图标

上次写了一篇博文,但是每次更新图标时,桌面会闪烁(刷新),有博友说人家的图标都不会刷新,还能动画.我想了一下,如果要达到这个效果,可以用Form来实现,就是在Form中嵌入一个图片,然后用一个label来动态显示消息数,关键是将Form的边框隐藏,背景设为透明即可.如果要有旋转或者缩放动画,都可以用C#来实现.

  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  10 namespace AOPDemo 11 { 12     public partial class AppIconMsg : Form 13     { 14         public AppIconMsg() 15         { 16             InitializeComponent(); 17             //设置背景为透明 18             this.BackColor = Color.FromArgb(116, 164, 2); 19             this.TransparencyKey = this.BackColor;   20              21         } 22  23         private void AppIconMsg_Load(object sender, EventArgs e) 24         { 25             this.Width = 64; 26             this.Height = 64; 27             this.label1.Text = "99"; 28             this.timer1.Enabled = true; 29              30         } 31  32         // Drag it around the screen 33         private const int WM_NCHITTEST = 0x84; 34         private const int HTCAPTION = 0x2; 35         protected override void WndProc(ref Message m) 36         { 37             //Disable mouseDoubleClick on form 38             if (m.Msg == WM_LBUTTONDBLCLK) 39             { 40                 Form2 frm = new Form2(msg); 41                 frm.Show(); 42                 //this.Close(); 43                 return; 44             } 45  46             if (m.Msg == WM_NCLBUTTONDBLCLK) 47             { 48                 Form2 frm = new Form2(msg); 49                 frm.Show(); 50                // this.Close(); 51                 return; 52             } 53  54             //drag 55             if (m.Msg == WM_NCHITTEST) 56                 m.Result = new IntPtr(HTCAPTION); 57             else 58                 base.WndProc(ref m); 59         } 60         private int msg = 0; 61         private void timer1_Tick(object sender, EventArgs e) 62         { 63             int num = new Random().Next(1, 100); 64             msg = num; 65             this.label1.Text = num.ToString(); 66         } 67  68         const int WM_LBUTTONDBLCLK = 0x0203;//client area 69         const int WM_NCLBUTTONDBLCLK = 0x00A3;//non-client area 70         private void toolStripExit_Click(object sender, EventArgs e) 71         { 72             this.Close(); 73         } 74  75     } 76 } 

C# 如何实现带消息数的App图标

正文到此结束
Loading...