博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bingil 组件LayerSelectControl lanix
阅读量:4980 次
发布时间:2019-06-12

本文共 12578 字,大约阅读时间需要 41 分钟。

目标:定制一个组件,选择不用的Layer,从地图AxMapControl上选择对象,并用DataGridView显示显性。

关键代码:
LayerSelectControl.Designer.cs
1 namespace LayerSelect 2 { 3     partial class LayerSelectControl 4     { 5         ///   6         /// Required designer variable. 7         ///  8         private System.ComponentModel.IContainer components = null; 9 10         ///  11         /// Clean up any resources being used.12         /// 13         /// true if managed resources should be disposed; otherwise, false.14         protected override void Dispose(bool disposing)15         {16             if (disposing && (components != null))17             {18                 components.Dispose();19             }20             base.Dispose(disposing);21         }22 23         #region Component Designer generated code24 25         ///  26         /// Required method for Designer support - do not modify 27         /// the contents of this method with the code editor.28         /// 29         private void InitializeComponent()30         {31             this.comboBox1 = new System.Windows.Forms.ComboBox();32             this.textBox1 = new System.Windows.Forms.TextBox();33             this.dataGridView1 = new System.Windows.Forms.DataGridView();34             ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();35             this.SuspendLayout();36             // 37             // comboBox138             // 39             this.comboBox1.Dock = System.Windows.Forms.DockStyle.Top;40             this.comboBox1.FormattingEnabled = true;41             this.comboBox1.Location = new System.Drawing.Point(0, 0);42             this.comboBox1.Name = "comboBox1";43             this.comboBox1.Size = new System.Drawing.Size(228, 20);44             this.comboBox1.TabIndex = 0;45             this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);46             // 47             // textBox148             // 49             this.textBox1.Dock = System.Windows.Forms.DockStyle.Bottom;50             this.textBox1.Location = new System.Drawing.Point(0, 200);51             this.textBox1.Name = "textBox1";52             this.textBox1.Size = new System.Drawing.Size(228, 21);53             this.textBox1.TabIndex = 1;54             // 55             // dataGridView156             // 57             this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;58             this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;59             this.dataGridView1.Location = new System.Drawing.Point(0, 20);60             this.dataGridView1.Name = "dataGridView1";61             this.dataGridView1.RowTemplate.Height = 23;62             this.dataGridView1.Size = new System.Drawing.Size(228, 180);63             this.dataGridView1.TabIndex = 2;64             // 65             // LayerSelectControl66             // 67             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);68             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;69             this.Controls.Add(this.dataGridView1);70             this.Controls.Add(this.textBox1);71             this.Controls.Add(this.comboBox1);72             this.Name = "LayerSelectControl";73             this.Size = new System.Drawing.Size(228, 221);74             ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();75             this.ResumeLayout(false);76             this.PerformLayout();77 78         }79 80         #endregion81 82         private System.Windows.Forms.ComboBox comboBox1;83         private System.Windows.Forms.TextBox textBox1;84         private System.Windows.Forms.DataGridView dataGridView1;85     }86 }

LayerSelectControl.cs

using System;using System.Collections;using System.Collections.Generic;using System.Data;using System.Windows.Forms;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.Geometry;using ESRI.ArcGIS.Geodatabase;using ESRI.ArcGIS.SystemUI;namespace LayerSelect{    public partial class LayerSelectControl : UserControl    {        AxMapControl _axMapControl;        IMap pMap;        public LayerSelectControl()        {            InitializeComponent();                    }        public void setBuddy(AxMapControl axMapControl)        {            _axMapControl = axMapControl;            pMap = _axMapControl.Map;            _axMapControl_OnMapReplaced(null, null);            _axMapControl.OnMapReplaced += new IMapControlEvents2_Ax_OnMapReplacedEventHandler(_axMapControl_OnMapReplaced);            _axMapControl.OnMouseDown += new IMapControlEvents2_Ax_OnMouseDownEventHandler(_axMapControl_OnMouseDown);            _axMapControl.OnSelectionChanged += new EventHandler(_axMapControl_OnSelectionChanged);        }        #region Event of axMapControl        void _axMapControl_OnSelectionChanged(object sender, EventArgs e)        {            this.textBox1.Text = this.comboBox1.Text + " --- " + pMap.SelectionCount.ToString() +                "features seleted---";            ILayer pLayer = pMap.Layer[comboBox1.SelectedIndex];            if(pLayer != null)            {                this.dataGridView1.DataSource = createDataTableWithSeletedData(pLayer, 100);            }                    }        void _axMapControl_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)        {            if (e.button == 1)            {                pMap.ClearSelection();                IPoint pPoint = new PointClass();                pPoint.PutCoords(e.mapX, e.mapY);                IEnvelope pEnvelope = _axMapControl.TrackRectangle();                pMap.SelectByShape(pEnvelope, null, false);                _axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, pEnvelope);            }        }        void _axMapControl_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)        {            this.comboBox1.Items.Clear();            pMap = _axMapControl.Map;            for (int i = 0; i < pMap.LayerCount; i++)            {                this.comboBox1.Items.Add(pMap.Layer[i].Name);            }        }                #endregion        #region Event of comboBox1        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)        {            IFeatureLayer pFeatureLayer;            for (int i = 0; i < pMap.LayerCount; i++)            {                pFeatureLayer = pMap.Layer[i] as IFeatureLayer;                if (i == comboBox1.SelectedIndex)                {                    pFeatureLayer.Selectable = true;                    DataTable pDataTable = createDataTableWithData(pMap.Layer[i],100);                    dataGridView1.DataSource = pDataTable;                }                else                {                    pFeatureLayer.Selectable = false;                }            }        }        #endregion        #region Function of Create DataTabel        DataTable createDataTableWithField(ILayer pLayer)        {            DataTable pDataTable = new DataTable();            ITable pTable = pLayer as ITable;            IField pField = null;            DataColumn pDataColumn;            for (int i = 0; i < pTable.Fields.FieldCount; i++)            {                pField = pTable.Fields.get_Field(i);                pDataColumn = new DataColumn(pField.Name);                if (pField.Name == pTable.OIDFieldName)                {                    pDataColumn.Unique = true;                }                pDataColumn.AllowDBNull = pField.IsNullable;                pDataColumn.Caption = pField.AliasName;                pDataColumn.DataType = System.Type.GetType(ParseFieldType(pField.Type));                pDataColumn.DefaultValue = pField.DefaultValue;                if (pField.VarType == 8)                {                    pDataColumn.MaxLength = pField.Length;                }                pDataTable.Columns.Add(pDataColumn);                pField = null;                pDataColumn = null;            }            return pDataTable;        }        DataTable createDataTableWithData(ILayer pLayer,int maxNumber)        {            DataTable pDataTable = createDataTableWithField(pLayer);            string shapeType = getShape(pLayer);            DataRow pDataRow = null;            ITable pTable = pLayer as ITable;            ICursor pCursor = pTable.Search(null, false);            IRow pRow = pCursor.NextRow();                        int n = 0;//the maxinum number of data show on the datagrids            while (pRow != null)            {                pDataRow = pDataTable.NewRow();                for (int i = 0; i < pRow.Fields.FieldCount; i++)                {                    if (pRow.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)                    {                        pDataRow[i] = shapeType;                    }                    else if (pRow.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeBlob)                    {                        pDataRow[i] = "Element";                    }                    else                    {                        pDataRow[i] = pRow.get_Value(i);                    }                }                pDataTable.Rows.Add(pDataRow);                pDataRow = null;                n++;                if (n == maxNumber)                {                    pRow = null;                }                else                {                    pRow = pCursor.NextRow();                }            }            return pDataTable;        }        private DataTable createDataTableWithSeletedData(ILayer pLayer, int maxNumber)        {            DataTable pDataTable = createDataTableWithField(pLayer);             string shapeType = getShape(pLayer);            ILayer rLayer = getSelectedLayer(pMap, this.comboBox1.SelectedIndex);            return createDataTableWithData(rLayer, maxNumber);        }        #endregion        private static string ParseFieldType(esriFieldType fieldType)        {            switch (fieldType)            {                case esriFieldType.esriFieldTypeBlob:                    return "System.String";                case esriFieldType.esriFieldTypeDate:                    return "System.DateTime";                case esriFieldType.esriFieldTypeDouble:                    return "System.Double";                case esriFieldType.esriFieldTypeGeometry:                    return "System.String";                case esriFieldType.esriFieldTypeGlobalID:                    return "System.String";                case esriFieldType.esriFieldTypeGUID:                case esriFieldType.esriFieldTypeInteger:                    return "System.Int32";                case esriFieldType.esriFieldTypeOID:                    return "System.String";                case esriFieldType.esriFieldTypeRaster:                    return "System.String";                case esriFieldType.esriFieldTypeSingle:                    return "System.Single";                case esriFieldType.esriFieldTypeSmallInteger:                    return "System.Int32";                case esriFieldType.esriFieldTypeString:                    return "System.String";                case esriFieldType.esriFieldTypeXML:                    return "System.String";                default :                    return "System.String";            }        }        private string getShape(ILayer pLayer)        {            IFeatureLayer pFeatLyr = pLayer as IFeatureLayer;            switch (pFeatLyr.FeatureClass.ShapeType)            {                case esriGeometryType.esriGeometryPoint:                    return "Point";                case esriGeometryType.esriGeometryPolyline:                    return "Polyline";                case esriGeometryType.esriGeometryPolygon:                    return "Polygon";                default:                    return "";            }        }        private ILayer getSelectedLayer(IMap pMap,int index)        {            IFeatureLayer pnewfeat;            IFeatureLayer pFeatLyr;            pFeatLyr = pMap.get_Layer(index) as IFeatureLayer;            IFeatureSelection pFeatSel = pMap.FeatureSelection as IFeatureSelection;            IFeatureLayerDefinition pFeatLyrDef = pFeatLyr as IFeatureLayerDefinition;            pnewfeat = pFeatLyrDef.CreateSelectionLayer("new" + pFeatLyr.Name, true, "", "");            ILayer rLayer = pnewfeat as ILayer;            return rLayer;        }    }}

用法:使用public void setBuddy(AxMapControl axMapControl)绑定对象即可。

效果图片:

转载于:https://www.cnblogs.com/lanix/archive/2012/06/01/2531298.html

你可能感兴趣的文章
ADO.NET中调用存储过程
查看>>
开发者 发展 2 码路指南
查看>>
830. Positions of Large Groups
查看>>
Bootstrap_网格系统
查看>>
java学习——异常处理
查看>>
实验10: RIP
查看>>
ArcGIS Server 10.1发布要素服务(FeatureLayer server)时遇到的数据库注册问题
查看>>
java 动手动脑
查看>>
手动删除数据库表,并注释创建的model后,同步数据库命令操作
查看>>
聊一聊JS的原型链之高级篇
查看>>
Ubuntu 11.10 禁用关机提示
查看>>
ListView实现点击事件以及总结
查看>>
设计模式使用方法总结
查看>>
ThreadLocal的应用与实现原理
查看>>
Ubuntu用sudo apt-get update出错:E: Problem executing scripts APT::Update::Post-Invoke-Success
查看>>
异常处理与内建模块
查看>>
Linux系统下搭建FTP/SFTP服务器
查看>>
获取手机信息(UIDevice、NSBundle、NSLocale)
查看>>
函数sprintf真好用
查看>>
网络欺诈防范
查看>>