在C#中的Picturebox上绘制一个箭头

Chr*_*con 2 c# graphics winforms

我希望能够从一个鼠标点击位置绘制一个直箭头到另一个,就像你在PowerPoint中一样.它也需要能够在PictureBox上绘图.

Alb*_*nbo 9

下面是一些基本代码,用于在图片框中从鼠标下拉到当前位置.
你只需要为箭头绘制更多的线条或三角形.

public partial class Form1 : Form
{
    private bool isMoving = false;
    private Point mouseDownPosition = Point.Empty;
    private Point mouseMovePosition = Point.Empty;
    private List<Tuple<Point, Point>> lines = new List<Tuple<Point, Point>>();
    public Form1()
    {
        InitializeComponent();

        // 
        // pictureBox1
        // 
        this.pictureBox1.Location = new System.Drawing.Point(0, 0);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(231, 235);
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
        this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
        this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
        this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
        this.Controls.Add(this.pictureBox1);
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        var g = e.Graphics;
        if (isMoving)
        {
            g.Clear(pictureBox1.BackColor);
            g.DrawLine(Pens.Black, mouseDownPosition, mouseMovePosition);
            foreach (var line in lines)
            {
                g.DrawLine(Pens.Black, line.Item1, line.Item2);
            }
        }
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        isMoving = true;
        mouseDownPosition = e.Location;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (isMoving)
        {
            mouseMovePosition = e.Location;
            pictureBox1.Invalidate();
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (isMoving)
        {
            lines.Add(Tuple.Create(mouseDownPosition, mouseMovePosition));
        }
        isMoving = false;
    }
}
Run Code Online (Sandbox Code Playgroud)


Cod*_*ray 5

人们很容易得出在你制定了如何画一个线以同样的方式箭头PictureBox先前的问题.

您所要做的就是指定一个StartCap或者您正在使用EndCapPen对象来绘制线条.直观地,该StartCap属性允许您指定在使用Pen对象绘制的任何线的开头使用的上限样式,而该EndCap属性允许您为线的末尾指定上限样式.

有几种不同的LineCap款式可供选择,包括:

Flat          Specifies a flat line cap.
Square        Specifies a square line cap.
Round         Specifies a round line cap.
Triangle      Specifies a triangular line cap.
NoAnchor      Specifies no anchor.
SquareAnchor  Specifies a square anchor line cap.
RoundAnchor   Specifies a round anchor cap.
DiamondAnchor Specifies a diamond anchor cap.
ArrowAnchor   Specifies an arrow-shaped anchor cap.
Custom        Specifies a custom line cap.
AnchorMask    Specifies a mask used to check whether a line cap is an anchor cap.

ArrowAnchor在这种情况下,您可能会发现最有用的样式:只需将a指定ArrowAnchor为行LineCap的开头或结尾的样式(取决于您希望箭头指向的方向).

以下代码将绘制一条绿色,右向箭头,线条粗细为4:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
 //Create a new pen to draw the arrow with
 using (Pen p = new Pen(Brushes.Green, 4f))
 {
  //Specify the EndCap, because we're drawing a right-facing arrow
  p.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;

  //Draw the arrow
  e.Graphics.DrawLine(p, 0, 0, 30, 45);
 }
}
Run Code Online (Sandbox Code Playgroud)