C# 动态绘制任务栏图标的实现

通常我们在做一个应用时会遇到这样的需求:将收到的消息条数显示到任务栏,比如如下的效果

 

怎么实现呢?

 

答案是采用WindowsAPICodePack实现,具体参见:Windows 7 任务栏开发 之 覆盖图标(Overlay Icon)

 

当然你也可以采用我下面的笨方法,缺点是就是不够底层!!

代码如下:

 /// <summary>
/// 动态设置任务栏图标
/// </summary>
/// <param name="number"></param>
public void SetTaskIconDynamic(string number) { //动态绘制图标样式 Size size = this.Icon.Size; Bitmap cursorBitmap = new Bitmap(size.Width, size.Height); Graphics graphics = Graphics.FromImage(cursorBitmap); graphics.Clear(Color.FromArgb(0, 0, 0, 0)); graphics.ResetClip(); Rectangle rect = new Rectangle(0, 0, size.Width, size.Height);

//Gdi+自定义绘制图标 graphics.DrawImage(
this.Icon.ToBitmap(), rect); graphics.FillEllipse(new SolidBrush(Color.FromArgb(244,107,10)), new Rectangle(rect.Width / 2 - 2, rect.Height / 2 - 2, rect.Width / 2, rect.Height / 2)); graphics.DrawString(number, this.Font, Brushes.White, new Rectangle(rect.Width / 2 - 2, rect.Height / 2 - 2, rect.Width / 2, rect.Height / 2), new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center }); //生成Icon Icon cursor = Icon.FromHandle(cursorBitmap.GetHicon()); graphics.Dispose(); cursorBitmap.Dispose(); //更新任务栏图标样式 this.Icon = cursor; }

拿走,不谢!

 

转载于:https://www.cnblogs.com/shaozhuyong/p/5523827.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值