GIS二次开发一些代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验一
在控制台界面实现加减乘除的四则运算功能,即输入参与计算数值,输出完成的计算结果。
using System;
using System.Collections.Generic;
using System.Text;
namespace 示例1
{
class Program
{
static void Main(string[] args)
{
string a;
string b;
string c;
double d = 0;
Console.WriteLine("请输入第一个数字");
a = Console.ReadLine();
Console.WriteLine("请输入运算符");
b = Console.ReadLine();
Console.WriteLine("请输入第二个数字");
c = Console.ReadLine();
if (b == "+")
d = double.Parse(a) + double.Parse(c);
Console.WriteLine("结果为:{0}", d);
Console.ReadLine();
if (b == "-")
d = double.Parse(a) - double.Parse(c);
Console.WriteLine("结果为:{0}", d);
Console.ReadLine();
if (b == "*")
d = double.Parse(a) * double.Parse(c);
Console.WriteLine("结果为:{0}", d);
Console.ReadLine();
if (b == "/")
d = double.Parse(a) / double.Parse(c);
Console.WriteLine("结果为:{0}", d);
Console.ReadLine();
}
}
}
创建Windows程序,按Button按钮,出现MessageBox信息框
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 示例2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("欢迎学习GIS二次开发", "提示");
}
}
}
实验二
Windows应用程序,利用TextBox控件和窗体Form的Form1_Load事件练习String.Format的格式化输出。
在以上练习基础上,试验“变量.ToString();”语句。
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ydf
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string a = 12345.ToString("n");
string b = 12345.ToString("c");
string c = 12345.ToString("e");
string d = 12345.ToString("f4");
string f = 12345.ToString("x");
string g = 12345.ToString("n");
this.textBox1.Text = a + "\r\n" + b + "\r\n" + c + "\r\n" + d + "\r\n" + f + "\r\n" + g + "\r\n"; }
}
}
Windows应用程序,利用Label、TextBox控件和Button控件设计实现:字符串的输入、复制、删除、连接、字符插入、字符查找等功能。
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ydf
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string a = textBox1.Text;
string b = textBox2.Text;
int m=pareTo(b);
if(m<0)
{
textBox3.Text=a +"<"+b;
}
else if(m==0)
{
textBox3.Text=a +"="+b;
}
else
{
textBox3.Text=a +">"+b;
}
}
private void button2_Click(object sender, EventArgs e)
{
string a = textBox1.Text;
string b = textBox2.Text;
string s;
s = string.Concat(b, a);
textBox3.Text = s;
}
private void button3_Click(object sender, EventArgs e) {
string a = textBox1.Text;
string b = textBox2.Text;
string []c = b.Split(',');
textBox3.Text = c[0];
}
private void button4_Click(object sender, EventArgs e) {
string a = textBox1.Text;
string b = textBox2.Text;
textBox1.Text = a;
textBox2.Text = b;
textBox3.Text = a.Insert(1, b);
}
private void button5_Click(object sender, EventArgs e) {
string a = textBox1.Text;
string b = textBox2.Text;
string l;
l = string.Copy(b);
textBox3.Text = l;
}
private void button6_Click(object sender, EventArgs e) {
string y;
y = string.Empty;
textBox3.Text = y;
}
}
}
实验三
数组、ArrayList类、Hashtable类、Queue类和Stack类
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string str;
Hashtable ht = new Hashtable();
private void button1_Click(object sender, EventArgs e) {
string[] str = textBox1.Lines;
ArrayList arr = new ArrayList(str);
textBox3.Text = "";
foreach (string j in arr)
{
textBox3.Text += j.ToString() + "\r\n";
}
}
private void button2_Click(object sender, EventArgs e) {
string[] str = textBox1.Lines;
string str1 = textBox2.Text;
ArrayList arr1 = new ArrayList(str);
arr1.Add(str1);
textBox3.Text = "";
foreach (string j in arr1)
{
textBox3.Text += j.ToString() + "\r\n";
}
}
private void button4_Click(object sender, EventArgs e) {
string[] str = textBox1.Lines;
ArrayList arr3 = new ArrayList(str);
arr3.Insert(1, "插入字符");
textBox3.Text = "";
foreach (string j in arr3)
{
textBox3.Text += j.ToString() + "\r\n";
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)
{
string[] str = textBox1.Lines;
ArrayList arr5 = new ArrayList(str);
string str1 = textBox2.Text ; //只能删除string,不能删除string[] arr5.Remove(str1);
textBox3.Text = "";
foreach (string j in arr5)
{
textBox3.Text += j.ToString() + "\r\n";
}
}
private void button7_Click(object sender, EventArgs e)
{
string[] str = textBox1.Lines;
ArrayList arr6 = new ArrayList(str);
arr6.Sort();
textBox3.Text = "";
foreach (string j in arr6)
{
textBox3.Text += j.ToString() + "\r\n";
}
textBox3.Text = textBox3.Text+"\r\n"+"按照首字母排序";
}
private void button8_Click(object sender, EventArgs e)
{
string[] str = textBox1.Lines;
ArrayList arr7 = new ArrayList(str);
arr7.Clear();
foreach (string j in arr7)
{
textBox3.Text += j.ToString() + "\r\n";
}
textBox3.Text = "数组已清除";
}
private void button9_Click(object sender, EventArgs e)
{
string str = textBox1.Text ;
string str1 = textBox2.Text ;
ht.Add(str, str1);
foreach (DictionaryEntry de in ht)
{
textBox3.Text += "Key为" + de.Key + ",Value值为" + de.Value + "\r\n"; }
textBox1.Text = "";
textBox2.Text = "";
}
private void button10_Click(object sender, EventArgs e)
{
string str = textBox1.Text;
string str1 = textBox2.Text;
ht.Add(str, str1);
textBox3.Text="";
foreach (DictionaryEntry de in ht)
{
textBox3.Text += "Key为" + de.Key + ",Value值为" + de.Value + "\r\n"; }
textBox1.Text = "";
textBox2.Text = "";
}
private void button11_Click(object sender, EventArgs e)
{
string str = textBox1.Text;
ht.Remove(str);
textBox3.Text = "";
foreach (DictionaryEntry de in ht)
{
textBox3.Text += "Key为" + de.Key + ",Value值为" + de.Value + "\r\n"; }
textBox1.Text = "";
textBox2.Text = "";
}
private void button12_Click(object sender, EventArgs e)
{
string str = textBox1.Text;
textBox3.Text="";
foreach (DictionaryEntry de in ht)
{
if ((string)de.Key == str)
{
textBox3.Text = "已找到Key为" + str + "的元素";
}
}
textBox1.Text = "";
textBox2.Text = "";
}
private void button13_Click(object sender, EventArgs e) {
string[] str = textBox1.Lines;
Queue qu = new Queue(str);
textBox3.Text = "";
foreach (string j in qu)
{
textBox3.Text += j.ToString() + "\r\n";
}
}
private void button14_Click(object sender, EventArgs e) {
string[] str = textBox1.Lines;
Queue qu = new Queue(str);
string str1 = textBox2.Text ;
qu.Enqueue(str1);
textBox3.Text = "";
foreach (string j in qu)
{
textBox3.Text += j.ToString() + "\r\n";
}
}
private void button15_Click(object sender, EventArgs e) {
string[] str = textBox1.Lines;
Queue qu = new Queue(str);
qu.Dequeue();
textBox3.Text = "";
foreach (string j in qu)
{
textBox3.Text += j.ToString() + "\r\n";
}
}
private void button16_Click(object sender, EventArgs e) {
string[] str = textBox1.Lines;
Stack st = new Stack();
string str1 = textBox2.Text;
st.Push(str1);
textBox3.Text = "";
foreach (string j in st)
{
textBox3.Text += j.ToString() + "\r\n";
}
}
private void button17_Click(object sender, EventArgs e)
{
string[] str = textBox1.Lines;
Stack st = new Stack();
st.Pop( );
textBox3.Text = "";
foreach (string j in st)
{
textBox3.Text += j.ToString() + "\r\n";
}
}
}
}
矩形:包括私有成员长、宽,包括面积计算方法(GetArea);圆形:包括私有成员半径,包括面积计算方法(GetArea);三角形:包括私有成员底、高,包括面积计算方法(GetArea)
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ydf
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double C, K, D, G, R;
private void button1_Click(object sender, EventArgs e)
{
Rectangle Re = new Rectangle();
Re.C = Convert.ToDouble(textBox1.Text);
Re.K = Convert.ToDouble(textBox2.Text);
textBox6.Text = "矩形的面积为;" + Convert.ToString(Re.GetArea());
}
private void button2_Click(object sender, EventArgs e)
{
Triangle Tr = new Triangle();
Tr.D = Convert.ToDouble(textBox3.Text);
Tr.G = Convert.ToDouble(textBox4.Text);
textBox6.Text = "三角形的面积为;" + Convert.ToString(Tr.GetArea()); }
private void button3_Click(object sender, EventArgs e)
{
Circle Ci = new Circle();
Ci.R = Convert.ToDouble(textBox5.Text);
textBox6.Text = "圆的面积为;" + Convert.ToString(Ci.GetArea());
}
}
}
Circle.cs:
using System;
using System.Collections.Generic;
using System.Text;
namespace ydf
{
class Circle:IGetArea
{
private double r;
public double R
{
get { return r; }
set { r = value; }
}
#region IGetArea 成员
public double GetArea()
{
return r*r*3.1415;
}
#endregion
}
}
Rectangle.cs:
using System;
using System.Collections.Generic;
using System.Text;
namespace ydf
{
class Rectangle:IGetArea
{
private double c;
private double k;
public double C
{
get { return c; }
set { c = value; }
}
public double K
{
get { return k; }
set { k = value; }
}
#region IGetArea 成员
public double GetArea() {
return c * k;
}
#endregion
}
}
Triangle.cs:
using System;
using System.Collections.Generic; using System.Text;
namespace ydf
{
class Triangle:IGetArea
{
private double d;
private double g;
public double D
{
get { return d; }
set { d = value; }
}
public double G
{
get { return g; }
set { g = value; }
}
#region IGetArea 成员
public double GetArea() {
return 0.5 * d * g;
}
#endregion
}
}
IGetArea.cs:
using System;
using System.Collections.Generic;
using System.Text;
namespace ydf
{
interface IGetArea
{
double GetArea();
}
}
实验四
MapX 地图文件*.gst 和*.tab的打开、关闭,地图当前工具的设置使用(选择工具、放大工具、缩小工具、漫游工具)
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ydf
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
MapXLib.Map a = this.axMap1.GetOcx() as MapXLib.Map;
yerInfo b = new yerInfoClass();
System.Windows.Forms.OpenFileDialog c = this.openFileDialog1;
string name = "";
string type = "";
c.Title = "打开文件";
c.Filter = "图层文件(*.tab)|*.tab|图层集合(*.gst)|*.gst";
c.ShowDialog();
name = c.FileName;
if (name.Length > 3)
{
type = name.Substring(name.Length - 3, 3);
}
if (type.ToUpper() == "GST")
{
yers.AddGeoSetLayers(name);
}
else if (type.ToUpper()== "TAB")
{
try
{
b.Type = ayerInfoTypeTab;
b.AddParameter("FileSpec", name);
b.AddParameter("visible", true);
b.AddParameter("autocreatedataset", true);
yers.Add(b, 0);
a.Bounds = yers.Bounds;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Windows.Forms.SaveFileDialog d = this.saveFileDialog1;
string name = "";
string type = "";
string filespec = "";
d.Title = "保存文件";
d.Filter = "图层集合(*.gst)|*.gst";
d.ShowDialog();
filespec = d.FileName;
name = filespec.Substring(stIndexOf("\\") + 1);
if (name.Length > 3)
{
type = name.Substring(name.Length - 3, 3);
if (type.ToUpper() == "GST")
{
this.axMap1.SaveMapAsGeoset(name, filespec);
}
}
}
private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
yers.RemoveAll();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 放大ToolStripMenuItem_Click(object sender, EventArgs e)
{
MapXLib.Map a = this.axMap1.GetOcx() as MapXLib.Map;
a.CurrentTool = MapXLi
b.ToolConstants.miZoomInTool;
}
private void 缩小ToolStripMenuItem_Click(object sender, EventArgs e)
{
MapXLib.Map a = this.axMap1.GetOcx() as MapXLib.Map;
a.CurrentTool = MapXLi
b.ToolConstants.miZoomOutTool;
}
private void 漫游ToolStripMenuItem_Click(object sender, EventArgs e)
{
MapXLib.Map a = this.axMap1.GetOcx() as MapXLib.Map;
a.CurrentTool = MapXLi
b.ToolConstants.miPanTool;
}
private void 单击选择ToolStripMenuItem_Click(object sender, EventArgs e) {
MapXLib.Map a = this.axMap1.GetOcx() as MapXLib.Map;
a.CurrentTool = MapXLi
b.ToolConstants.miSelectTool;
}
private void 矩形选择ToolStripMenuItem_Click(object sender, EventArgs e) {
MapXLib.Map a = this.axMap1.GetOcx() as MapXLib.Map;
a.CurrentTool = MapXLi
b.ToolConstants.miRectSelectTool;
}
private void 多边形选择ToolStripMenuItem_Click(object sender, EventArgs e) {
MapXLib.Map a = this.axMap1.GetOcx() as MapXLib.Map;
a.CurrentTool = MapXLi
b.ToolConstants.miPolygonSelectTool;
}
private void 自定义工具ToolStripMenuItem_Click(object sender, EventArgs e) {
MapXLib.Map a = this.axMap1.GetOcx() as MapXLib.Map;
a.CreateCustomTool(110, MapXLi
b.ToolTypeConstants.miToolTypePoint,
MapXLib.CursorConstants.miCrossCursor, null, null, null);
a.CurrentTool = (MapXLi
b.ToolConstants)110;
yers[1].Editable = true;
yers.InsertionLayer = yers[1];
}
private void axMap1_ToolUsed(object sender, AxMapXLib.CMapXEvents_ToolUsedEvent e)
{
MapXLib.Map a = this.axMap1.GetOcx() as MapXLib.Map;
if (e.toolNum == 110)
{
yers.InsertionLayer.Selection.SelectByPoint(e.x1, e.y1,
MapXLib.SelectionTypeConstants.miSelectionNew,
MapXLib.SearchResultTypeConstants.miSearchResultDefault);
if (yers.InsertionLayer.Selection.Count > 0)
{
string b = Convert.ToString(e.x1);
string c = Convert.ToString(e.y1);
string str = "坐标为" + b + "," + c;
MessageBox.Show(str);
}
}
}
}
}
实验五
练习MapX的图层集对象(Layers)的属性、方法,利用Layers创建图层,增加图层,删除图层,移动图层。
练习MapX的图层对象(Layer)的属性、方法,利用Layer的属性、方法自动标注图层,获取点图元,增加点图元,删除点图元。
练习MapX的图元对象(Feature)的属性、方法,利用FeatureFactory对象实现对图元的操作。
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ydf
{
public partial class Form1 : Form
{
private MapXLib.Map m_map;
public MapXLib.Map ObjMap
{
get { return m_map; }
set { m_map = value; }
}
public Form1()
{
InitializeComponent();
ObjMap = this.axMap1.GetOcx() as MapXLib.Map;
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog m_OpenDlg = this.openFileDialog1; string strName = "";
string strType = "";
m_OpenDlg.Title = "打开文件";
m_OpenDlg.Filter = "图层文件(*.tab)|*.tab|图层集合(*.gst)|*.gst";
m_OpenDlg.ShowDialog();
strName = m_OpenDlg.FileName;
if (strName.Length > 3)
{
strType = strName.Substring(strName.Length - 3, 3);
}
if (strType.ToUpper() == "GST")
{
yers.AddGeoSetLayers(strName);
}
else if (strType.ToUpper() == "TAB")
{
LoadLayers(strName);
}
LayerLable();
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Windows.Forms.SaveFileDialog m_SaveDlg = this.saveFileDialog1; string strName = "";
string strType = "";
string strFileSpec = "";
m_SaveDlg.Title = "保存文件";
m_SaveDlg.Filter = "图层集合(*.gst)|*.gst";
m_SaveDlg.ShowDialog();
strFileSpec = m_SaveDlg.FileName;
strName = strFileSpec.Substring(stIndexOf("\\") + 1); if (strName.Length > 3)
{
strType = strName.Substring(strName.Length - 3, 3);
if (strType.ToUpper() == "GST")
{
ObjMap.SaveMapAsGeoset(strName, strFileSpec);
}
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 图层对话框ToolStripMenuItem_Click(object sender, EventArgs e)
{
yersDlg(null, null);
LayerLable();
}
private void 创建ToolStripMenuItem_Click(object sender, EventArgs e)
{
string path = @"f:\新图层";
yers.CreateLayer("新图层", path, 0, 10, ObjMap.DisplayCoordSys);
LayerLable();
}
private void 移除ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (yers.Count > 0)
{
int s = this.treeView1.SelectedNode.Index;
yers.Remove(s + 1);
LayerLable();
}
}
private void 增加ToolStripMenuItem_Click(object sender, EventArgs e)
{
MapXLib.Point pnt = new MapXLib.Point();
pnt.Set(ObjMap.Bounds.XMin + (ObjMap.Bounds.XMax - ObjMap.Bounds.XMin) / 2, ObjMap.Bounds.YMin + (ObjMap.Bounds.YMax - ObjMap.Bounds.YMin) / 2);
yers._Item("新图层").Editable = true;
yers.InsertionLayer = yers._Item("新图层");
MapXLib.Feature ftr = new MapXLib.Feature();
MapXLib.Style stylPnt = new MapXLib.Style();
ftr = ObjMap.FeatureFactory.CreateSymbol(pnt, stylPnt);
yers._Item("新图层").AddFeature(ftr, new MapXLib.RowValuesClass()); }
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
ObjMap.CurrentTool = (MapXLib.ToolConstants)112;
yers._Item("新图层").Editable = true;
yers.InsertionLayer = yers._Item("新图层");
}
private void 遍历ToolStripMenuItem_Click(object sender, EventArgs e)
{
MapXLib.Feature ftr = yers[1].AllFeatures._Item(4);
string FeatureID = ftr.FeatureID.ToString();
string FeatureKey = ftr.FeatureKey;
string FeatureKeyValue = ftr.KeyValue;
string strTemp = "FeatureID:" + FeatureID + "\r\n";
strTemp = strTemp + "FeatureKey:" + FeatureKey + "\r\n";
strTemp = strTemp + "FeatureKeyValue:" + FeatureKeyValue + "\r\n";
MessageBox.Show(strTemp);
}
private void 查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
MapXLib.Feature ftr = yers[1].Selection[1];
double x = ftr.CenterX;
double y = ftr.CenterY;
MapXLib.Point pnt = new MapXLib.Point();
pnt.Set(x, y);
if (ftr != null)
{
MapXLib.Features ftrs = yers[1].SearchWithinDistance(pnt, 1, MapXLib.MapUnitConstants.miUnitMile, MapXLib.SearchTypeConstants.miSearchTypeEntirelyWithin); yers[1].Selection.Add(ftrs);
MessageBox.Show("查询到图元:" + ftrs.Count.ToString() + "个.");
}
}
public void LayerLable()
{
string m_layer;
this.treeView1.Nodes.Clear();
if (yers.Count > 0)
{
for (int i = 1; i <= yers.Count; i++)
{
m_layer = yers[i].Name;
this.treeView1.Nodes.Add(m_layer);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
ObjMap.CreateCustomTool(112, MapXLib.ToolTypeConstants.miToolTypePoint, MapXLib.CursorConstants.miCenterCursor, null, null, null);
LayerLable();
}
public void LoadLayers(string path)
{
yerInfo lyrInfo = new yerInfoClass();
try
{
lyrInfo.Type = ayerInfoTypeTab;
lyrInfo.AddParameter("FileSpec", path);
yers.Add(lyrInfo, 0);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void axMap1_ToolUsed(object sender, AxMapXLib.CMapXEvents_ToolUsedEvent e) {
if (ObjMap.CurrentTool == (MapXLib.ToolConstants)112)
{
yers.InsertionLayer.Selection.SelectByPoint(e.x1, e.y1,
MapXLib.SelectionTypeConstants.miSelectionNew,
MapXLib.SearchResultTypeConstants.miSearchResultDefault);
if (yers.InsertionLayer.Selection.Count > 0)
{
yers.InsertionLayer.DeleteFeature(yers.InsertionLayer.Selection._Item(1)); }
}
}
private void toolZoomIn_Click(object sender, EventArgs e)
{
ObjMap.CurrentTool = MapXLib.ToolConstants.miZoomInTool;
}
private void toolZoomOut_Click(object sender, EventArgs e)
{
ObjMap.CurrentTool = MapXLib.ToolConstants.miZoomOutTool;
}
private void toolPan_Click(object sender, EventArgs e)
{
ObjMap.CurrentTool = MapXLib.ToolConstants.miPanTool;
}
private void toolSelectRect_Click(object sender, EventArgs e)
{
ObjMap.CurrentTool = MapXLib.ToolConstants.miRectSelectTool;
}
private void toolInformation_Click(object sender, EventArgs e)
{
ObjMap.CurrentTool = MapXLib.ToolConstants.miSelectTool;
MessageBox.Show("显示选中的图元信息");
}
private void toolLayers_Click(object sender, EventArgs e)
{
yersDlg(null, null);
LayerLable();
}
private void toolZoomAll_Click(object sender, EventArgs e)
{
if (yers.Count > 0)
{
ObjMap.Bounds = yers.Bounds;
}
}
private void toolDelete_Click(object sender, EventArgs e)
{
ObjMap.CurrentTool = (MapXLib.ToolConstants)112;
yers._Item("新图层").Editable = true;
yers.InsertionLayer = yers._Item("新图层");
}
private void 获取ToolStripMenuItem_Click(object sender, EventArgs e)
{
yer objlyr = yers._Item("绿地");
MessageBox.Show();
}
}
}
实验六
在上次实验的基础上,复习MapX的图层的创建、添加与删除。
实现对图元(点、线、面)的编辑。
练习利用MapX 中的各种空间查询方法,如searchAtPoint,SearchWithinFeature、SearchWithinDistance。
练习利用MapX的Selection 对象选择需要的图元对象,方法如SelectByPoint、SelectByRegion、SelectCommon。
练习利用MapX的FeatureFactory 对象对图元对象进行缓冲区处理、合并、求交集等。
掌握MapX中的CoordSys对象和Style对象。
掌握MapX中的地图工具操作的相关事件和鼠标相关事件。
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MapXLib;
namespace ydf
{
public partial class Form1 : Form
{
private MapXLib.Map m_map;
public MapXLib.Map ObjMap
{
get { return m_map; }
set { m_map = value; }
}
public Form1()
{
InitializeComponent();
ObjMap = this.axMap1.GetOcx() as MapXLib.Map;
}
private void Form1_Load(object sender, EventArgs e)
{
LayerLable();
}
public void LayerLable()
{
string m_layer;
this.treeView1.Nodes.Clear();
if (yers.Count > 0)
{
for (int i = 1; i <= yers.Count; i++)
{
m_layer = yers[i].Name;
this.treeView1.Nodes.Add(m_layer);
}
}
}
public void LoadLayers(string path)
{
yerInfo lyrInfo = new yerInfoClass();
try
{
int layerNum = yers.Count;
lyrInfo.Type = ayerInfoTypeTab;
lyrInfo.AddParameter("FileSpec", path);
yers.Add(lyrInfo, 0);
if (layerNum == 0)
{
ObjMap.NumericCoordSys = yers[1].CoordSys;
ObjMap.DisplayCoordSys = yers[1].CoordSys; }
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog m_OpenDlg = this.openFileDialog1;
string strName = "";
string strType = "";
m_OpenDlg.Title = "打开文件";
m_OpenDlg.Filter = "图层文件(*.tab)|*.tab|图层集合(*.gst)|*.gst";
m_OpenDlg.ShowDialog();
strName = m_OpenDlg.FileName;
if (strName.Length > 3)
{
strType = strName.Substring(strName.Length - 3, 3);
}
if (strType.ToUpper() == "GST")
{
yers.AddGeoSetLayers(strName);
ObjMap.Title.Visible = false;
}
else if (strType.ToUpper() == "TAB")
{
LoadLayers(strName);
}
LayerLable();
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Windows.Forms.SaveFileDialog m_SaveDlg = this.saveFileDialog1;
string strName = "";
string strType = "";
string strFileSpec = "";
m_SaveDlg.Title = "保存文件";
m_SaveDlg.Filter = "图层集合(*.gst)|*.gst";
m_SaveDlg.ShowDialog();
strFileSpec = m_SaveDlg.FileName;
strName = strFileSpec.Substring(stIndexOf("\\") + 1); if (strName.Length > 3)
{
strType = strName.Substring(strName.Length - 3, 3);
if (strType.ToUpper() == "GST")
{
ObjMap.SaveMapAsGeoset(strName, strFileSpec);
}
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void toolZoomIn_Click(object sender, EventArgs e)
{
ObjMap.CurrentTool = MapXLib.ToolConstants.miZoomInTool;
}
private void toolZoomOut_Click(object sender, EventArgs e)
{
ObjMap.CurrentTool = MapXLib.ToolConstants.miZoomOutTool;
}
private void toolPan_Click(object sender, EventArgs e)
{
ObjMap.CurrentTool = MapXLib.ToolConstants.miPanTool;
}
private void toolSelectRect_Click(object sender, EventArgs e)
{
ObjMap.CurrentTool = MapXLib.ToolConstants.miRectSelectTool;
}
private void toolInformation_Click(object sender, EventArgs e)
{
ObjMap.CurrentTool = abelTool;
}
private void toolLayers_Click(object sender, EventArgs e)
{
yersDlg(null, null);
LayerLable();
}
private void toolZoomAll_Click(object sender, EventArgs e)
{
if (yers.Count > 0)
{
ObjMap.Bounds = yers.Bounds;
}
}
private void toolDelete_Click(object sender, EventArgs e)
{
if (yers.Count > 0)
{
int s = this.treeView1.SelectedNode.Index;
yers.Remove(s + 1);
LayerLable();
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
yer lyr;
string layerName = this.toolStripComboBox1.SelectedItem.ToString();
lyr = yers[layerName];
lyr.Editable = true;
if (lyr.Selection.Count > 0)
{
ObjMap.FeatureEditMode = (short)MapXLib.FeatureEditModeConstants.miEditModeNode;
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
yer lyr;
string layerName = this.toolStripComboBox1.SelectedItem.ToString();
lyr = yers[layerName];
yers.InsertionLayer = null;
if (lyr.Selection.Count > 0)
{
ObjMap.FeatureEditMode = (short)MapXLib.FeatureEditModeConstants.miEditModeAddNode; }
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
System.Windows.Forms.SaveFileDialog m_SaveDlg = this.saveFileDialog3;
string strName = "";。