网站首页 > 技术文章 正文
绘制按钮颜色列表:
private List<Color> buttonColors; // 按钮颜色列表
private int activeButtonIndex = -1; // 激活的按钮索引
public _55l2()
{
InitializeComponent();
buttonColors = new List<Color>
{
Color.Green,
Color.Blue,
Color.Red,
Color.Yellow,
Color.Orange
};
}
绘制按钮扇形或甜甜圈
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1);
// 绘制按钮扇形
if (buttonColors.Count > 0)
{
float startAngle = -90;
float sweepAngle = 360f / buttonColors.Count;
for (int i = 0; i < buttonColors.Count; i++)
{
using (SolidBrush buttonBrush = new SolidBrush(buttonColors[i]))
{
if (i == activeButtonIndex)
{
g.FillPie(buttonBrush, rect, startAngle, sweepAngle);
}
else
{
g.FillPie(buttonBrush, rect, startAngle, sweepAngle - 1);
}
}
startAngle += sweepAngle;
}
}
// 绘制按钮扇形或甜甜圈
if (buttonColors.Count > 0)
{
float innerRectRadius = rect.Height/2/2-Width/2/8; // 调整甜甜圈中心的圆的大小
for (int i = 0; i < buttonColors.Count; i++)
{
using (SolidBrush buttonBrush = new SolidBrush(buttonColors[i]))
{
if (i == activeButtonIndex)
{
g.FillEllipse(buttonBrush, rect.X + innerRectRadius, rect.Y + innerRectRadius, rect.Width - 2 * innerRectRadius, rect.Height - 2 * innerRectRadius);
// 添加文本
string buttonText = (i + 1) + "号";
SizeF textSize = g.MeasureString(buttonText, new Font(Font.FontFamily, 100)); // 调整字体大小为10
PointF textPosition = new PointF(rect.X + (rect.Width - textSize.Width) / 2, rect.Y + (rect.Height - textSize.Height) / 2);
g.DrawString(buttonText, new Font(Font.FontFamily, 100), Brushes.Pink, textPosition); // 调整字体大小为10
}
}
}
引发按钮点击事件
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
float startAngle = -90;
float sweepAngle = 360f / buttonColors.Count;
for (int i = 0; i < buttonColors.Count; i++)
{
GraphicsPath buttonPath = new GraphicsPath();
buttonPath.AddPie(new Rectangle(0, 0, Width - 1, Height - 1), startAngle, sweepAngle);
if (buttonPath.IsVisible(e.Location))
{
activeButtonIndex = i;
Invalidate();
break;
}
startAngle += sweepAngle;
}
}
猜你喜欢
- 2024-11-12 C#-练习题 014(c#程序题及答案)
- 2024-11-12 C# 类型转换(c类型转换有哪三种形式)
- 2024-11-12 C#-无参数有返回值的方法 060(有参无返回值 c语言)
- 2024-11-12 C#中的值类型和引用类型(c# 引用类型)
- 2024-11-12 abelkhan中的rpc框架(rpc框架应用场景)
- 2024-11-12 C#编程零基础到入门学习-C# 值类型和引用类型(4-8)
- 2024-11-12 2.6 C#的常用关键字和预定义类型(c#预留关键字)
- 2024-11-12 C#_图片拖拽(c#窗体图片移动)
- 2024-11-12 数据类型 - C#入门教程(c#常见数据类型)
- 2024-11-12 C#中堆栈(Stack)和堆(Heap)的区别——第一部分
- 02-21走进git时代, 你该怎么玩?_gits
- 02-21GitHub是什么?它可不仅仅是云中的Git版本控制器
- 02-21Git常用操作总结_git基本用法
- 02-21为什么互联网巨头使用Git而放弃SVN?(含核心命令与原理)
- 02-21Git 高级用法,喜欢就拿去用_git基本用法
- 02-21Git常用命令和Git团队使用规范指南
- 02-21总结几个常用的Git命令的使用方法
- 02-21Git工作原理和常用指令_git原理详解
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)