C#+ AE实现地图注记功能
基于Arcgis Engine的二次开发,需要地图注记功能。简单讲注记是以文字的形式将要素图层的属性标注出来。
首先,窗体设计如下:
其次,实现的主要代码如下:
public partial class FormMapAnnotation : Form
{
AxMapControl MapControl;
string pLayerName;//图层名
IStyleGalleryItem ISGI;
private Class.RelativePath Path;//文件路径
public FormMapAnnotation(AxMapControl pMapControl)
{
MapControl = pMapControl;
InitializeComponent();
}
private void FormMapAnnotation_Load(object sender, EventArgs e)
{
for (int i = 0; i < MapControl.LayerCount; i++)
{
cmbLayer.Items.Add(MapControl.get_Layer(i).Name);
}
//获取系统已安装的字体
System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
for (int i = 0; i < fonts.Families.Length; i++)
{
cmbFont.Items.Add(fonts.Families[i].Name);
}
cmbLayer.Text = cmbLayer.Items[0].ToString();
Path = new Class.RelativePath();
string StyleFilePath = Path.stylePath + "ESRI.ServerStyle";
this.axSymbologyControl1.LoadStyleFile(StyleFilePath);
this.axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassTextSymbols).Update();
this.axSymbologyControl1.StyleClass = esriSymbologyStyleClass.esriStyleClassTextSymbols;
this.axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassTextSymbols).SelectItem(0);
}
private void axSymbologyControl1_OnItemSelected(object sender, ISymbologyControlEvents_OnItemSelectedEvent e)
{
ISGI = e.styleGalleryItem as IStyleGalleryItem;
ITextSymbol ITS = new TextSymbolClass();
ITS = ISGI.Item as ITextSymbol;
cmbFont.Text = ITS.Font.Name;
txtSize.Text = ITS.Font.Size.ToString();
checkBox1.Checked = ITS.Font.Bold;
checkBox2.Checked = ITS.Font.Italic;
checkBox3.Checked = ITS.Font.Underline;
btnColor.BackColor = ColorTranslator.FromOle(ITS.Color.RGB);
}
private void cmbLayer_SelectedIndexChanged(object sender, EventArgs e)
{
cmbField.Items.Clear();
pLayerName = cmbLayer.Items[cmbLayer.SelectedIndex].ToString();
ILayer pLayer = getFeatureLayerByName(MapControl.Map, pLayerName);
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
for (int i = 0; i < pFeatureClass.Fields.FieldCount; i++)
{
cmbField.Items.Add(pFeatureClass.Fields.get_Field(i).Name);
}
cmbField.Text = cmbField.Items[0].ToString();
}
private ILayer getFeatureLayerByName(IMap map, string str)
{
IFeatureLayer pFeatureLayer = null;
for (int i = 0; i < map.LayerCount; i++)
{
if (str == map.get_Layer(i).Name)
{
pFeatureLayer = map.get_Layer(i) as IFeatureLayer;
}
}
return pFeatureLayer;
}
private void btnOk_Click(object sender, EventArgs e)
{
Refresh();
IColor pColor = ColorToIColor(btnColor.BackColor);
ITextSymbol ITS = new TextSymbolClass();
ITS = ISGI.Item as ITextSymbol;
ITS.Color = pColor;
stdole.StdFont pFont = new stdole.StdFontClass();
pFont.Bold = checkBox1.Checked;
pFont.Italic = checkBox2.Checked;
pFont.Underline = checkBox3.Checked;
pFont.Size = Decimal.Parse(txtSize.Text);
pFont.Name = cmbFont.Text;
ITS.Font = pFont as IFontDisp;
Annotation a = new Annotation(MapControl);
a.Anno(cmbField.Text, cmbLayer.Text, ITS);
}
//Color转ArcEngine的IColor
public IColor ColorToIColor(Color color)
{
IColor pColor = new RgbColorClass();
pColor.RGB = color.B * 65536 + color.G * 256 + color.R;
return pColor;
}
private void btnColor_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() != DialogResult.OK)
return;
btnColor.BackColor = colorDialog1.Color;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
程序中路径代码如下:
class RelativePath
{
String RootPath;
public RelativePath()
{
RootPath = Application.StartupPath;
RootPath = RootPath.Substring(0,RootPath.Length-9);//从bin下面的Debug开始算起
}
public String stylePath
{
get
{
return RootPath + @"Styles\";
}
}
}
可以尝试运行一下,看看效果。