using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TextElement标注.Class;
using TextElement标注.Label;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Animation;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Controls;
using stdole;
using ESRI.ArcGIS.SystemUI;
namespace TextElement标注
{
public partial class Form1 : Form
{
private frmTextElement frmTextElement = null;//
private OperateMap m_OperateMap = null;
public string filepath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
public Form1()
{
InitializeComponent();
m_OperateMap = new OperateMap();
}
#region 设置颜色函数
/// <summary>
///RGB色彩模型
/// </summary>
/// <param name="intR"></param>
/// <param name="intG"></param>
/// <param name="intB"></param>
/// <returns></returns>
//输入RGB值,获得IRgbColor型值
public IRgbColor GetRgbColor(int intR, int intG, int intB)
{
IRgbColor pRgbColor = null;
if (intR < 0 || intR > 255 || intG < 0 || intG > 255 || intB < 0 || intB > 255)
{
return pRgbColor;
}
pRgbColor = new RgbColorClass();
pRgbColor.Red = intR;
pRgbColor.Green = intG;
pRgbColor.Blue = intB;
return pRgbColor;
}
/// <summary>
/// HSV色彩模型
/// </summary>
/// <param name="intH"></param>
/// <param name="intS"></param>
/// <param name="intV"></param>
/// <returns></returns>
//输入HSV值,获得IHsvColor型值
public IHsvColor GetHsvColor(int intH, int intS, int intV)
{
IHsvColor pHsvColor = null;
if (intH < 0 || intH > 360 || intS < 0 || intS > 100 || intV < 0 || intV > 100)
{
return pHsvColor;
}
pHsvColor = new HsvColorClass();
pHsvColor.Hue = intH;
pHsvColor.Saturation = intS;
pHsvColor.Value = intV;
return pHsvColor;
}
/// <summary>
/// 创建色带
/// </summary>
/// <returns></returns>
public IColorRamp CreateAlgorithmicColorRamp()
{
//创建一个新AlgorithmicColorRampClass对象
IAlgorithmicColorRamp pAlgColorRamp = new AlgorithmicColorRampClass();
IRgbColor pFromColor = new RgbColorClass();
IRgbColor pToColor = new RgbColorClass();
//创建起始颜色对象
pFromColor.Red = 255;
pFromColor.Green = 0;
pFromColor.Blue = 0;
//创建终止颜色对象
pToColor.Red = 0;
pToColor.Green = 255;
pToColor.Blue = 0;
//设置AlgorithmicColorRampClass的起止颜色属性
pAlgColorRamp.ToColor = pFromColor;
pAlgColorRamp.FromColor = pToColor;
//设置梯度类型
pAlgColorRamp.Algorithm = esriColorRampAlgorithm.esriCIELabAlgorithm;
//设置颜色带颜色数量
pAlgColorRamp.Size = 10;
//创建颜色带
bool bture = true;
pAlgColorRamp.CreateRamp(out bture);
return pAlgColorRamp;
}
#endregion
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
ICommand Cmd = new ControlsOpenDocCommandClass();
Cmd.OnCreate(mainMapControl.Object);
Cmd.OnClick();
}
private void txtElement_Click(object sender, EventArgs e)
{
try
{
if (frmTextElement == null || frmTextElement.IsDisposed)
{
frmTextElement = new frmTextElement();
frmTextElement.TextElement += new frmTextElement.TextElementLabelEventHandler(frmTextElement_TextElement);
}
frmTextElement.Map = mainMapControl.Map;
frmTextElement.InitUI();
frmTextElement.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void frmTextElement_TextElement(string sFeatClsName, string sFieldName)
{
IFeatureLayer pFeatLyr = m_OperateMap.GetFeatLyrByName(mainMapControl.Map, sFeatClsName);
TextElementLabel(pFeatLyr, sFieldName);
}
private void TextElementLabel(IFeatureLayer pFeatLyr, string sFieldName)
{
try
{
IMap pMap = mainMapControl.Map;
//获得图层所有要素
IFeatureClass pFeatureClass = pFeatLyr.FeatureClass;
IFeatureCursor pFeatCursor = pFeatureClass.Search(null, true);
IFeature pFeature = pFeatCursor.NextFeature();
while (pFeature != null)
{
IFields pFields = pFeature.Fields;
//找出标注字段的索引号
int index = pFields.FindField(sFieldName);
//得到要素的Envelope
IEnvelope pEnve = pFeature.Extent;
IPoint pPoint = new PointClass();
pPoint.PutCoords(pEnve.XMin + pEnve.Width / 2, pEnve.YMin + pEnve.Height / 2);
//新建字体对象
stdole.IFontDisp pFont;
pFont = new stdole.StdFontClass() as stdole.IFontDisp;
pFont.Name = "arial";
//产生一个文本符号
ITextSymbol pTextSymbol = new TextSymbolClass();
//设置文本符号的大小
pTextSymbol.Size = 20;
pTextSymbol.Font = pFont;
pTextSymbol.Color = m_OperateMap.GetRgbColor(255, 0, 0);
//产生一个文本对象
ITextElement pTextElement = new TextElementClass();
pTextElement.Text = pFeature.get_Value(index).ToString();
pTextElement.ScaleText = true;
pTextElement.Symbol = pTextSymbol;
IElement pElement = pTextElement as IElement;
pElement.Geometry = pPoint;
IActiveView pActiveView = pMap as IActiveView;
IGraphicsContainer pGraphicsContainer = pMap as IGraphicsContainer;
//添加元素
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
pPoint = null;
pElement = null;
pFeature = pFeatCursor.NextFeature();
}
}
catch (Exception ex)
{
}
}
int flag = 1;
private void txtSymbol_Click(object sender, EventArgs e)
{
flag = 0;
}
}
}
VS2010--AE10.2---------字段标注
最新推荐文章于 2022-05-07 15:17:35 发布