打開mxd和兩控件複製
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
namespace MapCtrol //直接引用時需替換成自己當前的命名空間
{
public class MapBaseOperate
{
/// <summary>
/// 添加SHP文當
/// </summary>
/// <param name="mapControl"></param>
public static void AddShapeFile(IMapControlDefault mapControl)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "添加shp圖層文件";
openfileDlg.Filter = "map document (*.shp)|*.shp";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
bool exist = File.Exists(filepath);
if (!exist)
{
MessageBox.Show("路徑不存在!");
return;
}
string path;
string filename;
//int istart = filepath.LastIndexOf("\\");
//int iend = filepath.LastIndexOf(".");
//path = filepath.Substring(0, istart);
//filename = filepath.Substring(istart + 1, iend - istart - 1);
FileInfo fileinfo = new FileInfo(filepath);
path = filepath.Substring(0, filepath.Length - fileinfo.Name.Length);
filename = fileinfo.Name;
try
{
//加載圖層文件
mapControl.AddShapeFile(path, filename);
//設置MapControl的顯示範圍到數據的全局範圍
mapControl.Extent = mapControl.FullExtent;
}
catch (System.Exception ex)
{
MessageBox.Show("添加圖層文件失敗!" + ex.Message);
}
}
/// <summary>
/// 添加LYR文當
/// </summary>
/// <param name="mapControl"></param>
public static void AddLayerFile(IMapControlDefault mapControl)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "添加lyr圖層文件";
openfileDlg.Filter = "map documents (*.lyr)|*.lyr";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
bool exist = File.Exists(filepath);
if (!exist)
{
MessageBox.Show("路徑不存在!");
return;
}
try
{
mapControl.AddLayerFromFile(filepath);
//設置MapControl的顯示範圍到數據的全局範圍
mapControl.Extent = mapControl.FullExtent;
}
catch (System.Exception ex)
{
MessageBox.Show("添加圖層文件失敗!" + ex.Message);
}
}
/// <summary>
/// 刪除地圖所有圖層
/// </summary>
public static void DeleteAllLayers(IMapControlDefault mapControl)
{
try
{
for (int i = mapControl.LayerCount - 1; i >= 0; i-- )
{
mapControl.DeleteLayer(i);
}
}
catch (System.Exception ex)
{
MessageBox.Show("刪除圖層失敗!" + ex.Message);
}
}
/// <summary>
/// 將最底圖層,移動到最上層
/// </summary>
public static void MoveLayerToTop(IMapControlDefault mapControl)
{
try
{
if (mapControl.LayerCount > 0)
{
mapControl.MoveLayerTo(mapControl.LayerCount - 1, 0);
}
}
catch (System.Exception ex)
{
MessageBox.Show("移動圖層失敗!" + ex.Message);
}
}
/// <summary>
/// 加載地圖文當
/// </summary>
/// <param name="mapControl"></param>
public static void LoadMapDocument(IMapControlDefault mapControl)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "加載地圖文當";
openfileDlg.Filter = "map document (*.mxd)|*.mxd";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
if (mapControl.CheckMxFile(filepath))
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
mapControl.LoadMxFile(filepath, 0, Type.Missing);
mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show(filepath + "不是有效的地圖文當!");
}
}
/// <summary>
/// 加載特定地圖文當
/// </summary>
/// <param name="mapControl"></param>
public static void LoadSpecificMapDocument(IMapControlDefault mapControl, string specificMapName)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "加載特定地圖文當";
openfileDlg.Filter = "map document (*.mxd)|*.mxd";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
if (mapControl.CheckMxFile(filepath))
{
if (string.IsNullOrWhiteSpace(specificMapName))
{
int istart = filepath.LastIndexOf("\\");
int iend = filepath.LastIndexOf(".");
specificMapName = filepath.Substring(istart + 1, iend - istart - 1);
}
IArray arrayMap = mapControl.ReadMxMaps(filepath, Type.Missing);
for (int i = 0; i < arrayMap.Count; i++)
{
IMap map = arrayMap.get_Element(i) as IMap;
if (specificMapName == map.Name)
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
mapControl.LoadMxFile(filepath, 0, Type.Missing);
mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
break;
}
}
}
else
{
MessageBox.Show(filepath + "不是有效的地圖文當!");
}
}
/// <summary>
/// By MapDocument
/// </summary>
public static IMapDocument LoadMapDoc(IMapControlDefault mapControl)
{
MapDocument mapdoc = new MapDocument();
try
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "加載地圖文當";
openfileDlg.Filter = "map document (*.mxd)|*.mxd";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
mapdoc.Open(filepath, "");
for (int i = 0; i < mapdoc.MapCount; i++ )
{
mapControl.Map = mapdoc.get_Map(i);
}
mapControl.Refresh();
}
catch (System.Exception ex)
{
MessageBox.Show("加載地圖文當失敗" + ex.Message);
mapdoc = null;
}
return mapdoc;
}
/// <summary>
/// By MapDocument
/// </summary>
/// <param name="mapDoc"></param>
public static void SaveMapDoc(IMapDocument mapDoc)
{
if (null == mapDoc)
{
MessageBox.Show("保存地圖文檔失敗!");
return;
}
if (mapDoc.get_IsReadOnly(mapDoc.DocumentFilename) == true)
{
MessageBox.Show("文檔只讀無法保存!");
}
try
{
mapDoc.Save(mapDoc.UsesRelativePaths,true);
MessageBox.Show("保存地圖文檔成功!");
}
catch (System.Exception ex)
{
MessageBox.Show("保存地圖文檔失敗!" + ex.Message);
}
}
/// <summary>
/// By MapDocument
/// </summary>
/// <param name="mapDoc"></param>
public static void SaveAsMapDoc(IMapDocument mapDoc)
{
if (null == mapDoc)
{
MessageBox.Show("保存地圖文檔失敗!");
return;
}
if (mapDoc.get_IsReadOnly(mapDoc.DocumentFilename) == true)
{
MessageBox.Show("文檔只讀無法保存!");
}
SaveFileDialog savefiledlg = new SaveFileDialog();
savefiledlg.Title = "保存地圖文當";
savefiledlg.Filter = "map document (*.mxd)|*.mxd";
savefiledlg.ShowDialog();
string filepath = savefiledlg.FileName;
try
{
mapDoc.SaveAs(filepath,mapDoc.UsesRelativePaths,true);
MessageBox.Show("保存地圖文檔成功!");
}
catch (System.Exception ex)
{
MessageBox.Show("保存地圖文檔失敗!" + ex.Message);
}
}
/// <summary>
/// 縮小
/// </summary>
/// <param name="mapControl"></param>
public static void ZoomOut(IMapControlDefault mapControl)
{
try
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerPageZoomOut;
//IEnvelope ipEnv = mapControl.TrackRectangle();
IEnvelope ipEnv = mapControl.Extent;
ipEnv.Expand(2, 2, true);
mapControl.Extent = ipEnv;
}
catch (System.Exception ex)
{
MessageBox.Show("縮小失敗!" + ex.Message);
}
}
/// <summary>
/// 放大
/// </summary>
/// <param name="mapControl"></param>
public static void ZoomIn(IMapControlDefault mapControl)
{
try
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerPageZoomIn;
IEnvelope ipEnv = mapControl.TrackRectangle();
if (ipEnv.IsEmpty)
{
ipEnv = mapControl.Extent;
ipEnv.Expand(0.5, 0.5, true);
}
mapControl.Extent = ipEnv;
}
catch (System.Exception ex)
{
MessageBox.Show("放大失敗!" + ex.Message);
}
}
/// <summary>
/// 漫遊
/// </summary>
/// <param name="mapControl"></param>
public static void Pan(IMapControlDefault mapControl)
{
try
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerPagePan;
//IEnvelope ipEnv = mapControl.Extent;
mapControl.Pan();
}
catch (System.Exception ex)
{
MessageBox.Show("漫遊失敗!" + ex.Message);
}
}
/// <summary>
/// 全圖
/// </summary>
/// <param name="mapControl"></param>
public static void FullExtent(IMapControlDefault mapControl)
{
try
{
mapControl.Extent = mapControl.FullExtent;
}
catch (System.Exception ex)
{
MessageBox.Show("全圖失敗!" + ex.Message);
}
}
/// <summary>
/// 寫文字(待優化)
/// </summary>
/// <param name="mapControl"></param>
/// <param name="pGeom"></param>
/// <param name="pColor"></param>
/// <param name="text"></param>
public static void DrawMapText(IMapControlDefault mapControl, IGeometry pGeom, IRgbColor pColor, string text)
{
try
{
if (null == pColor)
{
pColor = new RgbColorClass();
pColor.Red = 255;
pColor.Green = 0;
pColor.Blue = 0;
}
ITextSymbol textsymbol = new TextSymbolClass();
textsymbol.Color = pColor;
if (null == text)
{
text = "Draw Text";
}
textsymbol.Text = "Text";
object symbol = textsymbol;
mapControl.DrawText(pGeom, text, ref symbol);
}
catch (System.Exception ex)
{
MessageBox.Show("寫文字失敗!" + ex);
}
}
/// <summary>
/// 畫圖
/// </summary>
/// <param name="mapControl"></param>
/// <param name="pGeom"></param>
/// <param name="pColor"></param>
/// <param name="width"></param>
public static void DrawMapShape(IMapControlDefault mapControl, IGeometry pGeom, IRgbColor pColor, int width)
{
try
{
if (null == pColor)
{
pColor = new RgbColorClass();
pColor.Red = 255;
pColor.Green = 255;
pColor.Blue = 0;
}
if (width < 1 || width > 20)
{
width = 5;
}
object symbol = null;
if (pGeom.GeometryType == esriGeometryType.esriGeometryPolyline)
{
ISimpleLineSymbol simpleLine = new SimpleLineSymbolClass();
simpleLine.Color = pColor;
simpleLine.Width = width;
symbol = simpleLine;
}
else
{
ISimpleFillSymbol simpleFill = new SimpleFillSymbolClass();
simpleFill.Color = pColor;
symbol = simpleFill;
}
mapControl.DrawShape(pGeom, ref symbol);
}
catch (System.Exception ex)
{
MessageBox.Show("畫圖失敗!" + ex);
}
}
/// <summary>
/// 顏色
/// </summary>
/// <param name="r"></param>
/// <param name="g"></param>
/// <param name="b"></param>
/// <param name="t"></param>
/// <returns></returns>
public static IRgbColor GetColor(int r, int g, int b, int t)
{
IRgbColor rgbcolor = new RgbColorClass();
rgbcolor.Red = r;
rgbcolor.Green = g;
rgbcolor.Blue = b;
rgbcolor.Transparency = (byte)t;
return rgbcolor;
}
/// <summary>
/// 框選指定區域(鷹眼功能)
/// </summary>
/// <param name="envelope">e.NewEnvelope</param>
/// <param name="mapControl"></param>
public static void ShowRectangleByEnvelope(IEnvelope envelope, IMapControlDefault mapControl)
{
try
{
IGraphicsContainer graphicsContainer = mapControl.Map as IGraphicsContainer;
IActiveView activeView = graphicsContainer as IActiveView;
//在繪製前,清除axMapControl2中的任何圖像元素
graphicsContainer.DeleteAllElements();
IElement element = new RectangleElementClass();
element.Geometry = envelope;
//設置鷹眼中的紅線
//產生一個符號對象
ILineSymbol outLineSymbol = new SimpleLineSymbolClass();
outLineSymbol.Width = 2;
outLineSymbol.Color = GetColor(255, 0, 0, 255);
//設置顏色屬性
//設置填充符號屬性
IFillSymbol fillsymbol = new SimpleFillSymbolClass();
fillsymbol.Color = GetColor(9, 0, 0, 0);
fillsymbol.Outline = outLineSymbol;
IFillShapeElement fillShapeElement = element as IFillShapeElement;
fillShapeElement.Symbol = fillsymbol;
graphicsContainer.AddElement((IElement)fillShapeElement, 0);
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
catch (System.Exception ex)
{
MessageBox.Show("框選指定區域失敗!" + ex);
}
}
/// <summary>
/// 清除選擇
/// </summary>
/// <param name="mapControl"></param>
public static void ClearSelection(IMapControlDefault mapControl)
{
try
{
IActiveView activeView = (IActiveView)mapControl.Map;
//清除數據集前必須先刷新
activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, mapControl.get_Layer(0), null);
mapControl.Map.ClearSelection();
activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, mapControl.get_Layer(0), null);
}
catch (System.Exception ex)
{
MessageBox.Show("清除選擇失敗!" + ex);
}
}
/// <summary>
/// 名稱查詢
/// </summary>
/// <param name="mapControl"></param>
/// <param name="value"></param>
public static void SelectByName(IMapControlDefault mapControl, string field, string value)
{
try
{
string selectName = value.Trim();
ILayer layer = mapControl.Map.get_Layer(0);
IFeatureLayer featureLayer = layer as IFeatureLayer;
IFeatureClass featureClass = featureLayer.FeatureClass;
IQueryFilter queryFilter = new QueryFilterClass();
IFeatureCursor featureCursor;
IFeature feature = null; ;
queryFilter.WhereClause = field + " = " + value;
featureCursor = featureClass.Search(queryFilter, true);
feature = featureCursor.NextFeature();
if (null != feature)
{
mapControl.Map.SelectFeature(layer, feature);
mapControl.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
}
}
catch (System.Exception ex)
{
MessageBox.Show("依據名稱查詢選中要素失敗!" + ex);
}
}
/// <summary>
/// 依據指定的Geometry(Shape)選中要素
/// </summary>
/// <param name="mapControl"></param>
/// <param name="geometry"></param>
public static void SelectByShape(IMapControlDefault mapControl, IGeometry geometry)
{
try
{
mapControl.Map.SelectByShape(geometry, null, false);
mapControl.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
}
catch (System.Exception ex)
{
MessageBox.Show("依據指定的Geometry(Shape)選中要素失敗!" + ex);
}
}
/// <summary>
/// 同步到MapControl控件
/// </summary>
public static void CopyToMapControl(IMapControlDefault mapControl, IMapControlDefault toMapControl)
{
try
{
IObjectCopy objCopy = new ObjectCopyClass();
object copyFromMap = mapControl.Map;
object copyMap = objCopy.Copy(copyFromMap);
object copyToMap = toMapControl.ActiveView.FocusMap;
objCopy.Overwrite(copyMap, ref copyToMap);
toMapControl.Extent = mapControl.FullExtent;
}
catch (System.Exception ex)
{
MessageBox.Show("Map間數據同步失敗!" + ex);
}
}
/// <summary>
/// 同步到PageLayout控件
/// </summary>
public static void CopyToPageLayout(IMapControlDefault mapControl, IPageLayoutControlDefault pageLayoutControl)
{
try
{
IObjectCopy objCopy = new ObjectCopyClass();
object copyFromMap = mapControl.Map;
object copyMap = objCopy.Copy(copyFromMap);
object copyToMap = pageLayoutControl.ActiveView.FocusMap;
objCopy.Overwrite(copyMap, ref copyToMap);
}
catch (System.Exception ex)
{
MessageBox.Show("Map與PageLayout數據同步失敗!" + ex);
}
}
/// <summary>
/// 屏幕變化後刷新屏幕
/// </summary>
/// <param name="mapControl"></param>
public static void AfterScreenDraw(IMapControlDefault mapControl)
{
try
{
IActiveView activeView = (IActiveView)mapControl.ActiveView.FocusMap;
IDisplayTransformation displayTransformation = activeView.ScreenDisplay.DisplayTransformation;
displayTransformation.VisibleBounds = mapControl.Extent;
mapControl.ActiveView.Refresh();
}
catch (System.Exception ex)
{
MessageBox.Show("刷新屏幕失敗!" + ex);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
namespace MapCtrol //直接引用時需替換成自己當前的命名空間
{
public class MapBaseOperate
{
/// <summary>
/// 添加SHP文當
/// </summary>
/// <param name="mapControl"></param>
public static void AddShapeFile(IMapControlDefault mapControl)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "添加shp圖層文件";
openfileDlg.Filter = "map document (*.shp)|*.shp";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
bool exist = File.Exists(filepath);
if (!exist)
{
MessageBox.Show("路徑不存在!");
return;
}
string path;
string filename;
//int istart = filepath.LastIndexOf("\\");
//int iend = filepath.LastIndexOf(".");
//path = filepath.Substring(0, istart);
//filename = filepath.Substring(istart + 1, iend - istart - 1);
FileInfo fileinfo = new FileInfo(filepath);
path = filepath.Substring(0, filepath.Length - fileinfo.Name.Length);
filename = fileinfo.Name;
try
{
//加載圖層文件
mapControl.AddShapeFile(path, filename);
//設置MapControl的顯示範圍到數據的全局範圍
mapControl.Extent = mapControl.FullExtent;
}
catch (System.Exception ex)
{
MessageBox.Show("添加圖層文件失敗!" + ex.Message);
}
}
/// <summary>
/// 添加LYR文當
/// </summary>
/// <param name="mapControl"></param>
public static void AddLayerFile(IMapControlDefault mapControl)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "添加lyr圖層文件";
openfileDlg.Filter = "map documents (*.lyr)|*.lyr";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
bool exist = File.Exists(filepath);
if (!exist)
{
MessageBox.Show("路徑不存在!");
return;
}
try
{
mapControl.AddLayerFromFile(filepath);
//設置MapControl的顯示範圍到數據的全局範圍
mapControl.Extent = mapControl.FullExtent;
}
catch (System.Exception ex)
{
MessageBox.Show("添加圖層文件失敗!" + ex.Message);
}
}
/// <summary>
/// 刪除地圖所有圖層
/// </summary>
public static void DeleteAllLayers(IMapControlDefault mapControl)
{
try
{
for (int i = mapControl.LayerCount - 1; i >= 0; i-- )
{
mapControl.DeleteLayer(i);
}
}
catch (System.Exception ex)
{
MessageBox.Show("刪除圖層失敗!" + ex.Message);
}
}
/// <summary>
/// 將最底圖層,移動到最上層
/// </summary>
public static void MoveLayerToTop(IMapControlDefault mapControl)
{
try
{
if (mapControl.LayerCount > 0)
{
mapControl.MoveLayerTo(mapControl.LayerCount - 1, 0);
}
}
catch (System.Exception ex)
{
MessageBox.Show("移動圖層失敗!" + ex.Message);
}
}
/// <summary>
/// 加載地圖文當
/// </summary>
/// <param name="mapControl"></param>
public static void LoadMapDocument(IMapControlDefault mapControl)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "加載地圖文當";
openfileDlg.Filter = "map document (*.mxd)|*.mxd";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
if (mapControl.CheckMxFile(filepath))
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
mapControl.LoadMxFile(filepath, 0, Type.Missing);
mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show(filepath + "不是有效的地圖文當!");
}
}
/// <summary>
/// 加載特定地圖文當
/// </summary>
/// <param name="mapControl"></param>
public static void LoadSpecificMapDocument(IMapControlDefault mapControl, string specificMapName)
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "加載特定地圖文當";
openfileDlg.Filter = "map document (*.mxd)|*.mxd";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
if (mapControl.CheckMxFile(filepath))
{
if (string.IsNullOrWhiteSpace(specificMapName))
{
int istart = filepath.LastIndexOf("\\");
int iend = filepath.LastIndexOf(".");
specificMapName = filepath.Substring(istart + 1, iend - istart - 1);
}
IArray arrayMap = mapControl.ReadMxMaps(filepath, Type.Missing);
for (int i = 0; i < arrayMap.Count; i++)
{
IMap map = arrayMap.get_Element(i) as IMap;
if (specificMapName == map.Name)
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
mapControl.LoadMxFile(filepath, 0, Type.Missing);
mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
break;
}
}
}
else
{
MessageBox.Show(filepath + "不是有效的地圖文當!");
}
}
/// <summary>
/// By MapDocument
/// </summary>
public static IMapDocument LoadMapDoc(IMapControlDefault mapControl)
{
MapDocument mapdoc = new MapDocument();
try
{
OpenFileDialog openfileDlg = new OpenFileDialog();
openfileDlg.Title = "加載地圖文當";
openfileDlg.Filter = "map document (*.mxd)|*.mxd";
openfileDlg.ShowDialog();
string filepath = openfileDlg.FileName;
mapdoc.Open(filepath, "");
for (int i = 0; i < mapdoc.MapCount; i++ )
{
mapControl.Map = mapdoc.get_Map(i);
}
mapControl.Refresh();
}
catch (System.Exception ex)
{
MessageBox.Show("加載地圖文當失敗" + ex.Message);
mapdoc = null;
}
return mapdoc;
}
/// <summary>
/// By MapDocument
/// </summary>
/// <param name="mapDoc"></param>
public static void SaveMapDoc(IMapDocument mapDoc)
{
if (null == mapDoc)
{
MessageBox.Show("保存地圖文檔失敗!");
return;
}
if (mapDoc.get_IsReadOnly(mapDoc.DocumentFilename) == true)
{
MessageBox.Show("文檔只讀無法保存!");
}
try
{
mapDoc.Save(mapDoc.UsesRelativePaths,true);
MessageBox.Show("保存地圖文檔成功!");
}
catch (System.Exception ex)
{
MessageBox.Show("保存地圖文檔失敗!" + ex.Message);
}
}
/// <summary>
/// By MapDocument
/// </summary>
/// <param name="mapDoc"></param>
public static void SaveAsMapDoc(IMapDocument mapDoc)
{
if (null == mapDoc)
{
MessageBox.Show("保存地圖文檔失敗!");
return;
}
if (mapDoc.get_IsReadOnly(mapDoc.DocumentFilename) == true)
{
MessageBox.Show("文檔只讀無法保存!");
}
SaveFileDialog savefiledlg = new SaveFileDialog();
savefiledlg.Title = "保存地圖文當";
savefiledlg.Filter = "map document (*.mxd)|*.mxd";
savefiledlg.ShowDialog();
string filepath = savefiledlg.FileName;
try
{
mapDoc.SaveAs(filepath,mapDoc.UsesRelativePaths,true);
MessageBox.Show("保存地圖文檔成功!");
}
catch (System.Exception ex)
{
MessageBox.Show("保存地圖文檔失敗!" + ex.Message);
}
}
/// <summary>
/// 縮小
/// </summary>
/// <param name="mapControl"></param>
public static void ZoomOut(IMapControlDefault mapControl)
{
try
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerPageZoomOut;
//IEnvelope ipEnv = mapControl.TrackRectangle();
IEnvelope ipEnv = mapControl.Extent;
ipEnv.Expand(2, 2, true);
mapControl.Extent = ipEnv;
}
catch (System.Exception ex)
{
MessageBox.Show("縮小失敗!" + ex.Message);
}
}
/// <summary>
/// 放大
/// </summary>
/// <param name="mapControl"></param>
public static void ZoomIn(IMapControlDefault mapControl)
{
try
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerPageZoomIn;
IEnvelope ipEnv = mapControl.TrackRectangle();
if (ipEnv.IsEmpty)
{
ipEnv = mapControl.Extent;
ipEnv.Expand(0.5, 0.5, true);
}
mapControl.Extent = ipEnv;
}
catch (System.Exception ex)
{
MessageBox.Show("放大失敗!" + ex.Message);
}
}
/// <summary>
/// 漫遊
/// </summary>
/// <param name="mapControl"></param>
public static void Pan(IMapControlDefault mapControl)
{
try
{
mapControl.MousePointer = esriControlsMousePointer.esriPointerPagePan;
//IEnvelope ipEnv = mapControl.Extent;
mapControl.Pan();
}
catch (System.Exception ex)
{
MessageBox.Show("漫遊失敗!" + ex.Message);
}
}
/// <summary>
/// 全圖
/// </summary>
/// <param name="mapControl"></param>
public static void FullExtent(IMapControlDefault mapControl)
{
try
{
mapControl.Extent = mapControl.FullExtent;
}
catch (System.Exception ex)
{
MessageBox.Show("全圖失敗!" + ex.Message);
}
}
/// <summary>
/// 寫文字(待優化)
/// </summary>
/// <param name="mapControl"></param>
/// <param name="pGeom"></param>
/// <param name="pColor"></param>
/// <param name="text"></param>
public static void DrawMapText(IMapControlDefault mapControl, IGeometry pGeom, IRgbColor pColor, string text)
{
try
{
if (null == pColor)
{
pColor = new RgbColorClass();
pColor.Red = 255;
pColor.Green = 0;
pColor.Blue = 0;
}
ITextSymbol textsymbol = new TextSymbolClass();
textsymbol.Color = pColor;
if (null == text)
{
text = "Draw Text";
}
textsymbol.Text = "Text";
object symbol = textsymbol;
mapControl.DrawText(pGeom, text, ref symbol);
}
catch (System.Exception ex)
{
MessageBox.Show("寫文字失敗!" + ex);
}
}
/// <summary>
/// 畫圖
/// </summary>
/// <param name="mapControl"></param>
/// <param name="pGeom"></param>
/// <param name="pColor"></param>
/// <param name="width"></param>
public static void DrawMapShape(IMapControlDefault mapControl, IGeometry pGeom, IRgbColor pColor, int width)
{
try
{
if (null == pColor)
{
pColor = new RgbColorClass();
pColor.Red = 255;
pColor.Green = 255;
pColor.Blue = 0;
}
if (width < 1 || width > 20)
{
width = 5;
}
object symbol = null;
if (pGeom.GeometryType == esriGeometryType.esriGeometryPolyline)
{
ISimpleLineSymbol simpleLine = new SimpleLineSymbolClass();
simpleLine.Color = pColor;
simpleLine.Width = width;
symbol = simpleLine;
}
else
{
ISimpleFillSymbol simpleFill = new SimpleFillSymbolClass();
simpleFill.Color = pColor;
symbol = simpleFill;
}
mapControl.DrawShape(pGeom, ref symbol);
}
catch (System.Exception ex)
{
MessageBox.Show("畫圖失敗!" + ex);
}
}
/// <summary>
/// 顏色
/// </summary>
/// <param name="r"></param>
/// <param name="g"></param>
/// <param name="b"></param>
/// <param name="t"></param>
/// <returns></returns>
public static IRgbColor GetColor(int r, int g, int b, int t)
{
IRgbColor rgbcolor = new RgbColorClass();
rgbcolor.Red = r;
rgbcolor.Green = g;
rgbcolor.Blue = b;
rgbcolor.Transparency = (byte)t;
return rgbcolor;
}
/// <summary>
/// 框選指定區域(鷹眼功能)
/// </summary>
/// <param name="envelope">e.NewEnvelope</param>
/// <param name="mapControl"></param>
public static void ShowRectangleByEnvelope(IEnvelope envelope, IMapControlDefault mapControl)
{
try
{
IGraphicsContainer graphicsContainer = mapControl.Map as IGraphicsContainer;
IActiveView activeView = graphicsContainer as IActiveView;
//在繪製前,清除axMapControl2中的任何圖像元素
graphicsContainer.DeleteAllElements();
IElement element = new RectangleElementClass();
element.Geometry = envelope;
//設置鷹眼中的紅線
//產生一個符號對象
ILineSymbol outLineSymbol = new SimpleLineSymbolClass();
outLineSymbol.Width = 2;
outLineSymbol.Color = GetColor(255, 0, 0, 255);
//設置顏色屬性
//設置填充符號屬性
IFillSymbol fillsymbol = new SimpleFillSymbolClass();
fillsymbol.Color = GetColor(9, 0, 0, 0);
fillsymbol.Outline = outLineSymbol;
IFillShapeElement fillShapeElement = element as IFillShapeElement;
fillShapeElement.Symbol = fillsymbol;
graphicsContainer.AddElement((IElement)fillShapeElement, 0);
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
catch (System.Exception ex)
{
MessageBox.Show("框選指定區域失敗!" + ex);
}
}
/// <summary>
/// 清除選擇
/// </summary>
/// <param name="mapControl"></param>
public static void ClearSelection(IMapControlDefault mapControl)
{
try
{
IActiveView activeView = (IActiveView)mapControl.Map;
//清除數據集前必須先刷新
activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, mapControl.get_Layer(0), null);
mapControl.Map.ClearSelection();
activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, mapControl.get_Layer(0), null);
}
catch (System.Exception ex)
{
MessageBox.Show("清除選擇失敗!" + ex);
}
}
/// <summary>
/// 名稱查詢
/// </summary>
/// <param name="mapControl"></param>
/// <param name="value"></param>
public static void SelectByName(IMapControlDefault mapControl, string field, string value)
{
try
{
string selectName = value.Trim();
ILayer layer = mapControl.Map.get_Layer(0);
IFeatureLayer featureLayer = layer as IFeatureLayer;
IFeatureClass featureClass = featureLayer.FeatureClass;
IQueryFilter queryFilter = new QueryFilterClass();
IFeatureCursor featureCursor;
IFeature feature = null; ;
queryFilter.WhereClause = field + " = " + value;
featureCursor = featureClass.Search(queryFilter, true);
feature = featureCursor.NextFeature();
if (null != feature)
{
mapControl.Map.SelectFeature(layer, feature);
mapControl.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
}
}
catch (System.Exception ex)
{
MessageBox.Show("依據名稱查詢選中要素失敗!" + ex);
}
}
/// <summary>
/// 依據指定的Geometry(Shape)選中要素
/// </summary>
/// <param name="mapControl"></param>
/// <param name="geometry"></param>
public static void SelectByShape(IMapControlDefault mapControl, IGeometry geometry)
{
try
{
mapControl.Map.SelectByShape(geometry, null, false);
mapControl.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
}
catch (System.Exception ex)
{
MessageBox.Show("依據指定的Geometry(Shape)選中要素失敗!" + ex);
}
}
/// <summary>
/// 同步到MapControl控件
/// </summary>
public static void CopyToMapControl(IMapControlDefault mapControl, IMapControlDefault toMapControl)
{
try
{
IObjectCopy objCopy = new ObjectCopyClass();
object copyFromMap = mapControl.Map;
object copyMap = objCopy.Copy(copyFromMap);
object copyToMap = toMapControl.ActiveView.FocusMap;
objCopy.Overwrite(copyMap, ref copyToMap);
toMapControl.Extent = mapControl.FullExtent;
}
catch (System.Exception ex)
{
MessageBox.Show("Map間數據同步失敗!" + ex);
}
}
/// <summary>
/// 同步到PageLayout控件
/// </summary>
public static void CopyToPageLayout(IMapControlDefault mapControl, IPageLayoutControlDefault pageLayoutControl)
{
try
{
IObjectCopy objCopy = new ObjectCopyClass();
object copyFromMap = mapControl.Map;
object copyMap = objCopy.Copy(copyFromMap);
object copyToMap = pageLayoutControl.ActiveView.FocusMap;
objCopy.Overwrite(copyMap, ref copyToMap);
}
catch (System.Exception ex)
{
MessageBox.Show("Map與PageLayout數據同步失敗!" + ex);
}
}
/// <summary>
/// 屏幕變化後刷新屏幕
/// </summary>
/// <param name="mapControl"></param>
public static void AfterScreenDraw(IMapControlDefault mapControl)
{
try
{
IActiveView activeView = (IActiveView)mapControl.ActiveView.FocusMap;
IDisplayTransformation displayTransformation = activeView.ScreenDisplay.DisplayTransformation;
displayTransformation.VisibleBounds = mapControl.Extent;
mapControl.ActiveView.Refresh();
}
catch (System.Exception ex)
{
MessageBox.Show("刷新屏幕失敗!" + ex);
}
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.