实现点的绘制
- 不妨先来看看效果
- 基本思路
- 在绘制按钮的点击事件里获取三个TextBox的内容,然后新建相应的点类,进行绘制
private void button1_Click(object sender, EventArgs e)
{
double x = Convert.ToDouble(textBox1.Text); //
double y = Convert.ToDouble(textBox2.Text);
GISVertext location = new GISVertext(x, y);
string attribute = textBox3.Text;
GISPoint point = new GISPoint(location, attribute);
Graphics graphics = this.CreateGraphics();
point.drawAttribute(graphics);
point.drawLocation(graphics);
}
- 下面是用到的两个类,放在BasicClassices.cs下
namespace CalfGIS
{
class GISVertext //节点
{
public double x;
public double y;
public GISVertext(double x, double y)
{
this.x = x;
this.y = y;
}
}
class GISPoint
{
public GISVertext location;
public string attribute;
public GISPoint(GISVertext location, string attribute)
{
this.location = location;
this.attribute = attribute;
}
public void drawLocation(Graphics graphics)
{
graphics.FillEllipse(new SolidBrush(Color.Red),
(int)location.x - 3, (int)location.y - 3, 6, 6);
}
public void drawAttribute(Graphics graphics)
{
graphics.DrawString(attribute, new Font("宋体", 20),
new SolidBrush(Color.Green), new Point((int)location.x, (int)location.y));
}
}
}
- 上述代码相对简单,不详讲,读者认真看应该问题不大,有问题可以留言
下面添加捕捉功能
- 思路很简单,在Form1里新建一个List points, 来存储所有绘制的点,然后监听鼠标的点击事件,获取点击的x,y值,与points里每个点计算距离,距离最小且距离不超过5个像素的点被捕捉
- 以下是全部代码
//BasicClasses.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalfGIS
{
class GISVertext //节点
{
public double x;
public double y;
public GISVertext(double x, double y)
{
this.x = x;
this.y = y;
}
public double distance(GISVertext anotherVertex) //节点
{
return Math.Sqrt((x - anotherVertex.x) * (x - anotherVertex.x) +
(y - anotherVertex.y) * (y- anotherVertex.y));
}
}
class GISPoint
{
public GISVertext location;
public string attribute;
public GISPoint(GISVertext location, string attribute)
{
this.location = location;
this.attribute = attribute;
}
public void drawLocation(Graphics graphics)
{
graphics.FillEllipse(new SolidBrush(Color.Red),
(int)location.x - 3, (int)location.y - 3, 6, 6);
}
public void drawAttribute(Graphics graphics)
{
graphics.DrawString(attribute, new Font("宋体", 20),
new SolidBrush(Color.Green), new Point((int)location.x, (int)location.y));
}
}
}
using CalfGIS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace lesson1
{
public partial class Form1 : Form
{
List<GISPoint> points = new List<GISPoint>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double x = Convert.ToDouble(textBox1.Text); //获取x
double y = Convert.ToDouble(textBox2.Text); //获取y
GISVertext location = new GISVertext(x, y);
string attribute = textBox3.Text; //属性
GISPoint point = new GISPoint(location, attribute);
Graphics graphics = this.CreateGraphics(); //获取绘图对象
point.drawAttribute(graphics); //绘制点属性
point.drawLocation(graphics); //绘制点的位置
points.Add(point);
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
GISVertext clickVertex = new GISVertext(e.X, e.Y);
double minDistance = double.MaxValue; //最小距离
int minIndex = -1; //记录距离最小点所在下标
for(int i = 0; i < points.Count; i++)
{
double distance = points[i].location.distance(clickVertex);
if(distance < minDistance)
{
minDistance = distance;
minIndex = i;
}
}
if(minIndex == -1)
{
MessageBox.Show("屏幕上没有点!");
}
else
{
MessageBox.Show("被选择的点是:" + points[minIndex].location.x.ToString()
+ points[minIndex].location.y.ToString() + points[minIndex].attribute);
}
}
}
}