1. 程式人生 > >C#+ AE實現地圖註記功能

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\";
            }
        }
    }

可以嘗試執行一下,看看效果。