画一个大盖帽的箭头 [英] Draw an arrow with big cap

查看:97
本文介绍了画一个大盖帽的箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi开发人员:

我想使用C#绘制箭头
我可以在下面写代码片段:

hi developers:

I want to draw an arrow using C#

I can write the snippet below :

protected override void OnPaint(PaintEventArgs e)
{
   base.OnPaint(e);
   Pen p = new Pen(Color.Blue, 1);
   p.StartCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
   p.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
   e.Graphics.DrawLine(p, 20, 20, 100, 100);
}



但是问题是:笔帽会很小,所以有没有地方可以解决这个问题((不改变笔的宽度))

在此先感谢



but the problem is that : the cap is going to be very small , so is there away to get around this problem ((without changing Pen width ))

thanks in advance

推荐答案

尝试使用此方法获得大箭头.您可以在AdjustableArrowCap构造函数中将箭头的大小调整为所需的任何大小,而无需更改笔的宽度.请确保为此参考System.Drawing.Drawing2D.

Try this approach for getting a big arrow. You can adjust the size of the arrow in the AdjustableArrowCap constructor to be any size you need, without changing the width of the pen. Be sure to reference System.Drawing.Drawing2D for this.

AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);
Pen p = new Pen(Color.Blue, 1);
p.CustomEndCap = bigArrow;
e.Graphics.DrawLine(p, 20, 20, 100, 100);



这里的一个优点是,无论您是绘制直线,曲线还是其他东西,箭头的方向都将是合适的.另外,它比执行您自己的例程要快.



One advantage here is that the orientation of the arrow will be suitable regardless of whether you''re drawing a straight line, curve or whatever. Plus, it''s faster than executing your own routine.


您可以编写自己的小例程.

我刚刚编写了自己的DrawArrow例程:
you can write your own little routine.

I just wrote up my own DrawArrow routine:
private void DrawArrow(Graphics g, PointF ArrowStart, PointF ArrowEnd, Color ArrowColor, int LineWidth, int ArrowMultiplier)
{
    //create the pen
    Pen p = new Pen(ArrowColor, LineWidth);

    //draw the line
    g.DrawLine(p, ArrowStart, ArrowEnd);

    //determine the coords for the arrow point

    //tip of the arrow
    PointF arrowPoint = ArrowEnd;

    //determine arrow length
    double arrowLength = Math.Sqrt(Math.Pow(Math.Abs(ArrowStart.X - ArrowEnd.X), 2) +
                                   Math.Pow(Math.Abs(ArrowStart.Y - ArrowEnd.Y), 2));

    //determine arrow angle
    double arrowAngle = Math.Atan2(Math.Abs(ArrowStart.Y - ArrowEnd.Y),Math.Abs(ArrowStart.X - ArrowEnd.X));

    //get the x,y of the back of the point

    //to change from an arrow to a diamond, change the 3
    //in the next if/else blocks to 6

    double pointX, pointY;
    if (ArrowStart.X > ArrowEnd.X)
    {
        pointX = ArrowStart.X - (Math.Cos(arrowAngle) * (arrowLength - (3 * ArrowMultiplier)));
    }
    else
    {
        pointX = Math.Cos(arrowAngle) * (arrowLength - (3 * ArrowMultiplier)) + ArrowStart.X;
    }

    if (ArrowStart.Y > ArrowEnd.Y)
    {
        pointY = ArrowStart.Y - (Math.Sin(arrowAngle) * (arrowLength - (3 * ArrowMultiplier)));
    }
    else
    {
        pointY = (Math.Sin(arrowAngle) * (arrowLength - (3 * ArrowMultiplier))) + ArrowStart.Y;
    }

    PointF arrowPointBack = new PointF((float)pointX, (float)pointY);

    //get the secondary angle of the left tip
    double angleB = Math.Atan2((3 * ArrowMultiplier), (arrowLength - (3 * ArrowMultiplier)));

    double angleC = Math.PI * (90 - (arrowAngle * (180 / Math.PI)) - (angleB * (180 / Math.PI))) / 180;

    //get the secondary length
    double secondaryLength = (3 * ArrowMultiplier)/Math.Sin(angleB);

    if (ArrowStart.X > ArrowEnd.X)
    {
        pointX = ArrowStart.X - (Math.Sin(angleC) * secondaryLength);
    }
    else
    {
        pointX = (Math.Sin(angleC) * secondaryLength) + ArrowStart.X;
    }

    if (ArrowStart.Y > ArrowEnd.Y)
    {
        pointY = ArrowStart.Y - (Math.Cos(angleC) * secondaryLength);
    }
    else
    {
        pointY = (Math.Cos(angleC) * secondaryLength) + ArrowStart.Y;
    }

    //get the left point
    PointF arrowPointLeft = new PointF((float)pointX, (float)pointY);

    //move to the right point
    angleC = arrowAngle - angleB;

    if (ArrowStart.X > ArrowEnd.X)
    {
        pointX = ArrowStart.X - (Math.Cos(angleC) * secondaryLength);
    }
    else
    {
        pointX = (Math.Cos(angleC) * secondaryLength) + ArrowStart.X;
    }

    if (ArrowStart.Y > ArrowEnd.Y)
    {
        pointY = ArrowStart.Y - (Math.Sin(angleC) * secondaryLength);
    }
    else
    {
        pointY = (Math.Sin(angleC) * secondaryLength) + ArrowStart.Y;
    }

    PointF arrowPointRight = new PointF((float)pointX,(float)pointY);

    //create the point list
    PointF[] arrowPoints = new PointF[4];
    arrowPoints[0] = arrowPoint;
    arrowPoints[1] = arrowPointLeft;
    arrowPoints[2] = arrowPointBack;
    arrowPoints[3] = arrowPointRight;

    //draw the outline
    g.DrawPolygon(p, arrowPoints);

    //fill the polygon
    g.FillPolygon(new SolidBrush(ArrowColor), arrowPoints);
}



为了测试它,我在表单中添加了一个面板和两个文本框,并添加了两个简单的鼠标事件:



to test it, I added a panel and two textboxes to a form and added two simple mouse events:

PointF mouseStart;
PointF mouseEnd;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    mouseStart = new PointF(e.X, e.Y);
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
    mouseEnd = new PointF
(e.X, e.Y);
    panel1.CreateGraphics().Clear(Form1.DefaultBackColor);
    DrawArrow(panel1.CreateGraphics(), mouseStart, mouseEnd, Color.Black,
              Convert.ToInt16(txtLineWidth.Text), Convert.ToInt16(txtArrowMultiplier.Text));
}



像魅力一样运作.您可以单独更改线宽或箭头的大小.

[修改:从Point更改为PointF,以获得更精确的点,还包含有关如何从箭头更改为菱形的注释...(需要更改计算arrowPointBack的代码)]



worked like a charm. You can change the line width or the size of the arrow independently.

[Modified: changed from Point to PointF for slightly more accurate points, also included a comment on how to change from an arrow to a diamond...(requires changing the code that calculates the arrowPointBack)]


Pen p = new Pen(Color.Black, 10);
     p.StartCap = LineCap.Round;
     p.EndCap = LineCap.ArrowAnchor;
     g.DrawLine(p, 30, 30, 80, 30);
     p.Dispose();


您可以阅读此链接

点击




如果有帮助,请投票并接受答案.


You can read this link

Click




Please vote and Accept Answer if it Helped.


这篇关于画一个大盖帽的箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