Fork me on GitHub

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 }
复制代码

posted @   JackWang-CUMT  阅读(3706)  评论(6编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示