使用ArcGIS Engine提供的命令和工具实现MapView操作



#region MapOperateBase
               
        #region NewMxd
        /// <summary>
        /// 新建Mxd文档
        /// </summary>
        private void NewMxd()
        {
            SaveFileDialog mxdFile = new SaveFileDialog();
            mxdFile.Title = "新建ArcMap文档";
            mxdFile.Filter = "ArcMap文档 (mxd)|*.mxd";
            if (mxdFile.ShowDialog() == DialogResult.OK)
            {
                string mxdFilePath = mxdFile.FileName;
                if (_MapDocument == null)
                {
                    _MapDocument = new MapDocumentClass();
                    _MapDocument.New(mxdFilePath);
                }
                else
                {
                    _MapDocument.Open(mxdFilePath, null);
                }
                _MapDocument.Save(true, true);
                this.mapControl.Map = this._MapDocument.get_Map(0);
            }
        }
        #endregion

        #region OpenMxd
        /// <summary>
        /// 打开Mxd文件
        /// </summary>
        private void OpenMxd()
        {
            OpenFileDialog ofDialog = new OpenFileDialog();
            ofDialog.Title = "选择ArcMap文档";
            ofDialog.Filter = "ArcMap文档 (mxd)|*.mxd";
            if (ofDialog.ShowDialog() == DialogResult.OK)
            {
                string mxdFilePath = ofDialog.FileName;
                if (null == _MapDocument)
                {
                    _MapDocument = new MapDocumentClass();                   
                }
                _MapDocument.Open(mxdFilePath, null);
            }
            IMap map = _MapDocument.get_Map(0);
            this.mapControl.Map = map;
        }
        #endregion

        #region AddShapeFile
        /// <summary>
        /// 添加shp文件
        /// </summary>
        private void AddShapeFile()
        {
            IWorkspaceFactory shpFactory = new ShapefileWorkspaceFactoryClass();
            OpenFileDialog ofDialog = new OpenFileDialog();
            ofDialog.Title = "输入ArcMap文档名称";
            ofDialog.Filter = "ArcView Shape文件|*.shp";
            ofDialog.Multiselect = true;
            if (ofDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = ofDialog.FileName;
                string filePath = fileName.Substring(0, fileName.LastIndexOf('\\'));
                IWorkspace shpWorkspace = shpFactory.OpenFromFile(filePath, 0);
                // 选择多个shape文件(取Map名称)
                string[] files = ofDialog.FileNames;
                IEnumDataset ds = shpWorkspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
                IDataset featureClass = ds.Next();
                while(featureClass != null)
                {
                    if (IsFileExists(featureClass.Name, files))
                    {
                        IFeatureLayer featureLayer = new FeatureLayerClass();
                        featureLayer.Name = featureClass.Name;
                        featureLayer.FeatureClass = featureClass as IFeatureClass;
                        mapControl.AddLayer(featureLayer as ILayer, 0);
                    }
                    featureClass = ds.Next();
                }

                IMxdContents mxdContents = mapControl.Map as IMxdContents;
                _MapDocument.ReplaceContents(mxdContents);
            }
          
           
        }
        #endregion

        #region MxdSave
        /// <summary>
        /// 保存Mxd
        /// </summary>
        private void MxdSave()
        {
            if (_MapDocument.get_IsReadOnly(_MapDocument.DocumentFilename) == true)
            {
                MessageBox.Show("此地图文档为只读!");
                return;
            }
            _MapDocument.Save(_MapDocument.UsesRelativePaths, true);
            MessageBox.Show("地图保存成功!");
        }
        #endregion

        #region MxdSaveAs
        /// <summary>
        /// Mxd令存为
        /// </summary>
        private void MxdSaveAs()
        {
            SaveFileDialog mxdFile = new SaveFileDialog();
            mxdFile.Title = "新建ArcMap文档";
            mxdFile.Filter = "ArcMap文档 (mxd)|*.mxd";
            if (mxdFile.ShowDialog() == DialogResult.OK)
            {
                string mxdFilePath = mxdFile.FileName;
                if (_MapDocument.get_IsReadOnly(_MapDocument.DocumentFilename) == true)
                {
                    MessageBox.Show("此地图文档为只读!");
                    return;
                }
                _MapDocument.SaveAs(mxdFilePath,true, true);
            }           
        }

        #endregion

        #region IsFileExists
        /// <summary>
        ///
        /// </summary>
        /// <param name="value"></param>
        /// <param name="files"></param>
        /// <returns></returns>
        bool IsFileExists(string value, string[] files)
        {
            foreach (string file in files)
            {
                if (file.IndexOf(value + ".shp") > 0)
                    return true;
            }
            return false;
        }
        #endregion

#endregion



#region MapView

        #region MapZoomIn
        /// <summary>
        /// 放大
        /// </summary>
        private void MapZoomIn()
        {
            ICommand command = new ControlsMapZoomInToolClass();
            command.OnCreate(mapControl.Object);
            mapControl.CurrentTool = command as ITool;
        }
        #endregion

        #region MapZoomOut
        /// <summary>
        /// 缩小
        /// </summary>
        private void MapZoomOut()
        {
            ICommand command = new ControlsMapZoomOutToolClass();
            command.OnCreate(mapControl.Object);
            mapControl.CurrentTool = command as ITool;
        }
        #endregion

        #region MapPan
        /// <summary>
        /// 平移
        /// </summary>
        private void MapPan()
        {
            ICommand command = new ControlsMapPanToolClass();
            command.OnCreate(mapControl.Object);
            mapControl.CurrentTool = command as ITool;
        }
        #endregion

        #region MapFullExtent
        /// <summary>
        /// 全屏显示
        /// </summary>
        private void MapFullExtent()
        {
            ICommand command = new ControlsMapFullExtentCommand();
            command.OnCreate(mapControl.Object);
            command.OnClick();
        }
        #endregion

        #region MapForwardView
        /// <summary>
        /// 上一视图
        /// </summary>
        private void MapForwardView()
        {
            ICommand command = new ControlsMapZoomToLastExtentForwardCommandClass();
            command.OnCreate(mapControl.Object);
            command.OnClick();
        }
        #endregion

        #region MapNextView
        /// <summary>
        /// 下一视图
        /// </summary>
        private void MapNextView()
        {
            ICommand command = new ControlsMapZoomToLastExtentBackCommandClass();
            command.OnCreate(mapControl.Object);
            command.OnClick();
        }
        #endregion

        #region MapFixedZoomIn
        /// <summary>
        /// 定点放大(1.5倍)
        /// </summary>
        private void MapFixedZoomIn()
        {
            IActiveView activeView = mapControl.ActiveView;
            IEnvelope envelope = activeView.Extent;
            envelope.Expand(0.5, 0.5, true);
            activeView.Extent = envelope;
            mapControl.CurrentTool = null;
            activeView.Refresh();
        }
        #endregion

        #region MapFixedZoomOut
        /// <summary>
        /// 定点放大(1.5倍)
        /// </summary>
        private void MapFixedZoomOut()
        {
            IActiveView activeView = mapControl.ActiveView;
            IEnvelope envelope = activeView.Extent;
            envelope.Expand(2, 2, true);
            activeView.Extent = envelope;
            mapControl.CurrentTool = null;
            activeView.Refresh();
        }
        #endregion

        #region MapRefresh
        /// <summary>
        /// 视图刷新
        /// </summary>
        private void MapRefresh()
        {
            ICommand command = new ControlsMapRefreshViewCommand();
            command.OnCreate(mapControl.Object);
            command.OnClick();
        }
        #endregion               
 
       
#endregion

你可能感兴趣的:(ArcGis Engine)