我希望能够从一个鼠标点击位置绘制一个直线箭头到另一个鼠标,就像在PowerPoint中一样。它需要能够在一个PictureBox画为好,请大家帮忙,在C中的一个Picturebox上绘制一个箭头#
感谢
我希望能够从一个鼠标点击位置绘制一个直线箭头到另一个鼠标,就像在PowerPoint中一样。它需要能够在一个PictureBox画为好,请大家帮忙,在C中的一个Picturebox上绘制一个箭头#
感谢
下面是一些基本的代码绘制从鼠标按下在PictureBox线的当前位置。
您只需要为箭头绘制更多线条或三角形。
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;
}
}
人们很容易得出在你制定了如何画一个PictureBox
线a previous question以同样的方式箭头。
所有你需要做的是你已经使用了画线的Pen
对象指定一个StartCap
或EndCap
。直观上,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
风格在这种情况下最有用:只需指定一个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);
}
}
参考阿尔宾Sunnanbo的答案,这是伟大的东西
如果你想到平局箭头只需更换
g.DrawLine(Pens.Black, mouseDownPosition, mouseMovePosition);
到
Pen p = new Pen(Color.Black,3);
p.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
g.DrawLine(p, mouseDownPosition, mouseMovePosition);
p.Dispose();
+1很大,一如既往 – 2010-11-23 12:06:24
这是完美的,只有一件事是我做使它所以我得出住宿有箭头时,我再画一个箭头,因为此刻的最后绘制箭头当我开始画一个新的时候消失?我如何解决它,让他们留下来?谢谢 – 2010-11-23 14:30:46