工作项目中需要实现0-180度范围内以15度为单位,高亮显示某些角度区间,网上找了些方法,写的比较零散,我自己总结了一下,方便后续使用,C#编写语言。
角度显示:
取消显示:
主要实现方法:
1.使用画刷填充扇形;
2.使用Pen画出边框;
实现代码如下:
namespace 画扇形
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
g = this.pictureBox2.CreateGraphics();
PBwidth = pictureBox2.Width-80 -1;
qidianX = 35;
qidianY = 20;
}
int PBwidth;//扇形长
int qidianX;//起点X位置
int qidianY;//起点Y位置
Graphics g;
private int qujianValue = 15;//扇形角度15°
private List<int> angleList = new List<int>();//角度列表
SolidBrush brush = new SolidBrush(Color.Orange);//橘色画刷
SolidBrush brushDispose = new SolidBrush(Color.White);//白画刷
Pen blackPen = new Pen(Color.Black, 1);//黑色边框笔
//显示角度
private void button1_Click(object sender, EventArgs e)
{
angleList.Clear();
angleList.Add(0);
angleList.Add(30);
angleList.Add(60);
angleList.Add(165);
for (int i = 0; i < 12; i++)
{
if (angleList.Contains(i * qujianValue))
{
g.FillPie(brush, qidianX, qidianY, PBwidth, PBwidth, 180 + i * qujianValue, 15);//需要高亮显示的扇形
}
else
{
g.FillPie(brushDispose, qidianX, qidianY, PBwidth, PBwidth, 180 + i * qujianValue, 15);//不需要高亮显示的扇形
}
g.DrawPie(blackPen, qidianX, qidianY, PBwidth, PBwidth, 180 + i * qujianValue, 15);//画黑色边框
}
}
//取消高亮
private void button2_Click(object sender, EventArgs e)
{
angleList.Clear();//清空角度列表
for (int i = 0; i < 12; i++)
{
if (angleList.Contains(i * qujianValue))
{
g.FillPie(brush, qidianX, qidianY, PBwidth, PBwidth, 180 + i * qujianValue, 15);
}
else
{
g.FillPie(brushDispose, qidianX, qidianY, PBwidth, PBwidth, 180 + i * qujianValue, 15);
}
g.DrawPie(blackPen, qidianX, qidianY, PBwidth, PBwidth, 180 + i * qujianValue, 15);
}
}
}
}