GIS作业01
创建工程
创建一个Windows窗体应用程序项目
如下图:

创建好后即可进入项目主界面

创建基本类
右键你的项目名称 如下图:

选择添加,新建项 进入新建界面

选择代码文件类型,并修改名称

编写代码
进入创建的代码文件编辑界面 粘贴代码:
namespace MyGIS
{
class GISVertex
{
public double x;
public double y;
}
class GISPoint
{
public GISVertex Location;
public string Attribute;
}
class GISLine
{
List<GISVertex> AllVertexs;
}
class GISPolygon
{
List<GISVertex> AllVertexs;
}
}

会有报红提示,因为没有导入类



添加GISVertex GISPoint类
重复前面操作(即新建代码文件),
我们先添加GISVertex类
代码:
using System;
class GISVertex
{
public double x;
public double y;
public GISVertex(double _x, double _y)
{
x = _x;
y = _y;
}
public double Distance(GISVertex anothervertex)
{
return Math.Sqrt((x - anothervertex.x) * (x - anothervertex.x) + (y - anothervertex.y) * (y - anothervertex.y));
}
}

同理创建GISPoint类
using System.Drawing;
class GISPoint
{
public GISVertex Location;
public string Attribute;
public GISPoint(GISVertex onevertex, string onestring)
{
Location = onevertex;
Attribute = onestring;
}
public void DrawPoint(Graphics graphics)
{
graphics.FillEllipse(new SolidBrush(Color.Red), new Rectangle((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 PointF((int)(Location.x), (int)(Location.y)));
}
public double Distance(GISVertex anothervertex)
{
return Location.Distance(anothervertex);
}
}

窗体设计视图
切换到form1设计界面 如下图:

通过左侧的工具箱,公开控件即可添加控件到窗体上 如下图:

点击控件,在右下角修改名字

修改名字后效果:

编辑窗体代码
双击进入form1代码编辑界面

界面如下:

添加代码:
List<GISPoint> points = new List<GISPoint>();

添加按钮事件代码
切换到form1主界面,双击按钮进入代码编辑页面

界面如下:

在方法添加代码:
double x = Convert.ToDouble(textBox1.Text);
double y = Convert.ToDouble(textBox2.Text);
string attribute = textBox3.Text;
GISVertex onevertex = new GISVertex(x, y);
GISPoint onepoint = new GISPoint(onevertex, attribute);
Graphics graphics = this.CreateGraphics();
onepoint.DrawPoint(graphics);
onepoint.DrawAttribute(graphics);
points.Add(onepoint);

添加事件
切回form1主界面 点击空白处 按下图操作

会生成一个鼠标点击方法:

在方法里添加代码:
GISVertex onevertex = new GISVertex((double)e.X, (double)e.Y);
double mindistance = Double.MaxValue;
int findid = -1;
for (int i = 0; i < points.Count; i++)
{
double distance = points[i].Distance(onevertex);
if (distance < mindistance)
{
mindistance = distance;
findid = i;
}
}
if (mindistance > 5 || findid == -1)
{
MessageBox.Show("没有点实体或者鼠标点击位置不准确!");
}
else
MessageBox.Show(points[findid].Attribute);

运行程序
点击上方按钮启动程序

添加point:





浙公网安备 33010602011771号