# 🔧 角度/高度选择器控件 for .NET

Updated on Mar. 5, 2018

# 实现

## 点和角度的函数

private PointF DegreesToXY(float degrees, float radius, Point origin)
{
PointF xy = new PointF();
double radians = degrees * Math.PI / 180.0;
return xy;
}


#### 注意

private float XYToDegrees(Point xy, Point origin)
{
double angle = 0.0;
if (xy.Y < origin.Y)
{
if (xy.X > origin.X)
{
angle = (double)(xy.X - origin.X) / (double)(origin.Y - xy.Y);
angle = Math.Atan(angle);
angle = 90.0 - angle * 180.0 / Math.PI;
}
else if (xy.X < origin.X)
{
//如此这般
}
}
else if (xy.Y > origin.Y)
{
//如此这般
}
if (angle > 180) angle -= 360; //控制角度范围
return (float)angle;
}


## 绘制控件

• 用宽度为2的Pen绘制外圈圆
• 用 40% 不透明度的白色填充
• 控件中心是 3×3 像素的正方形
protected override void OnPaint(PaintEventArgs e)
{
//...

//Draw
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawEllipse(outline, drawRegion);
g.FillEllipse(fill, drawRegion);

//...

g.SmoothingMode = SmoothingMode.HighSpeed;
g.FillRectangle(Brushes.Black, originSquare);

//...
}


## 处理用户点击

private int findNearestAngle(Point mouseXY)
{
int thisAngle = (int)XYToDegrees(mouseXY, origin);
if (thisAngle != 0)
return thisAngle;
else
return -1;
}


private int findAltitude(Point mouseXY)
{
float distance = getDistance(mouseXY, origin);
int alt = 90 - (int)(90.0f * (distance / origin.X));
if (alt < 0) alt = 0;
return alt;
}


## 自定义事件

public delegate void AngleChangedDelegate();
public event AngleChangedDelegate AngleChanged;


# 下载

Demo：点击下载

The End. $\Box$

posted @ 2018-03-06 00:53  Conmajia  阅读(793)  评论(0编辑  收藏  举报