就像这样中间的红线是我画的直线,外面是自动生成的,外边两条直线和中间线水平且长度一样,虚线距离是一样的,就是输入框内大小,所以外面类似是个圆,圆的中心就是线边的端点,生成这个区域,用算法吗?
------解决思路----------------------
做了个比较简单的,水平方向画没有问题; 代码如下。 如果需要功能,留个邮箱
// 半径
private int iR = 10;
// 记录鼠标移动点
private List<Point> listDraw = new List<Point>();
// 描画开始点的半圆
private void DrawFistPoint(Point pFirst, Graphics g)
{
Pen penGray = new Pen(Color.Gray, 1);
Rectangle rc = new Rectangle(pFirst.X - iR, pFirst.Y - iR, 2 * iR, 2 * iR);
g.DrawPie(penGray, rc, 90, 180);
Pen penWhite= new Pen(Color.White, 1);
g.DrawLine(penWhite, pFirst.X, pFirst.Y - iR, pFirst.X, pFirst.Y + iR);
}
// 描画结束点的半圆
private void DrawLastPoint(Point pLast, Graphics g)
{
Pen penGray = new Pen(Color.Gray, 1);
Rectangle rc = new Rectangle(pLast.X - iR, pLast.Y - iR, 2 * iR, 2 * iR);
g.DrawPie(penGray, rc, 270, 180);
Pen penWhite = new Pen(Color.White, 1);
g.DrawLine(penWhite, pLast.X, pLast.Y - iR, pLast.X, pLast.Y + iR);
}
private void DrawLine(List<Point> list, Graphics g)
{
if(null != list && list.Count > 1)
{
Pen penBlack = new Pen(Color.Black, 1);
Pen penGray = new Pen(Color.Gray, 1);
Point pFirst = list[0];
Point pLast = list[list.Count - 1];
// 描画开始点的半圆
DrawFistPoint(pFirst, g);
// 描画结束点的半圆
DrawLastPoint(pLast, g);
// 获取并描画上侧线
List<Point> ptUp = GetUpLine(list);
if (null != ptUp && ptUp.Count > 1)
{
g.DrawLines(penGray, ptUp.ToArray());
}
// 描画线
g.DrawLines(penBlack, list.ToArray());
// 描画下侧线
List<Point> ptDown = GetDownLine(list);
if (null != ptDown && ptDown.Count > 1)
{
g.DrawLines(penGray, ptDown.ToArray());
}
}
}
// 计算上侧线, Y - 半径; X不变
private List<Point> GetUpLine(List<Point> list)
{
List<Point> listTemp = null;
if (null != list && list.Count > 0)
{
listTemp = new List<Point>();
for (int i = 0; i < list.Count; i++ )
{
Point pt = list[i];
Point ptTemp = new Point();
ptTemp.X = pt.X;
ptTemp.Y = pt.Y - iR;
listTemp.Add(ptTemp);
}
}
return listTemp;
}
// 计算下侧线, Y + 半径; X不变
private List<Point> GetDownLine(List<Point> list)
{
List<Point> listTemp = null;
if (null != list && list.Count > 0)
{
listTemp = new List<Point>();
for (int i = 0; i < list.Count; i++)
{
Point pt = list[i];
Point ptTemp = new Point();
ptTemp.X = pt.X;
ptTemp.Y = pt.Y + iR;
listTemp.Add(ptTemp);
}
}
return listTemp;
}
------解决思路----------------------
数学实在太差,就画三根线(数学太差似乎不大适合做程序员)
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
Pen p1 = new Pen(Color.Black,30);
p1.EndCap = LineCap.Round;
p1.StartCap = LineCap.Round;
Pen p2 = new Pen(Color.White, 29);
p2.EndCap = LineCap.Round;
p2.StartCap = LineCap.Round;
Pen p = Pens.Black;
Point pt1 = new Point(100, 100);
Point pt2 = new Point(200, 200);
e.Graphics.DrawLine(p1, pt1, pt2);
e.Graphics.DrawLine(p2, pt1, pt2);
e.Graphics.DrawLine(p, pt1, pt2);