代码如下:
#region "工具条按钮"
//拉框放大
private void toolStripButton_Zoomin_Click(object sender, EventArgs e)
{
currentoperation = "拉框放大";
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerZoomIn;
}
//拉框缩小
private void toolStripButton_Zoomout_Click(object sender, EventArgs e)
{
currentoperation = "拉框缩小";
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerZoomOut;
}
//平移
private void toolStripButton_Pan_Click(object sender, EventArgs e)
{
currentoperation = "平移漫游";
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerPan;
}
//全图
private void toolStripButton_FullExtent_Click(object sender, EventArgs e)
{
currentoperation = "全图显示";
axMapControl1.Extent = axMapControl1.FullExtent;
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
//上一视图
private void toolStripButton_LastExtent_Click(object sender, EventArgs e)
{
currentoperation = "上一视图";
IExtentStack pExtentStack = null;
pExtentStack = axMapControl1.ActiveView.ExtentStack;
if (pExtentStack.CanUndo())
pExtentStack.Undo();
axMapControl1.Refresh();
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
//下一视图
private void toolStripButton_NextExtent_Click(object sender, EventArgs e)
{
currentoperation = "下一视图";
IExtentStack pExtentStack = null;
pExtentStack = axMapControl1.ActiveView.ExtentStack;
if (pExtentStack.CanRedo())
pExtentStack.Redo();
axMapControl1.Refresh();
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
//中心放大
private void toolStripButton_Zoomin1_Click(object sender, EventArgs e)
{
currentoperation = "中心放大";
IEnvelope objEnvelope = null;
objEnvelope = axMapControl1.Extent;
objEnvelope.Expand(0.5, 0.5, true);
axMapControl1.Extent = objEnvelope;
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
//中心缩小
private void toolStripButton_Zoomout1_Click(object sender, EventArgs e)
{
currentoperation = "中心缩小";
IEnvelope objEnvelope = null;
objEnvelope = axMapControl1.Extent;
objEnvelope.Expand(2, 2, true);
axMapControl1.Extent = objEnvelope;
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
//刷新
private void toolStripButton_Refresh_Click(object sender, EventArgs e)
{
currentoperation = "刷新";
axMapControl1.Refresh();
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
//长度量测
private void toolStripButton_LengthMeasure_Click(object sender, EventArgs e)
{
currentoperation = "长度量测";
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
//面积量测
private void toolStripButton_AreaMeasure_Click(object sender, EventArgs e)
{
currentoperation = "面积量测";
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
//地图输出
private void toolStripButton_OutputMap_Click(object sender, EventArgs e)
{
currentoperation = "地图输出";
OutputMap();
}
#endregion
//=========================================================================================================
#region"axMapControl事件"
//鼠标按下事件
private void axMapControl1_OnMouseDown(object sender, AxESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
IEnvelope objEnvelope = null;
IPoint pPoint = null;
double x;
x = (6371110 * 2 * Math.PI) / 360;
IActiveView pActiveView = axMapControl1.ActiveView.FocusMap as IActiveView;
pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);
switch (currentoperation)
{
case "拉框放大":
objEnvelope = axMapControl1.TrackRectangle();
if (!objEnvelope.IsEmpty)
{
axMapControl1.Extent = objEnvelope;
}
else
{
//点击放大
pPoint.X = e.mapX;
pPoint.Y = e.mapY;
pPoint.Z = 0;
objEnvelope = axMapControl1.Extent;
objEnvelope.CenterAt(pPoint);
objEnvelope.Expand(0.5, 0.5, true);
axMapControl1.Extent = objEnvelope;
}
break;
case "拉框缩小":
objEnvelope = axMapControl1.TrackRectangle();
IEnvelope currentExtent = this.axMapControl1.Extent;
double dXmin = 0, dYmin = 0, dXmax = 0, dYmax = 0, dHeight = 0, dWidth = 0;
if (!objEnvelope.IsEmpty)
{
dWidth = currentExtent.Width * (currentExtent.Width / objEnvelope.Width);
dHeight = currentExtent.Height * (currentExtent.Height / objEnvelope.Height);
dXmin = currentExtent.XMin - ((objEnvelope.XMin - currentExtent.XMin) * (currentExtent.Width / objEnvelope.Width));
dYmin = currentExtent.YMin - ((objEnvelope.YMin - currentExtent.YMin) * (currentExtent.Height / objEnvelope.Height));
dXmax = dXmin + dWidth;
dYmax = dYmin + dHeight;
objEnvelope.PutCoords(dXmin, dYmin, dXmax, dYmax);
this.axMapControl1.Extent = objEnvelope;
}
else
{
//点击缩小
pPoint.X = e.mapX;
pPoint.Y = e.mapY;
pPoint.Z = 0;
objEnvelope = axMapControl1.Extent;
objEnvelope.CenterAt(pPoint);
objEnvelope.Expand(2, 2, true);
axMapControl1.Extent = objEnvelope;
}
break;
case "平移漫游":
axMapControl1.Pan();
break;
case "长度量测":
IPolyline pPolyline = null;
pPolyline = (IPolyline)axMapControl1.TrackLine();
double l ;
l =Math.Abs( Math.Round(pPolyline.Length * x,2));
MessageBox.Show("您量测的距离为" + l.ToString() + "米。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case "面积量测":
IPolygon pPolygon = null;
pPolygon =(IPolygon)axMapControl1.TrackPolygon();
IArea pArea =(IArea)pPolygon;
double s;
s = Math.Abs(Math.Round(pArea.Area * x * x, 2));
MessageBox.Show("您量测的距离为" + s.ToString() + "平方米。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
//鼠标移动事件,获取坐标值
private void axMapControl1_OnMouseMove(object sender, AxESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
{
//double X = 0;
//double Y = 0;
//X = Math.Round(e.mapX, 6); //保留6位小数
//Y = Math.Round(e.mapY, 6);
toolStripStatusLabel_X.Text = "X=" + e.mapX.ToString() + " ";
toolStripStatusLabel_Y.Text = "Y=" + e.mapY.ToString();
}
//获取当前比例尺
private void axMapControl1_OnViewRefreshed(object sender, AxESRI.ArcGIS.Controls.IMapControlEvents2_OnViewRefreshedEvent e)
{
double S= Math.Round(axMapControl1.MapScale, 0); //保留0位小数
toolStripStatusLabel_Scale.Text = "比列尺: 1:" + S.ToString() + " ";
}
#endregion
#region"私有函数"
private void OutputMap()
{
try
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "(*jpg)|*jpg|(*.tif)|*tif|(*.pdf)|*.pdf|(*.bmp)|*.bmp";
sfd.Title = "图片另存为";
if (sfd.ShowDialog() == DialogResult.OK)
{
IExport pExport = null;
if (1 == sfd.FilterIndex)
{
pExport = new ExportJPEGClass();
pExport.ExportFileName = sfd.FileName + ".jpeg";
}
else if (2 == sfd.FilterIndex)
{
pExport = new ExportTIFFClass();
pExport.ExportFileName = sfd.FileName + ".tif";
}
else if (3 == sfd.FilterIndex)
{
pExport = new ExportPDFClass();
pExport.ExportFileName = sfd.FileName + ".pdf";
}
else if (4 == sfd.FilterIndex)
{
pExport = new ExportBMPClass();
pExport.ExportFileName = sfd.FileName + ".bmp";
}
//pExport.ExportFileName = sfd.FileName;
int res = 96;
pExport.Resolution = res;
tagRECT exportRECT = axMapControl1.ActiveView.ExportFrame;
IEnvelope pENV = new EnvelopeClass();
pENV.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
pExport.PixelBounds = pENV;
int Hdc = pExport.StartExporting();
IEnvelope pVisibleBounds = null;
ESRI.ArcGIS.esriSystem.ITrackCancel pTrack = null;
axMapControl1.ActiveView.Output(Hdc, (int)pExport.Resolution, ref exportRECT, pVisibleBounds, pTrack);
Application.DoEvents();
pExport.FinishExporting();
pExport.Cleanup();
}
MessageBox.Show("地图输出成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
}
}