⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 运用ArcEngine9.2结合C#开发的gis系统
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesOleDB;

namespace WindowsApplication1
{
    public partial class mainform : Form
    {
        public ITable pPrimaryTable;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;//dakaiwenjian
        private string sFilePath;
        public System.Windows.Forms.TextBox txtMapDocument;
        private IMapDocument m_MapDocument;
        private System.Windows.Forms.SaveFileDialog saveFileDialog1;//baocunwenjian 
        bool LaKuang = false;//在拉框中用到的函数
        int LKChaXunLeiXing = 0;
        public mainform()
        {
            InitializeComponent();
        }
        //dakaiwenjian
          private void Open_shp(string filepath,string filename)
        {
            IWorkspaceFactory pwsf;
            IFeatureWorkspace pws;
            IFeatureClass pfc;
            IFeatureLayer layer; 

            pwsf=new ShapefileWorkspaceFactoryClass();
            pws=(IFeatureWorkspace)pwsf.OpenFromFile(filepath,0);
            pfc=pws.OpenFeatureClass(filename);
            layer=new FeatureLayerClass();
            layer.FeatureClass = pfc;
            layer.Name = filename;
            axMapControl1.AddLayer(layer);
            axMapControl2.AddLayer(layer);
            axMapControl1.Refresh();
            axMapControl2.Refresh();
        }
         //打开栅格文件,主要是Jpg型的 
        private void Open_raster(string path)
        {
            IRasterLayer prasterlayer = new RasterLayerClass();
            
            prasterlayer.CreateFromFilePath(path);
            axMapControl1.AddLayer(prasterlayer);
            axMapControl2.AddLayer(prasterlayer);
            axMapControl1.Refresh();
            axMapControl2.Refresh();
        }
        private void openOToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //打开地图文档
            openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Title = "打开地图文档";
            openFileDialog1.Filter = "矢量数据(*.shp)|*.shp|栅格数据(*.jpg)|*.jpg|地图文档 (*.mxd)|*.mxd";
            openFileDialog1.ShowDialog();
            sFilePath = openFileDialog1.FileName;
            if (sFilePath == "")
            {
                return;
            }
            string filename, filepath;
            filepath = Environment.CurrentDirectory;
            filename = openFileDialog1.FileName.Substring(filepath.Length + 1, openFileDialog1.FileName.Length - filepath.Length - 5);
            if (openFileDialog1.FileName.Contains(".shp") == true || openFileDialog1.FileName.Contains(".SHP") == true)
            {
                Open_shp(filepath, filename);
                int layercount = openFileDialog1.FileNames.Length;
                for (int i = 0; i < layercount; i++)
                    if (openFileDialog1.FileNames[i].Contains("xiaqu_region"))
                        DataBaseJion(i);//若打开的为"xiaqu_region"图层则连接数据库
                return;
            }
            else if (openFileDialog1.FileName.Contains(".jpg") == true)
            {
                Open_raster(sFilePath);
                return;
            }
            else
                //打开选中的题图文档              
            {
                //初始化一些实例
                m_MapDocument = new MapDocumentClass();
                txtMapDocument = new TextBox();
                //打开选中的地图文档
                m_MapDocument.Open(sFilePath, "");
                //显示
                axMapControl1.Map = m_MapDocument.get_Map(0);
                //辅助
                txtMapDocument.Text = m_MapDocument.DocumentFilename;

                //当主地图显示控件的地图更换时,鹰眼中的地图也跟随更换
                axMapControl2.Map = m_MapDocument.get_Map(0);
                axMapControl2.Extent = axMapControl2.FullExtent;

            }          
        }
        //画点
        private void drawpointToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ICommand pPoint;
            pPoint = new ControlsNewMarkerToolClass();
            pPoint.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = pPoint as ITool;

        }
        //画线
        private void drawlineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ICommand pLine;
            pLine = new ControlsNewLineToolClass();
            pLine.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = pLine as ITool;

        }
        //画面
        private void drawfaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ICommand pPolygon;
            pPolygon = new ControlsNewPolygonToolClass();
            pPolygon.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = pPolygon as ITool;
        }

        IGeoFeatureLayer pGeoFeatureL;
        //>连接数据库> 
        /// <summary>
        /// 连接数据库函数
        /// 参数为要连接属性值的图层索引
        /// </summary>
        /// <param name="num"></param>
        private void DataBaseJion(int num)
        {
            try
            {
                //IFeatureLayer pFeatureLayer;
                //pFeatureLayer = null;
               
                //ITable pForeignTable;
                //IDisplayTable pDisplayTable;
                //IMemoryRelationshipClassFactory pMemoryRelationshipCF;

                //IRelationshipClass pRelationshipClass;
                //IDisplayRelationshipClass pDisplayRelationshipC;


                //IWorkspaceFactory pWorkspaceFactory;
                //IWorkspace pWorkspace;
                //IPropertySet pPropertyset;
                //pPropertyset = new PropertySet();
                //pPropertyset.SetProperty("ConnectString", "Provider=SQLOLEDB.1;Data Source=DMT202042;Integrated Security=SSPI;Initial Catalog=xiuxiu");
                //pWorkspaceFactory = new OLEDBWorkspaceFactory();

                //pWorkspace = pWorkspaceFactory.Open(pPropertyset, 0);
                //IFeatureWorkspace objectworkspace;
                //objectworkspace = pWorkspace as IFeatureWorkspace;
                //pForeignTable = objectworkspace.OpenTable("xiuxiu");
                ////objectworkspace.
                ////int num;

                ////for (num = 0; num < axMapControl1.LayerCount; num++)
                ////{
                ////    if (axMapControl1.get_Layer(num).Name == "xiaqu_region")
                ////    {
                //pFeatureLayer = axMapControl1.get_Layer(num) as IFeatureLayer;
                ////        break;
                ////    }

                ////}
                //pDisplayTable = pFeatureLayer as IDisplayTable;
                //pPrimaryTable = pDisplayTable.DisplayTable;
                //pMemoryRelationshipCF = new MemoryRelationshipClassFactory();
                //pRelationshipClass = pMemoryRelationshipCF.Open("TabletoLayer", pPrimaryTable as IObjectClass, "ID", pForeignTable as IObjectClass, "ID", "forward", "backward", esriRelCardinality.esriRelCardinalityOneToOne);
                //pDisplayRelationshipC = pFeatureLayer as IDisplayRelationshipClass;

                //pDisplayRelationshipC.DisplayRelationshipClass(pRelationshipClass, esriJoinType.esriLeftInnerJoin);
                //MessageBox.Show("数据库连接成功!");
                
                pGeoFeatureL = axMapControl1.get_Layer(num) as IGeoFeatureLayer;
                ITable pTLayer;
                IDisplayTable pDisTable;
                pDisTable = (IDisplayTable)pGeoFeatureL;
                pTLayer = pDisTable.DisplayTable as ITable;

                IFeatureClass pFeatureClass = pGeoFeatureL.FeatureClass;

                ITable pGDBTable;
                IWorkspaceFactory2 oleDBWorkspaceFactory = (IWorkspaceFactory2)new ESRI.ArcGIS.DataSourcesOleDB.OLEDBWorkspaceFactoryClass();
                string connectionString = "CONNECTSTRING=Provider=SQLOLEDB.1;Initial Catalog=xiuxiu;Data Source=(local);uid=sa;pwd=sa;";

                IFeatureWorkspace pFeatureWS;
                pFeatureWS = oleDBWorkspaceFactory.OpenFromString(connectionString, 0) as IFeatureWorkspace;

                pGDBTable = pFeatureWS.OpenTable("xiuxiu");//为要关联的表明

                IMemoryRelationshipClassFactory pMenRelClassFac = new MemoryRelationshipClassFactoryClass();
                IRelationshipClass pRelClass;

                pRelClass = pMenRelClassFac.Open("join", pFeatureClass, "ID", (IObjectClass)pGDBTable, "ID", "forward", "backward", esriRelCardinality.esriRelCardinalityOneToOne);

                pGeoFeatureL = axMapControl1.get_Layer(num) as IGeoFeatureLayer;
                IDisplayRelationshipClass pDisRC = pGeoFeatureL as IDisplayRelationshipClass;
                pDisRC.DisplayRelationshipClass(pRelClass, esriJoinType.esriLeftInnerJoin);

                IDisplayTable tempDTable;
               // ITable connectedTable;
                tempDTable = pDisRC as IDisplayTable;
                pPrimaryTable = tempDTable.DisplayTable;
                MessageBox.Show("连接数据库成功");
            }
            catch (Exception e)
            {
                // 
                Console.WriteLine("{0} Exception caught.", e);
                MessageBox.Show("可能错误如下:\n1、IP不对!\n2、数据库名字错了;\n3、表名错了;\n4、图层名与数据库中的属性不符。", "数据库连接错误!");

            }
        }

        //baocunwenjian 
        private void SaveAs()
        {
            if (axMapControl1.Map.LayerCount == 0)
            {
                MessageBox.Show("当前没有打开任何地图文档或者图层", "小提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //打开另存为的对话框
            saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Title = "地图文档另存为";
            saveFileDialog1.Filter = "地图文档 (*.mxd)|*.mxd";
            saveFileDialog1.ShowDialog();
            //当选择取消是不做任何反应
            string sFilePath = saveFileDialog1.FileName;
            if (sFilePath == "")
            {
                return;
            }
            else
            {
                //另存

                //m_MapDocument.SaveAs(sFilePath, true, true);
                //继续显示原始地图
                //OpenDocument((sFilePath));
                // MessageBox.Show("另存成功", "小提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                /* IMxdContents pMxdContents = axMapControl1.Map as IMxdContents;
                 m_MapDocument = new MapDocumentClass();
                 //m_MapDocument.Open(axMapControl1.DocumentFilename, null);
                 IActiveView pActiveView = axMapControl1.Map as IActiveView;
                 m_MapDocument.ReplaceContents(pMxdContents);
                 m_MapDocument.SaveAs(sFilePath, true, true);
                 MessageBox.Show("地图保存成功!");*/

                IMxdContents pMxdC;
                pMxdC = axMapControl1.Map as IMxdContents;
                IMapDocument pMapDocument = new MapDocumentClass();
                pMapDocument.Open(axMapControl1.DocumentFilename, "");
                IActiveView pActiveView = axMapControl1.Map as IActiveView;
                pMapDocument.ReplaceContents(pMxdC);
                pMapDocument.SaveAs(sFilePath, true, true);
                MessageBox.Show("地图保存成功!");

            }
        }
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveAs();
        }

        private void saveasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveAs();
        }
        //shixianyingyan
        private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
        {
            //当主地图显示控件的地图更换时,鹰眼中的地图也跟随更换

            axMapControl2.LoadMxFile(axMapControl1.DocumentFilename);

            axMapControl2.Extent = axMapControl2.FullExtent;
        }

        private void axMapControl1_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            // 得到新范围

            IEnvelope pEnv = (IEnvelope)e.newEnvelope;


            IGraphicsContainer pGra = axMapControl2.Map as IGraphicsContainer;

            IActiveView pAv = pGra as IActiveView;

            //在绘制前,清除axMapControl2中的任何图形元素

            pGra.DeleteAllElements();


            IRectangleElement pRectangleEle = new RectangleElementClass();

            IElement pEle = pRectangleEle as IElement;

            pEle.Geometry = pEnv;


            //设置鹰眼图中的红线框

            IRgbColor pColor = new RgbColorClass();

            pColor.Red = 255;

            pColor.Green = 0;

            pColor.Blue = 0;

            pColor.Transparency = 255;

            //产生一个线符号对象

            ILineSymbol pOutline = new SimpleLineSymbolClass();

            pOutline.Width = 2;

            pOutline.Color = pColor;


            //设置颜色属性

            pColor = new RgbColorClass();

            pColor.Red = 255;

            pColor.Green = 0;

            pColor.Blue = 0;

            pColor.Transparency = 0;

            //设置填充符号的属性

            IFillSymbol pFillSymbol = new SimpleFillSymbolClass();

            pFillSymbol.Color = pColor;

            pFillSymbol.Outline = pOutline;


            IFillShapeElement pFillShapeEle = pEle as IFillShapeElement;

            pFillShapeEle.Symbol = pFillSymbol;

            pGra.AddElement((IElement)pFillShapeEle, 0);

            pAv.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

        }

        private void axMapControl2_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {
            IPoint pPt = new PointClass();

            pPt.PutCoords(e.mapX, e.mapY);

            //改变主控件的视图范围

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -