www.pudn.com > ArcgisMapOperation.rar > MainForm.cs


using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using System.IO; 
using System.Runtime.InteropServices; 
using System.Collections.Generic; 
 
using ESRI.ArcGIS.esriSystem; 
using ESRI.ArcGIS.Carto; 
using ESRI.ArcGIS.Controls; 
using ESRI.ArcGIS.ADF; 
using ESRI.ArcGIS.SystemUI; 
using ESRI.ArcGIS.Geodatabase; 
using ESRI.ArcGIS.Geometry; 
using ESRI.ArcGIS.Display; 
 
using ArcgisMapOperation.EsriLib; 
using ArcgisMapOperation.Struct; 
using ArcgisMapOperation.Enum; 
 
namespace ArcgisMapOperation 
{ 
    public sealed partial class MainForm : Form 
    { 
        #region 私有成员 
 
        ///  
        /// 主图指针 
        ///  
        private IMapControl3 mapControlMain ; 
 
        ///  
        /// 鹰眼图指针 
        ///  
        private IMapControl3 mapControlOverview ; 
 
        ///  
        /// 工作区的名称 
        ///  
        private string strMapDocumentName ; 
 
        ///  
        /// 主图操作标志 
        ///  
        private MapViewOperation Flag; 
 
        ///  
        /// 判断是开始点还是结束点 
        ///  
        private bool bStartOrEndPoint; 
 
        ///  
        /// 屏幕操作对象 
        ///  
        private IScreenDisplay sceenDisplay; 
 
        ///  
        /// 画图 
        ///  
        private IPolygon polygonDraw; 
 
        ///  
        /// 工作区 
        ///  
        private IWorkspace workSpace; 
 
        #endregion 
 
        #region 初始化 
 
        public MainForm() 
        { 
            InitializeComponent(); 
        } 
 
        private void MainForm_Load(object sender, EventArgs e) 
        { 
            mapControlMain = (IMapControl3)axMapControlMain.Object; 
            mapControlOverview = (IMapControl3)axMapControlOverview.Object; 
             
            menuSaveDoc.Enabled = false; 
        } 
 
        #endregion 
 
        #region “文件”菜单事件 
 
        ///  
        /// 新工作区 
        ///  
        ///  
        ///  
        private void menuNewDoc_Click(object sender, EventArgs e) 
        { 
            ICommand command = new CreateNewDocument(); 
            command.OnCreate(mapControlMain.Object); 
            command.OnClick(); 
        } 
 
        ///  
        /// 打开工作区 
        ///  
        ///  
        ///  
        private void menuOpenDoc_Click(object sender, EventArgs e) 
        { 
            ICommand command = new ControlsOpenDocCommandClass(); 
            command.OnCreate(mapControlMain.Object); 
            command.OnClick(); 
        } 
 
        ///  
        /// 保存工作区 
        ///  
        ///  
        ///  
        private void menuSaveDoc_Click(object sender, EventArgs e) 
        { 
            if (mapControlMain.CheckMxFile(strMapDocumentName)) 
            { 
                //create a new instance of a MapDocument 
                IMapDocument mapDoc = new MapDocumentClass(); 
                mapDoc.Open(strMapDocumentName, string.Empty); 
 
                //Make sure that the MapDocument is not readonly 
                if (mapDoc.get_IsReadOnly(strMapDocumentName)) 
                { 
                    MessageBox.Show("Map document is read only!"); 
                    mapDoc.Close(); 
                    return; 
                } 
 
                //Replace its contents with the current map 
                mapDoc.ReplaceContents((IMxdContents)mapControlMain.Map); 
 
                //save the MapDocument in order to persist it 
                mapDoc.Save(mapDoc.UsesRelativePaths, false); 
 
                //close the MapDocument 
                mapDoc.Close(); 
            } 
        } 
 
        ///  
        /// 另存工作区 
        ///  
        ///  
        ///  
        private void menuSaveAs_Click(object sender, EventArgs e) 
        { 
            ICommand command = new ControlsSaveAsDocCommandClass(); 
            command.OnCreate(mapControlMain.Object); 
            command.OnClick(); 
        } 
 
        ///  
        /// 退出 
        ///  
        ///  
        ///  
        private void menuExitApp_Click(object sender, EventArgs e) 
        { 
            Application.Exit(); 
        } 
        #endregion 
 
        #region "Database"菜单事件 
        ///  
        private void tsmiLoadData_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                //首先清除所有的图层 
                this.axMapControlMain.Map.ClearLayers(); 
 
                string strAccessFile = SystemConfig.DataBaseCon; 
                workSpace = UserDatabaseOperation.GetWorkspaceBySetPerproty(strAccessFile); 
 
                ILayer pLayer = UserFeatureOperation.AddFeature(workSpace, "test_py"); 
                this.axMapControlMain.Map.AddLayer(pLayer); 
 
                pLayer = UserFeatureOperation.AddFeature(workSpace, "test_pt"); 
                this.axMapControlMain.Map.AddLayer(pLayer); 
 
