图层右键功能
图层右键功能中用到了contextMenuStrip控件,前文有提到,详细可参照上文,若有任何问题欢迎留言交流!
1、打开属性表
主窗体代码:
private void 打开属性表ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
AttributeForm attributeForm = new AttributeForm();
attributeForm.CurFeatureLayer = pTocFeatureLayer;
attributeForm.InitUI();
attributeForm.ShowDialog();
//显示右键菜单,并定义其相对控件的位置,正好在鼠标出显示
}
catch { MessageBox.Show("属性表为空"); }
}
属性表窗体代码:
public partial class AttributeForm : Form
{
public AttributeForm()
{
InitializeComponent();
}
private IFeatureLayer curFeatureLayer;
public IFeatureLayer CurFeatureLayer
{
get { return curFeatureLayer; }
set { curFeatureLayer = value; }
}
public void InitUI()
{
if (curFeatureLayer == null) return;
IFeature pFeature = null;
DataTable pFeatDT = new DataTable();
DataRow pDataRow = null;
DataColumn pDatacol = null;
IField pField = null;
//遍历图层的每一个字段
for (int i = 0; i < curFeatureLayer.FeatureClass.Fields.FieldCount; i++)
{
pDatacol = new DataColumn();
pField = curFeatureLayer.FeatureClass.Fields.get_Field(i);
pDatacol.ColumnName = pField.AliasName;
pDatacol.DataType = Type.GetType("System.Object");
pFeatDT.Columns.Add(pDatacol);
}
IFeatureCursor pFeatureCursor = curFeatureLayer.Search(null, true);
pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)
{
pDataRow = pFeatDT.NewRow();
for (int k = 0; k < pFeatDT.Columns.Count; k++)
{
pDataRow[k] = pFeature.get_Value(k);
}
pFeatDT.Rows.Add(pDataRow);
pFeature = pFeatureCursor.NextFeature();
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
dataGridView1.DataSource = pFeatDT;
}
}
2、缩放至图层
private void 缩放至图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (pTocFeatureLayer == null) return;
(axMapControl1.Map as IActiveView).Extent = pTocFeatureLayer.AreaOfInterest;
(axMapControl1.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
}
3、移除图层
private void 移除ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (pTocFeatureLayer == null) return;
DialogResult result = MessageBox.Show("是否移除[" + pTocFeatureLayer.Name + "]图层", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
axMapControl1.Map.DeleteLayer(pTocFeatureLayer);
axMapControl1.ActiveView.Refresh();
}
}
private void 移除所有图层ToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("是否移除所有图层", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
axMapControl1.Map.ClearLayers();
axTOCControl1.Update();//更新图层栏图表信息
axMapControl1.Refresh();
//axMapControl2.Refresh();
}
}
4、标注
基本思路:创建颜色→创建字体→创建符号→删除已有元素→遍历要素添加标注→刷新地图
private void 标注ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
// 创建颜色(字体颜色)
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 49;
pRgbColor.Green = 234;
pRgbColor.Blue = 26;
// 创建字体
IFontDisp pFontDisp = new StdFont() as IFontDisp;
pFontDisp.Bold = true;
pFontDisp.Name = "楷体";
pFontDisp.Size = 12;//字体大小
// 创建符号
ITextSymbol pTextSymbol = new TextSymbol();
pTextSymbol.Angle = 0;
pTextSymbol.Color = pRgbColor;
pTextSymbol.Font = pFontDisp;
// 删除已有文本元素
IActiveView pActiveView = axMapControl1.ActiveView;
IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
pGraphicsContainer.DeleteAllElements();
// 获取要素游标
IFeatureLayer pFeatureLayer = pTocFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
IFeature pFeature = pFeatureCursor.NextFeature();
// 遍历要素游标
int fieldIndex = pFeatureClass.Fields.FindField("F1");//标注字段
while (pFeature != null)
{
// 获取重心
IArea pArea = pFeature.ShapeCopy as IArea;
IPoint pPoint = pArea.Centroid;
// 创建文本元素
ITextElement pTextElement = new TextElement() as ITextElement;
pTextElement.Symbol = pTextSymbol;
pTextElement.Text = pFeature.get_Value(fieldIndex).ToString();
// 添加文本元素
IElement pElement = pTextElement as IElement;
pElement.Geometry = pPoint;
pGraphicsContainer.AddElement(pElement, 0);
pFeature = pFeatureCursor.NextFeature();
}
// 刷新地图
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
也是想出一个系列吧,在自己刚开始学习AE开发的时候苦于查找书籍代码不可用或者各博客描述不够详尽到我一个小白可以读懂,然后踩了各种各样的坑(虽然踩过的坑忘记了很多),其间很多代码的产生都是始于Copy,然后结合自己的查询资料和思考所得,还是希望自己能帮助到各位正在学习或使用的各位同仁吧。
扫码关注公众号