Files
TimerApp/IconGenerator.cs
2026-01-17 12:48:01 +08:00

68 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
namespace TimerApp
{
public static class IconGenerator
{
public static Icon GenerateClockIcon()
{
int size = 64;
using (Bitmap bmp = new Bitmap(size, size))
using (Graphics g = Graphics.FromImage(bmp))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.Clear(Color.Transparent);
// 绘制闹钟主体
int margin = 4;
int clockSize = size - margin * 2;
Rectangle rect = new Rectangle(margin, margin, clockSize, clockSize);
// 外圈
using (Pen pen = new Pen(Color.White, 4))
{
g.DrawEllipse(pen, rect);
}
// 两个耳朵 (闹钟铃)
using (Brush earBrush = new SolidBrush(Color.White))
{
// 左耳
g.FillPie(earBrush, 0, 0, size/2, size/2, 200, 50);
// 右耳
g.FillPie(earBrush, size/2, 0, size/2, size/2, 290, 50);
}
// 重绘外圈盖住耳朵连接处
using (Brush bgBrush = new SolidBrush(Color.FromArgb(30, 30, 30))) // 深色背景
{
g.FillEllipse(bgBrush, rect);
}
using (Pen pen = new Pen(Color.White, 3))
{
g.DrawEllipse(pen, rect);
}
// 指针
Point center = new Point(size / 2, size / 2);
using (Pen handPen = new Pen(Color.LightGreen, 3))
{
handPen.EndCap = LineCap.Round;
// 时针
g.DrawLine(handPen, center, new Point(center.X + 10, center.Y - 10));
// 分针
g.DrawLine(handPen, center, new Point(center.X, center.Y - 18));
}
// 转换为图标
// 注意GetHicon 需要释放
IntPtr hIcon = bmp.GetHicon();
return Icon.FromHandle(hIcon);
}
}
}
}