                pLayer = UserFeatureOperation.AddFeature(workSpace, "test_pl"); 
                this.axMapControlMain.Map.AddLayer(pLayer); 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message); 
            } 
        } 
        #endregion 
 
        #region "SpatialFilter"菜单事件 
        ///  
        /// 测试空间过滤 
        ///  
        ///  
        ///  
        private void tsmiFilterTest_Click(object sender, EventArgs e) 
        { 
            Flag = MapViewOperation.DrawGeometry; 
            bStartOrEndPoint = false; 
        } 
        #endregion 
 
        #region "Query"菜单事件 
        ///  
        /// 查询 
        ///  
        ///  
        ///  
        private void tsmiQueryTest_Click(object sender, EventArgs e) 
        { 
            IQueryFilter pQueryFilter = new QueryFilter(); 
            pQueryFilter.WhereClause = "name ='A'"; 
 
            if (mapControlMain.LayerCount > 0) 
            { 
                IFeatureSelection featureSelection = mapControlMain.get_Layer(0) as IFeatureSelection; 
                featureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); 
                featureSelection.SelectionSet.Refresh(); 
                mapControlMain.ActiveView.Refresh(); 
            } 
        } 
 
        ///  
        /// 内存中添加新记录 
        ///  
        ///  
        ///  
        private void tsmiAddNewRecord_Click(object sender, EventArgs e) 
        { 
            IFeatureLayer featureLayer = mapControlMain.get_Layer(0) as IFeatureLayer; 
 
            IPoint pointStart = new PointClass(); 
            pointStart.X = 80; 
            pointStart.Y = 67; 
 
            IPoint pointEnd = new PointClass(); 
            pointEnd.X = 121; 
            pointEnd.Y = 26; 
 
            IPolyline polyLine = UserGeometryGenerate.CreatePolyline(pointStart, pointEnd); 
 
            List lstFieldName = new List(); 
            lstFieldName.Add("name"); 
 
            List lstFieldValue = new List(); 
            lstFieldValue.Add("lj"); 
 
            TableInfo tabInfo = new TableInfo(); 
            tabInfo.ShapeValue = polyLine; 
            tabInfo.FieldName = lstFieldName; 
            tabInfo.FieldValue = lstFieldValue; 
 
            UserPerprotyQuery.CreateNewFeature(featureLayer.FeatureClass, tabInfo); 
            mapControlMain.ActiveView.Refresh(); 
        } 
        #endregion 
 
        #region "Menu"菜单事件 
        ///  
        /// 颜色设置 
        ///  
        ///  
        ///  
        private void tsmiColorSetting_Click(object sender, EventArgs e) 
        { 
            IFeatureLayer featureLayer = mapControlMain.get_Layer(0) as IFeatureLayer; 
 
            UserColorSetting userColorSetting = new UserColorSetting(); 
 
            IColor colorSet = new RgbColor(); 
            colorSet.UseWindowsDithering = true; 
            colorSet.RGB = Color.Red.ToArgb(); 
 
            userColorSetting.SetLayerColor(featureLayer, colorSet); 
            mapControlMain.ActiveView.Refresh(); 
        } 
        #endregion 
 
 
        #region 主图控件事件 
        ///  
        /// 主图数据呈现事件 
        ///  
        ///  
        ///  
        private void axMapControlMain_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e) 
        { 
            strMapDocumentName = mapControlMain.DocumentFilename; 
 
            if (string.IsNullOrEmpty(strMapDocumentName)) 
            { 
                menuSaveDoc.Enabled = false; 
                statusBarXY.Text = string.Empty; 
            } 
            else 
            { 
                menuSaveDoc.Enabled = true; 
                statusBarXY.Text = System.IO.Path.GetFileName(strMapDocumentName); 
            } 
        } 
 
        ///  
        /// 主图鼠标移动事件 
        ///  
        ///  
        ///  
        private void axMapControlMain_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) 
        { 
            statusBarXY.Text = string.Format("{0}, {1}  {2}", e.mapX.ToString("#######.##"), e.mapY.ToString("#######.##"), 
                axMapControlMain.MapUnits.ToString().Substring(4)); 
        } 
 
        ///  
        /// 显示地图时触发 
        ///  
        ///  
        ///  
        private void axMapControlMain_OnAfterScreenDraw(object sender, IMapControlEvents2_OnAfterScreenDrawEvent e) 
        { 
            //添加鹰眼图数据(跟主图保持一致) 
            for (int i = 0; i < mapControlMain.LayerCount; i++) 
            { 
                mapControlOverview.AddLayer(mapControlMain.get_Layer(i), i); 
            } 
        } 
 
        ///  
        /// 主图鼠标按下事件 
        ///  
        ///  
        ///  
        private void axMapControlMain_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) 
        { 
            //1代表左键,2代表右键 
            if (e.button == 1) 
            { 
                switch (Flag) 
                { 
                    case MapViewOperation.DrawGeometry: 
 
                        IPoint point = new PointClass(); 
                        point.X = e.mapX; 
                        point.Y = e.mapY; 
 
                        if (!bStartOrEndPoint) 
                        { 
                            IGeometry geomtry = UserGeometryGenerate.GetGeomtry(mapControlMain.ActiveView,point); 
                            ISpatialFilter spatialFilter = UserSpatialFilter.GetQueryFilter(geomtry); 
 
                            if (mapControlMain.LayerCount > 0) 
                            { 
                                UserSpatialFilter.GetSelection(mapControlMain.get_Layer(0), spatialFilter); 
                                mapControlMain.ActiveView.Refresh(); 
                            } 
                            bStartOrEndPoint = !bStartOrEndPoint; 
                        } 
 
                        break; 
                } 
            } 
            else if (e.button == 2) 
            {                 
            } 
        } 
 
        ///  
        /// 主图鼠标弹起事件 
        ///  
        ///  
        ///  
        private void axMapControlMain_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e) 
        { 
        }  
        #endregion 
 
        #region 鹰眼图控件事件 
        ///  
        /// 鹰眼点击动作触发主图的响应 
        ///  
        ///  
        ///  
        private void axMapControlOverview_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) 
        { 
            IPoint pntLocate = new PointClass(); 
            pntLocate.PutCoords(e.mapX, e.mapY); 
 
            mapControlMain.CenterAt(pntLocate); 
        }  
        #endregion 
 
         
 
        
    } 
}