1、開發環境
Vs2012+Arcgis10.2+win7 64bit
2、實現代碼
首先在VS2012中新建一個解決方案,命名AddInTest。
接着,給解決方案AddInTest新建一個項目:

點擊[確定],出現如下界面,點擊[Next]。

接着,設置相關信息,並點擊[finish]完成。

上圖中,class name是類的名稱,caption是button顯示的文字,category是所屬的command的分類,tooltip是鼠標在上面時狀態欄顯示的文字,description是工具的描述。
項目建成后,文件組織如下圖:

將項目的框架改為framework 4.5
Config.esriaddinx是一個XML文件,是一個配置文件,里面包含了項目的相關配置,是自動生成的,內容如下:
01.
<esri.configuration xmlns="http://schemas.esri.com/Desktop/AddIns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
02.
<name>ArcMapAddinTest</name>
03.
<addinid>{0f7ec41b-d1e3-4391-8d67-9dea10bea621}</addinid>
04.
<description>Type in a description for this Add-in.</description>
05.
<version>1.0</version>
06.
<img src="" style="display: none;"><img alt="加載中..." title="圖片加載中..."src="http://www.it165.net/statics/images/s_nopic.gif">ImagesArcMapAddinTest.png
07.
<author>Administrator</author>
08.
<company></company>
09.
<date>2013/12/12</date>
10.
<targets>
11.
<target name="Desktop" version="10.0">
12.
</target></targets>
13.
<addin language="CLR" library="ArcMapAddinTest.dll" namespace="ArcMapAddinTest">
14.
<arcmap>
15.
<commands><button caption="AddShp" category="Add-In Controls" class="AddShp" id="ArcMapAddinTest_AddShp"image="ImagesAddShp.png" message="點擊瀏覽shp文件並添加" tip="實現添加shp文件的功能"></button></commands></arcmap></addin></esri.configuration>
01.
using System;
02.
using System.Collections.Generic;
03.
using System.Text;
04.
using System.IO;
05.
06.
07.
namespace ArcMapAddinTest
08.
{
09.
public class AddShp : ESRI.ArcGIS.Desktop.AddIns.Button
10.
{
11.
public AddShp()
12.
{
13.
}
14.
15.
protected override void OnClick()
16.
{
17.
}
18.
19.
protected override void OnUpdate()
20.
{
21.
}
22.
}
23.
}
里面包含兩個方法,onclick和onupdate方法。onclick方法是點擊按鈕時,我們在里面寫添加shp文件的代碼。
首先,添加如下引用:
1.
using System.Windows.Forms;
2.
using ESRI.ArcGIS.ArcMapUI;
3.
using ESRI.ArcGIS.Carto;
4.
using ESRI.ArcGIS.Geometry;
5.
using ESRI.ArcGIS.Geodatabase;
6.
using ESRI.ArcGIS.DataSourcesFile;
onclick方法里,事件的實現代碼如下:
01.
IMxDocument pMxd;
02.
public Button1()
03.
{
04.
pMxd = ArcMap.Document as IMxDocument;
05.
}
06.
07.
protected override void OnClick()
08.
{
09.
System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
10.
openFileDialog.Filter = "shp(*.shp)|*.shp";
11.
openFileDialog.InitialDirectory = @"D:";
12.
openFileDialog.Multiselect = false;
13.
DialogResult pDialogResult = openFileDialog.ShowDialog();
14.
if (pDialogResult != DialogResult.OK)
15.
{
16.
return;
17.
}
18.
string pPath = openFileDialog.FileName;
19.
string pFolder = System.IO.Path.GetDirectoryName(pPath);
20.
string pFileName = System.IO.Path.GetFileName(pPath);
21.
22.
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
23.
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pFolder, 0);
24.
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
25.
IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(pFileName);
26.
IFeatureLayer pFLayer = new FeatureLayerClass();
27.
pFLayer.FeatureClass = pFC;
28.
pFLayer.Name = pFC.AliasName;
29.
ILayer pLayer = pFLayer as ILayer;
30.
IMap pMap = pMxd.FocusMap;
31.
pMap.AddLayer(pLayer);
32.
33.
//
34.
// TODO: Sample code showing how to access button host
35.
//
36.
ArcMap.Application.CurrentTool = null;
37.
}
OnUpdate方法事件的代碼如下:
1.
protected override void OnUpdate()
2.
{
3.
Enabled = pMxd.FocusMap.LayerCount >= 0;
4.
}
代碼編寫完成后,編譯程序,打開編譯目錄,編譯文件如下:

雙擊.esriAddIn文件,添加工具到Arcmap中。打開Arcmap,打開擴展管理,command選項卡,找到Add-In Controls,這時候你會發現你編寫的工具會出現在這一組里面。

點擊[close],點點試試……
