/// <summary> |
/// 仿qq非直线箭头:采用填充多边形的方法 |
/// </summary> |
/// <param name="g"></param> |
/// <param name="p"></param> |
/// <param name="start"></param> |
/// <param name="end"></param> |
private void DrawUArrowF(Graphics g, Pen p, Point start, Point end) |
{ |
int par = 5; |
par = (( int )p.Width - 1) * 5 + par; //默认为10 |
double Length = Math.Sqrt(Math.Abs(start.X - end.X) * Math.Abs(start.X - end.X) + Math.Abs(start.Y - end.Y) * Math.Abs(start.Y - end.Y)); |
if (Length > 5) |
{ |
g.CompositingQuality = CompositingQuality.HighQuality; |
double k = Math.Atan2(( double )(end.Y - start.Y), ( double )(end.X - start.X)); //返回的是原点至点(x,y)的方位角,即与 x 轴的夹角 |
double siny = Math.Sin(k); |
double cosy = Math.Cos(k); |
//箭头是一个底边=高=par的等腰三角形 |
//运用正弦、余弦与边之间的关系;sin30°=cos60°求出坐标 |
PointF point1 = new PointF(end.X + ( float )(-par * cosy - (par / 2.0 * siny)), end.Y + ( float )(-par * siny + (par / 2.0 * cosy))); |
PointF point2 = new PointF(end.X + ( float )(-par * cosy + (par / 2.0 * siny)), end.Y - ( float )(par / 2.0 * cosy + par * siny)); |
PointF[] ps = { end, point1, point2 }; |
//g.DrawPolygon(p, ps);//画三角形会出现偏移量的问题 |
float offsetX = ( float )(par * siny / 3.5); |
float offsetY = ( float )(par * cosy / 3.5); |
PointF point3 = new PointF(end.X + ( float )(-par * cosy - (par / 2.0 * siny)) + offsetX, end.Y + ( float )(-par * siny + (par / 2.0 * cosy)) - offsetY); |
PointF point4 = new PointF(end.X + ( float )(-par * cosy + (par / 2.0 * siny) - offsetX), end.Y - ( float )(par / 2.0 * cosy + par * siny) + offsetY); |
PointF[] pj = { start, point3, point4 }; |
//g.DrawPolygon(p, pj);//画三角形会出现偏移量的问题 |
FillMode flMode = FillMode.Alternate; |
SolidBrush brush = new SolidBrush(p.Color); |
//g.FillPolygon(brush, ps, flMode);//填充三角形箭头部 |
//g.FillPolygon(brush, pj, flMode);//填充三角形箭身 |
//两个三角形组合出现缝隙 |
PointF[] pp = { start, point3, point1, end, point2, point4 }; //填充箭头多边形 |
g.FillPolygon(brush, pp, flMode); |
Pen px = new Pen(p.Color, 1); //新定义一个pen画一条直线,防止箭尾太细 |
g.DrawLine(px, start, end); |
px.Dispose(); |
} |
} |