对矢量图层的字段:进行增加、删除和查找及读取相应字段的属性值,鹰眼实现
// 添加字段
private void button5_Click(object sender, EventArgs e)
{
IFeatureLayer myLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer;
IFeatureClass myClass = myLayer.FeatureClass;
IFieldEdit field = new FieldClass();
field.Name_2 = "wwx";
field.Type_2 = esriFieldType.esriFieldTypeString;
field.Length_2 = 10;
myClass.AddField(field as IField);
MessageBox.Show("添加字段完成!");
}
// 删除字段
private void button6_Click(object sender, EventArgs e)
{
IFeatureLayer myLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer;
IFeatureClass myClass = myLayer.FeatureClass;
IFields fds = myClass.Fields;
IField fd = fds.get_Field(4);
myClass.DeleteField(fd);
MessageBox.Show("删除字段完成!");
}
// 字段查找
private void button2_Click(object sender, EventArgs e)
{
IFeatureLayer myLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer;
IFeatureClass myClass = myLayer.FeatureClass;
int index = myClass.FindField("KIND");
MessageBox.Show(index.ToString());
}
// 读取属性值
private void button1_Click(object sender, EventArgs e)
{
IFeatureLayer myLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer;
IFeatureClass myClass = myLayer.FeatureClass;
IFeature fea = myClass.GetFeature(6);
int index = myClass.FindField("NAME");
string name = Convert.ToString(fea.get_Value(index));
//byte[] temp = Encoding.GetEncoding("ISO8859-1").GetBytes(name);
// byte[] temp = Encoding.GetEncoding("GbK").GetBytes(name);
// 这里读取shp文件出现了中文乱码,不知道怎么回事
//name = Encoding.Default.GetString(temp);
MessageBox.Show(name);
}
鹰眼实现代码:
private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{
IEnvelope pEnv;
pEnv = e.newEnvelope as IEnvelope;
IGraphicsContainer graphicscontainer;
IActiveView activewer;
graphicscontainer = axMapControl2.Map as IGraphicsContainer;
activewer = graphicscontainer as IActiveView;
graphicscontainer.DeleteAllElements();
IElement plement;
plement = new RectangleElementClass();
plement.Geometry = pEnv;
IRgbColor rgbcol = new RgbColorClass();
rgbcol.RGB = 255;
rgbcol.Transparency = 255;
ILineSymbol poutline = new SimpleLineSymbolClass();
poutline.Width = 1;
poutline.Color = rgbcol;
IRgbColor pcolor = new RgbColorClass();
pcolor.RGB = 255;
pcolor.Transparency = 0;
IFillSymbol fillsym = new SimpleFillSymbolClass();
fillsym.Color = pcolor;
fillsym.Outline = poutline;
IFillShapeElement pfillshapeelement;
pfillshapeelement = plement as IFillShapeElement;
pfillshapeelement.Symbol = fillsym;
plement = pfillshapeelement as IElement;
graphicscontainer.AddElement(plement, 0);
activewer.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}