// 将角度转为弧度
private double TransOrt2Radian(double fOrt)
{
double fPAI = 3.1415926535;
double fTmp = fOrt + 270;
if (fTmp > 360.0) fTmp -= 360.0;
return (fTmp * fPAI) / (float)180.0;
}
private Point GetPointInCircle(double dCtrx, double dCtry, double dOrt, double dRadius)
{
Point pnt1 = new Point(0, 0);
double dOrt1 = dOrt;
if (dOrt > 360.0) dOrt -= 360.0;
pnt1.X = dCtrx + dRadius * (float)Math.Cos(TransOrt2Radian(dOrt1));
pnt1.Y = dCtry + dRadius * (float)Math.Sin(TransOrt2Radian(dOrt1));
return pnt1;
}
// 根据提供的点获取一个封闭区域
private PathFigure GetLineFigure(Point pt1, Point []pts)
{
PathFigure pgtmp = new PathFigure();
pgtmp.StartPoint = pt1;
PathSegmentCollection psc = new PathSegmentCollection();
for (int i = 0; i < pts.Count(); i++)
{
Point pnt = pts[i];
LineSegment lg = new LineSegment();
lg.Point = pnt;
psc.Add(lg);
}
pgtmp.Segments = psc;
pgtmp.IsClosed = true;
pgtmp.IsFilled = true;
return pgtmp;
}
// 根据半径和中心点获取指针区域
private PathFigure GetTimePointer(double dCtrx, double dCtry,
double dRadius1, double dRadius2, double dRadius, double dOrt)
{
// 计算区域的四个点
Point pnt1 = GetPointInCircle(dCtrx, dCtry, dOrt + 180, dRadius1);
Point pnt2 = GetPointInCircle(dCtrx, dCtry, dOrt + 70, dRadius2);
Point pnt3 = GetPointInCircle(dCtrx, dCtry, dOrt - 70, dRadius2);
Point pnt4 = GetPointInCircle(dCtrx, dCtry, dOrt, dRadius);
PathFigure pgtmp = GetLineFigure(pnt1, new Point[3] { pnt2, pnt4, pnt3 });
return pgtmp;
}
private void DrawTimePointer(DrawingContext dc)
{
if (ActualWidth < 30) return;
double dCtrx = ActualWidth / 2;
double dCtry = ActualHeight / 2;
// 时针长度
double dHourRadius = ActualWidth * 7 / 20;
double dHourRadius1 = ActualWidth * 3 / 40;
double dHourRadius2 = ActualWidth / 20;
// 时针角度
double dOrt = 90.0;
// 分针长度
double dMinRadius = ActualWidth * 2 / 5;
double dMinRadius1 = ActualWidth *3 / 40;
double dMinRadius2 = dMinRadius1 / 2;
// 分针角度
double dMinOrt = 21;
// 计算区域
PathGeometry pg = new PathGeometry();
PathFigure pgtmp = GetTimePointer(dCtrx, dCtry, dHourRadius1, dHourRadius2, dHourRadius, dOrt);
pg.Figures.Add(pgtmp);
pg.Freeze();
dc.DrawGeometry(Brushes.Blue, new Pen(Brushes.Blue, 1.0), pg);
PathGeometry pg2 = new PathGeometry();
PathFigure pgtmp2 = GetTimePointer(dCtrx, dCtry, dMinRadius1, dMinRadius2, dMinRadius, dMinOrt);
pg2.Figures.Add(pgtmp2);
pg2.Freeze();
dc.DrawGeometry(Brushes.Yellow, new Pen(Brushes.Green, 1.0), pg2);
}
效果如图: