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; }

拿走,不謝!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM