需求:实现和GIS桌面端中Identify的类似功能,鼠标滑动的时候可以显示鼠标所在位置的要素的指定字段的值.。
主要操作流程:
①先打开一个对话框,用于选择需要显示的图层和字段名
②点击确定之后,在mapControl上鼠标滑动的时候利用axMapControl的showTips功能实现实时显示,相对于Identify,取消的点击查看属性,和只能查看所有属性的弊端。
主窗体代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; namespace MapChuanzhi { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public int s_flag = 0; private void button2_Click(object sender, EventArgs e) { IMap pMap = this.axMapControl1.Map; Form2 frm2 = new Form2(pMap); frm2.formDelegate += new FormDelegate(Display); frm2.StartPosition = FormStartPosition.CenterScreen; frm2.ShowDialog(); s_flag = 1; } string LayerName = ""; List<string> FieldList = new List<string>(); public void Display(List<string> lc) { LayerName = lc[0].ToString(); FieldList.Clear(); for (int j = 1; j < lc.Count; j++) { FieldList.Add(lc[j].ToString()); } } private void axMapControl1_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e) { switch (s_flag) { case 1: showtips(FieldList, e); break; default: break; } } private void showtips(List<string> index, IMapControlEvents2_OnMouseMoveEvent e) { StringBuilder sb = new StringBuilder(); string headd = string.Format("{0}:{1}", "图层", LayerName); sb.AppendLine(headd); sb.AppendLine( "————————"); for (int i = 0; i < this.axMapControl1.Map.LayerCount; i++) { if (axMapControl1.get_Layer(i).Name.ToString().Equals(LayerName)) { for (int j = 0; j < index.Count; j++) { IFeatureLayer pFLayer = axMapControl1.Map.get_Layer(i) as IFeatureLayer; pFLayer.DisplayField = index[j].ToString(); pFLayer.ShowTips = true; string Text = pFLayer.get_TipText(e.mapX, e.mapY, axMapControl1.ActiveView.FullExtent.Width / 1000); if (string.IsNullOrWhiteSpace(Text)) { break; } sb.AppendLine(string.Format("{0}:{1}",index[j].ToString(),Text)); } if (sb.Length == headd.Length + "————————".Length + 4) sb.Clear(); toolTip1.SetToolTip(axMapControl1, sb.ToString()); } } } } }
属性窗口代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Carto; namespace MapChuanzhi { public delegate void FormDelegate(List<string> FormMessage); public partial class Form2 : Form { private ESRI.ArcGIS.Carto.IMap pMap; public Form2( ESRI.ArcGIS.Carto.IMap pMap) { InitializeComponent(); // TODO: Complete member initialization this.pMap = pMap; } private void Form2_Load(object sender, EventArgs e) { for (int i = 0; i < pMap.LayerCount; i++) { this.comboBox1.Items.Add(pMap.get_Layer(i).Name.ToString()); } } string selectItems; private void comboBox1_SelectedValueChanged(object sender, EventArgs e) { selectItems=comboBox1.SelectedItem.ToString(); for (int i = 0; i < pMap.LayerCount; i++) { if (pMap.get_Layer(i).Name.ToString() == selectItems) { IFeatureLayer pFLayer = pMap.get_Layer(i) as IFeatureLayer; IFeatureClass pFClass = pFLayer.FeatureClass; this.checkedListBox1.Items.Clear(); for (int j = 0; j < pFClass.Fields.FieldCount; j++) { this.checkedListBox1.Items.Add(pFClass.Fields.get_Field(j).Name); } try { checkedListBox1.Items.Remove("Shape"); } catch (Exception) { throw; } } } } /***************窗体委托传值*********************/ public FormDelegate formDelegate; private void button1_Click(object sender, EventArgs e) { if (this.checkedListBox1.CheckedItems == null || this.comboBox1.SelectedItem== null) return; List<string> cz = new List<string>(); cz.Add(comboBox1.SelectedItem.ToString()); for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) { cz.Add(checkedListBox1.CheckedItems[i].ToString()); } try { cz.Remove("Shape"); } catch { } formDelegate(cz); this.Dispose(); } private void button2_Click(object sender, EventArgs e) { if (this.button2.Text == "全选") { for (int i = 0; i < this.checkedListBox1.Items.Count; i++) { this.checkedListBox1.SetItemChecked(i, true); this.button2.Text = "取消全选"; } } else { for (int i = 0; i < this.checkedListBox1.Items.Count; i++) { this.checkedListBox1.SetItemChecked(i, false); this.button2.Text = "全选"; } } } } }