
打开Visual Studio 2005
在菜单栏中点击“文件\新建\项目”,打开新建项目框,点击“项目类型\\Windows”,选择“模板\\Windows应用程序”,在名称中输入“2009083022”,选择位置,点击“确定”。打开“2009083022 Microsoft Visual Studio”。
选择窗体属性,将“Name\\Form1”改为“Name\\Mapviewer”
选择“工具箱\对话框\\ArcGIS Windows Forms”,依次添加“ToolbarControl、TOCControl、MapControl、LicenseControl”四个控件。
依次选择“ToolbarControl、TOCControl、MapControl”三个控件的属性,将其属性“Dock”依次改为“Top、Left、Fill”
选择“ToolbarControl、TOCControl”的属性General与
“MapControl”绑定
右击“ToolbarControl”选择“Items\\Add”,添加需要的按钮并运行
在工具箱中选择“菜单和工具栏”,双击添加“MenuStrip”菜单,命名“添加shp”,并修改其属性”Name-MenuAddshp”,双击“添加shp”,编写程序并运行,在“所有Windows窗体”
中双击“openFileDialog”添加按钮,编写程序并运行
private void MenuAddshp_Click(object sender, EventArgs e)
{
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
//IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(@"E:\软件安装\\arcgis\\ArcGlobeData", 0);
//IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
//IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass("continent.shp");
openFileDialog1.Filter = "shapefile文件(*.shp)|*.shp";
openFileDialog1.InitialDirectory = @"E:\软件安装\\arcgis\\ArcGlobeData";
openFileDialog1.Multiselect = false;
DialogResult pDialogResult = openFileDialog1.ShowDialog();
if (pDialogResult != DialogResult.OK)
return;
string pPath = openFileDialog1.FileName;
string pFolder = Path.GetDirectoryName(pPath);
string pFileName = Path.GetFileName(pPath);
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pFolder, 0);
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(pFileName);
IFeatureLayer pFLayer=new FeatureLayerClass();
pFLayer.FeatureClass=pFC;
pFLayer.Name=pFC.AliasName;
ILayer pLayer=pFLayer as ILayer;
IMap pMap=axMapControl1.Map;
pMap.AddLayer(pLayer);
axMapControl1.ActiveView.Refresh();
}
同理添加lyr
private void MenuAddlyr_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "lyr文件(*.lyr)|*.lyr";
openFileDialog1.InitialDirectory = @"E:\软件安装\\arcgis\\ArcGlobeData";
openFileDialog1.Multiselect = false;
DialogResult pDialogResult = openFileDialog1.ShowDialog();
if (pDialogResult != DialogResult.OK)
return;
string pFileName = openFileDialog1.FileName;
axMapControl1.AddLayerFromFile(pFileName);
axMapControl1.ActiveView.Refresh();
}
在编写以上程序前需添加应用和头文件程序
添加应用:
ESRI.ArcGIS.DataSourcesFile;
ESRI.ArcGIS.Geodatabase;
头文件程序:
using System.IO;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
添加图层属性:计算面积
在解决方案中右击2008093022\添加\\Windows窗体,名称:FrmAttributeTable.cs
在工具箱中选择“数据\\DataGridView”,设置属性Dock:Fill,单击事件按钮,双击load进行编程并运行
namespace _009083022
{ public partial class FrmAttributeTable : Form
{ private AxMapControl m_MapCtrl;
public FrmAttributeTable(AxMapControl pMapCtrl)
{ InitializeComponent();
m_MapCtrl = pMapCtrl;
}
private void FrmAttributeTable_Load(object sender, EventArgs e)
{ ILayer pLayer = m_MapCtrl.get_Layer(0);
IFeatureLayer pFLayer = pLayer as IFeatureLayer;
IFeatureClass pFC = pFLayer.FeatureClass;
IFeatureCursor pFCursor = pFC.Search(null, false);
IFeature pFeature = pFCursor.NextFeature();
DataTable pTable = new DataTable();
DataColumn colName = new DataColumn("洲名");
colName.DataType = System.Type.GetType("Systym.String");
pTable.Columns.Add(colName);
DataColumn colArea = new DataColumn("面积");
colArea.DataType = System.Type.GetType("Systym.String");
pTable.Columns.Add(colArea);
int indexofName = pFC.FindField("CONTINENT");
int indexofArea = pFC.FindField("Area");
while (pFeature != null)
{ string name = pFeature.get_Value(indexofName).ToString();
string area = pFeature.get_Value(indexofArea).ToString();
DataRow pRow = pTable.NewRow();
pRow[0] = name;
pRow[1] = area;
pTable.Rows.Add(pRow);
pFeature = pFCursor.NextFeature();
}
dataGridView1.DataSource = pTable;
}
}
}
遇到的问题:
1.在机房不用注释掉Form1.Designer.cs中的this.openFileDialog1.FileOk+=new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
但在这里都得注释掉,否则运行时虽然出现图形,但同时有异常出现。解决方法:自己没懂怎么修改,通过问同学才知道的。
2.在编写程序时字母的大小写中出现问题程序抄错。解决方法:多次与源程序对比,仔细检查,修改正确。
3.在ToolbarContronl中添加图标运行时有些不可用。解决方法:多次添加并运行、修改,得到可用图标。
4.有些过程忘记了,运行时不出图。解决方法:借同宿舍的笔记参考参考,重行做过程。
