DSAPI 生成桌面图标(带数字)

时间:2021-12-04 15:48:50

功能:在桌面上创建一个带有指定数字的图标。

效果图:DSAPI 生成桌面图标(带数字)

生成的ICO图标

DSAPI 生成桌面图标(带数字)

代码

Private Sub 生成桌面图标(消息数量 As Integer)
Try
Dim B As New Bitmap(My.Resources.ICO)
Using G As Graphics = Graphics.FromImage(B)
G.Clip = New Region(New RectangleF(0, 0, B.Width, B.Height))
Dim Cnt As String = If(消息数量 > 999, "999+", 消息数量)
Dim Ft As Font = New Font("黑体", 24, FontStyle.Bold)
Dim Sz As Size = DSAPI.图形图像.获取绘制文字所需大小(Cnt, Ft)
Using Tb As Bitmap = DSAPI.图形图像.绘制圆角矩形(Sz + New Drawing.Size(6, 4), 8, Color.Red, Color.DarkRed, Color.Transparent)
G.DrawImage(Tb, New Rectangle(B.Width - Tb.Width, 0, Tb.Width, Tb.Height), New Rectangle(0, 0, Tb.Width, Tb.Height), GraphicsUnit.Pixel)
G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
G.DrawString(Cnt, Ft, Brushes.White, New Point(B.Width - Tb.Width + 6, 0))
End Using
End Using
Dim Pth As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim Stm As New IO.MemoryStream
DSAPI.图形图像.图像生成ICO图标(B, 128).Save(Stm)
IO.File.WriteAllBytes("c:\tmp.ico", Stm.ToArray)
Stm.Close()
Dim Lnk As New DSAPI.文件.快捷方式 With
{
.图标路径 = "c:\tmp.ico",
.文件路径或URL = "https://www.cnblogs.com/dylike/",
.提示说明 = "有新的消息"
}
Lnk.保存快捷方式到(String.Concat(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "\程序.lnk"))
Catch
End Try
End Sub