问题描述
大家好,
我的问题是,当我想在表单上绘制2个点并连接它们时,这些点不会出现,但连接仍在表单上。我正在使用DrawEllipse和DrawLine方法。
Hello guys,
My problem is that when I want to draw 2 points on the form and connect them, the points do not appear but the connections are still on the form. I am using DrawEllipse and DrawLine methods.
public partial class Form1 : Form
{
private Point p1, p2;
List<Point> p1List = new List<Point>();
List<Point> p2List = new List<Point>();
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
using (var p = new Pen(Color.Blue, 4))
{
for (int x = 0; x < p1List.Count; x++)
{
e.Graphics.DrawLine(p, p1List[x], p2List[x]);
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Graphics gr = this.CreateGraphics();
int x = e.X;
int y = e.Y;
// Create pen.
Pen whitePen = new Pen(Color.Blue, 3);
Pen red = new Pen(Color.Red, 3);
SolidBrush whiteBrush = new SolidBrush(Color.Blue);
// Create rectangle for ellipse.
Rectangle rect = new Rectangle(x - 5, y - 5, 10, 10);
gr.DrawEllipse(whitePen, rect);
gr.FillEllipse(whiteBrush, rect);
if (p1.X == 0)
{
p1.X = e.X;
p1.Y = e.Y;
}
else
{
p2.X = e.X;
p2.Y = e.Y;
p1List.Add(p1);
p2List.Add(p2);
Invalidate();
p1.X = 0;
}
}
}
第一张图片你可以看到我第一次点击它绘制一个点的表格,然后在第二张图片上看到了连接,但是没有积分。
我需要你的帮助!
图片:
推荐答案
<pre lang="c#">
p2.X = eX;
p2.Y = eY;
p1List.Add(p1);
p2List.Add(p2);
使用(var p =新笔(Color.Blue,4))
{
for(int i = 0;我< p1List.Count; i ++)
{
gr.DrawLine(p,p1List [i],p2List [i]);
}
}
p1.X = 0;
}
}
这可能对你有所帮助
p2.X = e.X;
p2.Y = e.Y;
p1List.Add(p1);
p2List.Add(p2);
using (var p = new Pen(Color.Blue, 4))
{
for (int i = 0; i < p1List.Count; i++)
{
gr.DrawLine(p, p1List[i], p2List[i]);
}
}
p1.X = 0;
}
}
this might help you
这篇关于使用Draw方法C#连接2个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!