C#+AE实现类似Identify功能
2017-12-31 22:46
357 查看
转眼已经到2017年年末,再不发博文就要到下一年了^_^
这次做的是老师布置的作业,用C#+AE实现类似ArcMap中Identify的功能
最后做出来是用矩形框选择要素,点击treeview中的要素,该要素会在地图上闪烁,但只能对一个图层进行identify。
首先,建立显示属性信息的窗体(identifyform):
窗体identifyform中代码:
public partial class identifyform : Form
{
public IMapControlDefault m;
public identifyform()
{
InitializeComponent();
}
private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
IFeatureLayer pfeaturelayer = new FeatureLayerClass();
string ss = treeView1.SelectedNode.Parent.Text;
for (int i = 0; i < m.LayerCount; i++)
{
ILayer play = m.get_Layer(i);
if (play.Name == ss)
{
pfeaturelayer = m.get_Layer(i) as IFeatureLayer;
break;
}
}
IQueryFilter pqueryfilter = new QueryFilter();
string s = treeView1.SelectedNode.Text.ToString();
pqueryfilter.SubFields = "*";
pqueryfilter.WhereClause = "NAME='" + s + "'";
IFeatureCursor pfeaturecursor = pfeaturelayer.Search(pqueryfilter, true);
IFeature pfeature = pfeaturecursor.NextFeature();
IFields pfields = pfeaturelayer.FeatureClass.Fields;
listView1.Items.Clear();
while (pfeature != null)
{
for (int i = 0; i < pfields.FieldCount; i++)
{
ListViewItem list1 = new ListViewItem();
list1.Text = pfields.get_Field(i).Name;
int pindex2 = pfeaturecursor.FindField(pfields.get_Field(i).Name);
list1.SubItems.Add(pfeature.get_Value(pindex2).ToString());
listView1.Items.Add(list1);
}
m.FlashShape(pfeature.Shape, 3, 500, null);
pfeature = pfeaturecursor.NextFeature();
}
}
}
新建类(identifyicom)中代码:
class identifyicom:ICommand,ITool
{
IMapControlDefault mapp;
identifyform identform = new identifyform();
public string s1;
public string s2;
public int Bitmap
{
get { throw new NotImplementedException(); }
}
public string Caption
{
get { throw new NotImplementedException(); }
}
public string Category
{
get { throw new NotImplementedException(); }
}
public bool Checked
{
get { throw new NotImplementedException(); }
}
public bool Enabled
{
get { throw new NotImplementedException(); }
}
public int HelpContextID
{
get { throw new NotImplementedException(); }
}
public string HelpFile
{
get { throw new NotImplementedException(); }
}
public string Message
{
get { throw new NotImplementedException(); }
}
public string Name
{
get { throw new NotImplementedException(); }
}
public void OnClick()
{
}
public void OnCreate(object Hook)
{
mapp = Hook as IMapControlDefault;
}
public string Tooltip
{
get { throw new NotImplementedException(); }
}
public int Cursor
{
get;
set;
}
public bool Deactivate()
{
return true;
}
public bool OnContextMenu(int x, int y)
{
return false;
}
public void OnDblClick()
{
throw new NotImplementedException();
}
public void OnKeyDown(int keyCode, int shift)
{
throw new NotImplementedException();
}
public void OnKeyUp(int keyCode, int shift)
{
throw new NotImplementedException();
}
public void OnMouseDown(int button, int shift, int x, int y)
{
bool b1 = false;
foreach (Form f2 in System.Windows.Forms.Application.OpenForms)
{
if ("identifyform" == f2.Name)
{
f2.Activate();
b1 = true;
}
}
if (!b1)
{
identform = new identifyform();
identform.Show();
}
IGeometry pgeometry = mapp.TrackRectangle();
IEnvelope pEnvelope = pgeometry as IEnvelope;
ISelectionEnvironment pSelectionEnvironment = new SelectionEnvironmentClass();
IRgbColor prgbcolor = new RgbColor();
prgbcolor.Red = 0;
prgbcolor.Green = 0;
prgbcolor.Blue = 255;
pSelectionEnvironment.DefaultColor = prgbcolor;
mapp.ActiveView.FocusMap.SelectByShape(pEnvelope, pSelectionEnvironment, false);
mapp.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
s1 = pgeometry.Envelope.XMax.ToString();
s2 = pgeometry.Envelope.YMax.ToString();
identform.toolStripStatusLabel2.Text = "X:" + s1 + " " + "Y:" + s2;
ISpatialFilter psf = new SpatialFilter();
psf.SubFields = "*";
psf.Geometry = pgeometry;
psf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
for (int j = 0; j < mapp.LayerCount; j++)
{
IFeatureLayer pfl = mapp.get_Layer(j) as IFeatureLayer;
if (pfl == null) return;
IFeatureCursor pfc = pfl.Search((ISpatialFilter)psf, true);
IFeature pfeature = pfc.NextFeature();
identform.treeView1.Nodes.Clear();
TreeNode pnode1 = new TreeNode(pfl.Name);
identform.treeView1.Nodes.Add(pnode1);
identform.listView1.Clear();
identform.listView1.Columns.Add("Field", 180, HorizontalAlignment.Left);
identform.listView1.Columns.Add("Value", 180, HorizontalAlignment.Left);
IFields pfields = pfl.FeatureClass.Fields;
int pind = pfc.FindField("NAME");
if (pfeature != null)
{
for (int i = 0; i < pfields.FieldCount; i++)
{
ListViewItem list1 = new ListViewItem();
list1.Text = pfields.get_Field(i).Name;
int pindex = pfc.FindField(pfields.get_Field(i).Name);
list1.SubItems.Add(pfeature.get_Value(pindex).ToString());
identform.listView1.Items.Add(list1);
}
}
while (pfeature != null)
{
pnode1.Nodes.Add(pfeature.get_Value(pind).ToString());
pfeature = pfc.NextFeature();
}
identform.m = mapp;
mapp.ActiveView.Refresh();
}
}
public void OnMouseMove(int button, int shift, int x, int y)
{
}
public void OnMouseUp(int button, int shift, int x, int y)
{
}
public void Refresh(int hdc)
{
}
}
调用:
效果图如下:
这次做的是老师布置的作业,用C#+AE实现类似ArcMap中Identify的功能
最后做出来是用矩形框选择要素,点击treeview中的要素,该要素会在地图上闪烁,但只能对一个图层进行identify。
首先,建立显示属性信息的窗体(identifyform):
窗体identifyform中代码:
public partial class identifyform : Form
{
public IMapControlDefault m;
public identifyform()
{
InitializeComponent();
}
private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
IFeatureLayer pfeaturelayer = new FeatureLayerClass();
string ss = treeView1.SelectedNode.Parent.Text;
for (int i = 0; i < m.LayerCount; i++)
{
ILayer play = m.get_Layer(i);
if (play.Name == ss)
{
pfeaturelayer = m.get_Layer(i) as IFeatureLayer;
break;
}
}
IQueryFilter pqueryfilter = new QueryFilter();
string s = treeView1.SelectedNode.Text.ToString();
pqueryfilter.SubFields = "*";
pqueryfilter.WhereClause = "NAME='" + s + "'";
IFeatureCursor pfeaturecursor = pfeaturelayer.Search(pqueryfilter, true);
IFeature pfeature = pfeaturecursor.NextFeature();
IFields pfields = pfeaturelayer.FeatureClass.Fields;
listView1.Items.Clear();
while (pfeature != null)
{
for (int i = 0; i < pfields.FieldCount; i++)
{
ListViewItem list1 = new ListViewItem();
list1.Text = pfields.get_Field(i).Name;
int pindex2 = pfeaturecursor.FindField(pfields.get_Field(i).Name);
list1.SubItems.Add(pfeature.get_Value(pindex2).ToString());
listView1.Items.Add(list1);
}
m.FlashShape(pfeature.Shape, 3, 500, null);
pfeature = pfeaturecursor.NextFeature();
}
}
}
新建类(identifyicom)中代码:
class identifyicom:ICommand,ITool
{
IMapControlDefault mapp;
identifyform identform = new identifyform();
public string s1;
public string s2;
public int Bitmap
{
get { throw new NotImplementedException(); }
}
public string Caption
{
get { throw new NotImplementedException(); }
}
public string Category
{
get { throw new NotImplementedException(); }
}
public bool Checked
{
get { throw new NotImplementedException(); }
}
public bool Enabled
{
get { throw new NotImplementedException(); }
}
public int HelpContextID
{
get { throw new NotImplementedException(); }
}
public string HelpFile
{
get { throw new NotImplementedException(); }
}
public string Message
{
get { throw new NotImplementedException(); }
}
public string Name
{
get { throw new NotImplementedException(); }
}
public void OnClick()
{
}
public void OnCreate(object Hook)
{
mapp = Hook as IMapControlDefault;
}
public string Tooltip
{
get { throw new NotImplementedException(); }
}
public int Cursor
{
get;
set;
}
public bool Deactivate()
{
return true;
}
public bool OnContextMenu(int x, int y)
{
return false;
}
public void OnDblClick()
{
throw new NotImplementedException();
}
public void OnKeyDown(int keyCode, int shift)
{
throw new NotImplementedException();
}
public void OnKeyUp(int keyCode, int shift)
{
throw new NotImplementedException();
}
public void OnMouseDown(int button, int shift, int x, int y)
{
bool b1 = false;
foreach (Form f2 in System.Windows.Forms.Application.OpenForms)
{
if ("identifyform" == f2.Name)
{
f2.Activate();
b1 = true;
}
}
if (!b1)
{
identform = new identifyform();
identform.Show();
}
IGeometry pgeometry = mapp.TrackRectangle();
IEnvelope pEnvelope = pgeometry as IEnvelope;
ISelectionEnvironment pSelectionEnvironment = new SelectionEnvironmentClass();
IRgbColor prgbcolor = new RgbColor();
prgbcolor.Red = 0;
prgbcolor.Green = 0;
prgbcolor.Blue = 255;
pSelectionEnvironment.DefaultColor = prgbcolor;
mapp.ActiveView.FocusMap.SelectByShape(pEnvelope, pSelectionEnvironment, false);
mapp.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
s1 = pgeometry.Envelope.XMax.ToString();
s2 = pgeometry.Envelope.YMax.ToString();
identform.toolStripStatusLabel2.Text = "X:" + s1 + " " + "Y:" + s2;
ISpatialFilter psf = new SpatialFilter();
psf.SubFields = "*";
psf.Geometry = pgeometry;
psf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
for (int j = 0; j < mapp.LayerCount; j++)
{
IFeatureLayer pfl = mapp.get_Layer(j) as IFeatureLayer;
if (pfl == null) return;
IFeatureCursor pfc = pfl.Search((ISpatialFilter)psf, true);
IFeature pfeature = pfc.NextFeature();
identform.treeView1.Nodes.Clear();
TreeNode pnode1 = new TreeNode(pfl.Name);
identform.treeView1.Nodes.Add(pnode1);
identform.listView1.Clear();
identform.listView1.Columns.Add("Field", 180, HorizontalAlignment.Left);
identform.listView1.Columns.Add("Value", 180, HorizontalAlignment.Left);
IFields pfields = pfl.FeatureClass.Fields;
int pind = pfc.FindField("NAME");
if (pfeature != null)
{
for (int i = 0; i < pfields.FieldCount; i++)
{
ListViewItem list1 = new ListViewItem();
list1.Text = pfields.get_Field(i).Name;
int pindex = pfc.FindField(pfields.get_Field(i).Name);
list1.SubItems.Add(pfeature.get_Value(pindex).ToString());
identform.listView1.Items.Add(list1);
}
}
while (pfeature != null)
{
pnode1.Nodes.Add(pfeature.get_Value(pind).ToString());
pfeature = pfc.NextFeature();
}
identform.m = mapp;
mapp.ActiveView.Refresh();
}
}
public void OnMouseMove(int button, int shift, int x, int y)
{
}
public void OnMouseUp(int button, int shift, int x, int y)
{
}
public void Refresh(int hdc)
{
}
}
调用:
private void toolStripButton18_Click(object sender, EventArgs e) { ICommand pCmd = new identifyicom(); pCmd.OnCreate(axMapControl1.Object); ITool ptool = pCmd as ITool; axMapControl1.CurrentTool = ptool; }
效果图如下:
相关文章推荐
- C#+AE实现类似IDentify功能及对高亮显示相
- C#+AE实现类似IDentify功能及对高亮显示相关接口的总结
- C#+AE实现类似IDentify功能及对高亮显示相关接口的总结
- ASP.NET(C#)AJAX+JAVASCRIPT实现类似QQ,MSN窗口之功能(续)
- C#+AE 实现点击查询属性功能
- JAVA--实现类似C#输入功能的Console类
- javascript模拟实现类似c#下的hashtable的简单功能
- C#函数如何实现类似多个返回值的功能
- C#+AE 实现点击查询属性功能
- 用javaScript实现跟C#中类似的StringBuilder的功能
- JAVA--实现类似C#输入功能的Console类
- Android实现类似C#ComBox功能用AutoCompleteTextView实现
- Android实现类似C#ComBox功能用AutoCompleteTextView实现
- AE+C#实现缩放坐标比例尺长度面积量测输出图片等功能
- AE+C#实现鹰眼功能——(二)(带鼠标拖动)
- 在C#中实现类似C++函数指针数组的功能
- SQL实现类似C#的Split()函数的功能
- 在C++中实现类似C#的字符串格式功能
- C# DataGridView中实现类似ComboBox可编辑的下拉框功能