1.新建窗体项目
打开C#,新建windows窗体项目,添加控件,ArcGIS Windows Forms:MapControl,TOCControl,
LicenseControl 和Janus WinForms Controls v4:Ribbon
2.属性关联
右击TOCControl,选择属性,将Buddy改为Mapcontrol
3.编写每个控件的功能
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
namespace basic_tool
{
public partial class Form1 : Form
{
//定义变量
private IClone pClone;
private IEnvelope pEnvelope;
int flag = 0;
public Form1()
{
InitializeComponent();
}
private void btn_open_mxd_Click(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
{
System.Windows.Forms.OpenFileDialog openFileDialog;
openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开地图文档";
openFileDialog.Filter = "map documents(*.mxd)|*.mxd";
openFileDialog.ShowDialog();
string filepath = openFileDialog.FileName;
if (axMapControl1.CheckMxFile(filepath))
{
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;//鼠标沙漏等待功能
axMapControl1.LoadMxFile(filepath, 0, Type.Missing);//打开图层
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;//鼠标沙漏等待功能停止
}
else
{
MessageBox.Show(filepath + "不是有效的地图文档");
}
}
//放大
private void btn_zoomin_Click(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
{
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerPageZoomIn;//鼠标形状控制
flag = 2;
}
//缩小
private void btn_zoomout_Click(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
{
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerPageZoomOut;
flag = 1;
}
//全屏
private void btn_full_Click(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
{
axMapControl1.Extent = axMapControl1.FullExtent;//全屏显示
}
//漫游
private void btn_pan_Click(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
{
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerPan;
flag = 3;
}
//中心放大
private void btn_zoomin_click_Click(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
{
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerPageZoomIn;
pClone = axMapControl1.Extent as IClone;
pEnvelope = pClone as IEnvelope;
pEnvelope.Expand(0.8, 0.8, true);
axMapControl1.Extent = pEnvelope;
}
//中心缩小
private void btn_zoomout_click_Click(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
{
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerPageZoomOut;
pClone = axMapControl1.Extent as IClone;
pEnvelope = pClone as IEnvelope;
pEnvelope.Expand(2, 2, true);
axMapControl1.Extent = pEnvelope;
}
//地图显示
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
ESRI.ArcGIS.Geometry.IEnvelope ipEnv;//虚拟矩形
if (flag == 2)
{
ipEnv = axMapControl1.TrackRectangle();//画框放大
ipEnv = axMapControl1.Extent;//点击放大
ipEnv.Expand(0.8, 0.8, true);//放大比例,0.8为比例系数,可修改
axMapControl1.Extent = ipEnv;
}
else if (flag == 1)
{
ipEnv = axMapControl1.TrackRectangle();
ipEnv = axMapControl1.Extent;
ipEnv.Expand(2, 2, true);
axMapControl1.Extent = ipEnv;
}
else if (flag == 3)
{
axMapControl1.Pan();//漫游
}
}
}
}