ArcGIS Engine - 添加图例向导

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

		int Page = 1;
        private AxMapControl MapControl;
        private AxPageLayoutControl PageLayoutControl;
        private IFrameProperties m_FrameProperties;
        private stdole.IFontDisp pFont_title, pFont_labels;

        public AddLegend(AxMapControl mapControl, AxPageLayoutControl pageLayoutControl)
        {
            InitializeComponent();

            MapControl = mapControl;
            PageLayoutControl = pageLayoutControl;

            FrameElement pFrameElement = new FrameElementClass();
            IFrameElement pIFrameElement = pFrameElement as IFrameElement;
            m_FrameProperties = pIFrameElement as IFrameProperties;
        }

        //初始化
        private void AddLegend_Load(object sender, EventArgs e)
        {
            GetLayer(MapControl, listMapLayer);
            listMapLayer.SetSelected(0,true);
           
        }

        //创建图例
        private void CreateLegend(List<string> List_Layer, int Columns, string Title, ILegendFormat pLegendFormat, ITextSymbol symbolLabels)
        {
            IGraphicsContainer pGraphicsContainer = PageLayoutControl.GraphicsContainer;
            IMapFrame pMapFrame = pGraphicsContainer.FindFrame(MapControl.Map) as IMapFrame;

                if (pMapFrame != null)
                {
                    UID uID = new UIDClass();
                    uID.Value = "esriCarto.Legend";
                    IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(uID, null);
                    if (pMapSurroundFrame != null)
                    {
                        IMapSurround pMapSurround = pMapSurroundFrame.MapSurround;
                        ILegend2 pLegend = pMapSurround as ILegend2;
                        pLegend.ClearItems();

                        //设置标题
                        pLegend.Title = Title;
                        //设置样式
                        pLegend.Format = pLegendFormat;
                        //添加指定图层
                        for (int i = 0; i < List_Layer.Count; i++)
                        {
                            ILayer layer = GetLayerByName(List_Layer[i]);
                            ILegendItem pItem = SetItemStyle(layer, symbolLabels);
                            pLegend.AddItem(pItem);
                        }
                        //设置图例列数
                        pLegend.AdjustColumns(Columns);

                        //设置边框样式
                        IFrameProperties pFrameProperties = pMapSurroundFrame as IFrameProperties;
                        pFrameProperties.Border = m_FrameProperties.Border;
                        pFrameProperties.Background = m_FrameProperties.Background;
                        pFrameProperties.Shadow = m_FrameProperties.Shadow;

                        IEnvelope envelope = new EnvelopeClass();
                        envelope.PutCoords(16, 3, 20, 5);
                        IElement element = (IElement)pMapSurroundFrame;
                        element.Geometry = envelope;

                        pGraphicsContainer.AddElement(element, 0);
                        PageLayoutControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

                    }
                    else
                    {
                        MessageBox.Show("添加图例失败!", "提示", MessageBoxButtons.OK);
                    }
                }
                else
                {
                    MessageBox.Show("添加图例失败!", "提示", MessageBoxButtons.OK);
                }
        }

        //设置图例
        private ILegendItem SetItemStyle(ILayer layer, ITextSymbol symbolLabels)
        {
            ILegendItem pLegendItem = new HorizontalLegendItemClass();
            pLegendItem.Layer = layer;
            pLegendItem.ShowHeading = checkHeading.Checked;
            pLegendItem.ShowLayerName = checkLayerName.Checked;
            pLegendItem.ShowLabels = true;
            pLegendItem.ShowDescriptions = false;
            pLegendItem.LegendClassFormat.LabelSymbol = symbolLabels;

            return pLegendItem;
        }
        
		//遍历所有图层
        private void GetLayer(AxMapControl MapControl,ListBox listMapLayer)
        {
            for (int i = 0; i < MapControl.LayerCount; i++)
            {
                if (MapControl.get_Layer(i) is ICompositeLayer)
                {
                    ICompositeLayer pCompositeLayer = MapControl.get_Layer(i) as ICompositeLayer;
                    Togetlayer(pCompositeLayer, listMapLayer);
                }
                else
                {
                    listMapLayer.Items.Add(MapControl.get_Layer(i).Name);
                }
            }
        }

        //遍历图层组下的所有图层
       private ILayer getlayer(ICompositeLayer pCompositeLayer, ILayer pLayer, string IN_LayerName)
        {
            for (int i = 0; i < pCompositeLayer.Count; i++)
            {
                if (pCompositeLayer.get_Layer(i) is ICompositeLayer)
                {
                    ICompositeLayer CompositeLayer = pCompositeLayer.get_Layer(i) as ICompositeLayer;
                    pLayer = getlayer(CompositeLayer, pLayer, IN_LayerName);
                    if (pLayer != null)
                    {
                        break;
                    }
                }
                else if (pCompositeLayer.get_Layer(i).Name == IN_LayerName)
                {
                    pLayer = pCompositeLayer.get_Layer(i);
                    break;
                }
            }
            return pLayer;
        }

       /// <summary>
        /// 根据图层名获取图层
        /// </summary>
        private ILayer GetLayerByName(string IN_LayerName)
        {
            ILayer pLayer = null;
            for (int i = 0; i < MapControl.LayerCount; i++)
            {
                if (MapControl.get_Layer(i) is ICompositeLayer)
                {
                    ICompositeLayer pCompositeLayer = MapControl.get_Layer(i) as ICompositeLayer;
                    pLayer = getlayer(pCompositeLayer, pLayer, IN_LayerName);
                    if(pLayer != null) break;
                }
                else if(MapControl.get_Layer(i).Name == IN_LayerName)
                {
                    pLayer = MapControl.get_Layer(i);
                    break;
                }
            }
            return pLayer;
        }

        /// <summary>
        /// 遍历图层组下的所有图层
        /// </summary>
        private ILayer getlayer(ICompositeLayer pCompositeLayer, ILayer pLayer, string IN_LayerName)
        {
            for (int i = 0; i < pCompositeLayer.Count; i++)
            {
                if (pCompositeLayer.get_Layer(i) is ICompositeLayer)
                {
                    ICompositeLayer CompositeLayer = pCompositeLayer.get_Layer(i) as ICompositeLayer;
                    getlayer(CompositeLayer, pLayer, IN_LayerName);
                }
                else if (pCompositeLayer.get_Layer(i).Name == IN_LayerName)
                {
                    pLayer = pCompositeLayer.get_Layer(i);
                    break;
                }
            }
            return pLayer;
        }

        //上一步
        private void simpleLast_Click(object sender, EventArgs e)
        {
            Page --;
            visible(Page);
        }

        //下一步
        private void simpleNext_Click(object sender, EventArgs e)
        {
            if (listLegend.Items.Count != 0)
            {
                if (nColumns.Value != 0)
                {
                    if (textTitle.Text != "")
                    {
                        Page ++;
                        visible(Page);
                    }
                    else
                    {
                        MessageBox.Show("请输入图例名称!", "提示 ", MessageBoxButtons.OK);
                    }
                }
                else
                {
                    MessageBox.Show("请设置图例列数!", "提示 ", MessageBoxButtons.OK);
                }
            }
            else
            {
                MessageBox.Show("请添加图例项!","提示 ",MessageBoxButtons.OK);
            }
        }

        //完成
        private void simpleFinish_Click(object sender, EventArgs e)
        {
            //获取图层列表
            List<string> List_Layer = new List<string>();
            for (int i = 0; i < listLegend.Items.Count; i++ )
            {
                string[] a = new string[listLegend.Items.Count];
                a[i] = Convert.ToString(listLegend.Items[i]);
                List_Layer.Add(a[i]);
            }

            //获取列数
            int Columns = (int)nColumns.Value;

            //获取图例标题
            string Title = textTitle.Text;

            //获取图例标题样式
            ITextSymbol symbolTitle = new TextSymbolClass(); 
            if (pFont_title != null)
            {
                symbolTitle.Font = pFont_title;
            }
            symbolTitle.Color = Toolkit.toColor(cboTextColor.BackColor);

            //获取图例标签样式
            ITextSymbol symbolLabels = new TextSymbolClass();
            if (pFont_labels != null)
            {
                symbolLabels.Font = pFont_labels;
            }
            symbolLabels.Color = Toolkit.toColor(cboLabelsColor.BackColor);

            ILegendFormat pLegendFormat = new LegendFormatClass();
            //设置标题样式
            pLegendFormat.TitleSymbol = symbolTitle;

            if (IsNumbericA(textHeadingGap.Text) && IsNumbericA(textGroupGap.Text) && IsNumbericA(textHorizontalItemGap.Text) && IsNumbericA(textTextGap.Text) && 
                IsNumbericA(textLayerNameGap.Text) && IsNumbericA(textVerticalItemGap.Text) && IsNumbericA(textHorizontalPatchGap.Text))
            {
                //标题和图例项之间的垂直距离
                pLegendFormat.HeadingGap = Convert.ToDouble(textHeadingGap.Text);
                //图例项之间的垂直距离
                pLegendFormat.GroupGap = Convert.ToDouble(textGroupGap.Text);
                //图例项列之间的水平距离
                pLegendFormat.HorizontalItemGap = Convert.ToDouble(textHorizontalItemGap.Text);
                //图层名称和图形之间的垂直距离
                pLegendFormat.LayerNameGap = Convert.ToDouble(textLayerNameGap.Text);
                //标签和描述之间的水平距离
                pLegendFormat.TextGap = Convert.ToDouble(textTextGap.Text);
                //图例项目之间的垂直距离
                pLegendFormat.VerticalItemGap = Convert.ToDouble(textVerticalItemGap.Text);
                //图面和标注之间的水平距离
                pLegendFormat.HorizontalPatchGap = Convert.ToDouble(textHorizontalPatchGap.Text);

                CreateLegend(List_Layer, Columns, Title, pLegendFormat, symbolLabels);
                this.Close();
            }
            else
            {
                MessageBox.Show("请输入正确的数值!", "提示");
            }
            
        }

        private void simpleCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //显示控制
        private void visible(int Page)
        {
            if (Page == 1)
            {
                simpleLast.Enabled = false;
                simpleNext.Visible = true;
                simpleFinish.Visible = false;
                //第一页要素
                groupLayer.Visible = true;
                label.Visible = true;
                nColumns.Visible = true;
                checkHeading.Visible = true;
                checkLayerName.Visible = true;
                //第二页要素
                groupTitle.Visible = false;
                groupBorderStyle.Visible = false;
                groupLabels.Visible = false;
                //第三页要素
                groupGap.Visible = false;
            }
            if (Page == 2)
            {
                simpleLast.Enabled = true;
                simpleNext.Visible = true;
                simpleFinish.Visible = false;
                //第一页要素
                groupLayer.Visible = false;
                label.Visible = false;
                nColumns.Visible = false;
                checkHeading.Visible = false;
                checkLayerName.Visible = false;
                //第二页要素
                groupTitle.Visible = true;
                groupBorderStyle.Visible = true;
                groupLabels.Visible = true;
                //第三页要素
                groupGap.Visible = false;
            }
            if (Page == 3)
            {
                simpleLast.Enabled = true;
                simpleNext.Visible = false;
                simpleFinish.Visible = true;
                //第一页要素
                groupLayer.Visible = false;
                label.Visible = false;
                nColumns.Visible = false;
                checkHeading.Visible = false;
                checkLayerName.Visible = false;
                //第二页要素
                groupTitle.Visible = false;
                groupBorderStyle.Visible = false;
                groupLabels.Visible = false;
                //第三页要素
                groupGap.Visible = true;
            }
        }

        private void simpleAdd_Click(object sender, EventArgs e)
        {
            object Item = listMapLayer.SelectedItem;
            listLegend.Items.Add(Item);
            listLegend.SetSelected(0, true);
            listLegend_ValueChanged();
        }

        private void simpleAddAll_Click(object sender, EventArgs e)
        {
            listLegend.Items.AddRange(listMapLayer.Items);
            listLegend.SetSelected(0, true);
            listLegend_ValueChanged();
        }

        private void simpleRemove_Click(object sender, EventArgs e)
        {
            if (listLegend.Items.Count != 0)
            {
                object Item = listLegend.SelectedItem;
                listLegend.Items.Remove(Item);
                if (listLegend.Items.Count != 0)
                {
                    listLegend.SetSelected(0, true);
                }
                listLegend_ValueChanged();
            }
            
        }

        private void simpleRemoveAll_Click(object sender, EventArgs e)
        {
            if (listLegend.Items.Count != 0)
            {
                listLegend.Items.Clear();
                listLegend_ValueChanged();
            }
            
        }

        private void simpleUp_Click(object sender, EventArgs e)
        {
            object Item = listLegend.SelectedItem;
            int i = listLegend.SelectedIndex;
            if( i != 0)
            {
                listLegend.Items.Insert(i - 1, Item);
                listLegend.Items.RemoveAt(i + 1);
                listLegend.SetSelected(i - 1, true);
            }
        }

        private void simpleDown_Click(object sender, EventArgs e)
        {
            object Item = listLegend.SelectedItem;
            int i = listLegend.SelectedIndex;
            if (i != listLegend.Items.Count - 1)
            {
                listLegend.Items.Insert(i + 2, Item);
                listLegend.Items.RemoveAt(i);
                listLegend.SetSelected(i + 1, true);
            }
        }

        private void listLegend_ValueChanged()
        {
            if (listLegend.Items.Count != 0)
            {
                simpleRemove.Enabled = true;
                simpleRemoveAll.Enabled = true;
                simpleUp.Enabled = true;
                simpleDown.Enabled = true;
            }
            else
            {
                simpleRemove.Enabled = false;
                simpleRemoveAll.Enabled = false;
                simpleUp.Enabled = false;
                simpleDown.Enabled = false;
            }
        }

        private void cboTextColor_Click(object sender, EventArgs e)
        {
            cboTextColor.BackColor = chooseColor2(cboTextColor.BackColor.ToArgb());
        }

        private void cboLabelsColor_Click(object sender, EventArgs e)
        {
            cboLabelsColor.BackColor = chooseColor2(cboTextColor.BackColor.ToArgb());
        }

        private System.Drawing.Color chooseColor2(int color)
        {
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.AllowFullOpen = true;
            colorDialog.CustomColors = new int[] { color };
            colorDialog.ShowHelp = true;
            DialogResult result = colorDialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                return colorDialog.Color;
            }

            return System.Drawing.Color.FromArgb(color);
        }

        private void simpleTextStyle_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog = new FontDialog();
            if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                System.Drawing.Font selectFont = fontDialog.Font;
                pFont_title = ESRI.ArcGIS.ADF.COMSupport.OLE.GetIFontDispFromFont(selectFont) as stdole.IFontDisp;
                textBox2.Text = selectFont.Name + "  " + selectFont.Size.ToString();
            }
        }

        private void simpleLabelTextStyle_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog = new FontDialog();
            if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                System.Drawing.Font selectFont = fontDialog.Font;
                pFont_labels = ESRI.ArcGIS.ADF.COMSupport.OLE.GetIFontDispFromFont(selectFont) as stdole.IFontDisp;
                textBox3.Text = selectFont.Name + "  " + selectFont.Size.ToString();
            }
        }

        private void simpleBorder_Click(object sender, EventArgs e)
        {
            BorderForm pBorderForm = new BorderForm("Border");
            pBorderForm.ShowDialog();
            IBorder pBorder = pBorderForm.getFrameProperties().Border;
            if (pBorder != null)
            {
                pBorder.Gap = (double)numGap.Value;
                m_FrameProperties.Border = pBorder;
            }
        }

        private void simpleBackground_Click(object sender, EventArgs e)
        {
            BorderForm pBorderForm = new BorderForm("Background");
            pBorderForm.ShowDialog();
            m_FrameProperties.Background = pBorderForm.getFrameProperties().Background;
        }

        private void simpleShadow_Click(object sender, EventArgs e)
        {
            BorderForm pBorderForm = new BorderForm("Shadow");
            pBorderForm.ShowDialog();
            m_FrameProperties.Shadow = pBorderForm.getFrameProperties().Shadow;
        }

        #region 通用函数
        /// <summary>  
        /// 是否大于0的数字  
        /// </summary>  
        /// <param name="v"></param>  
        /// <returns></returns>  
        private bool IsNumbericA(string v)
        {
            return (this.IsFloatA(v));
        }
        /// <summary>  
        /// 是否正浮点数  
        /// </summary>  
        /// <param name="v"></param>  
        /// <returns></returns>  
        private bool IsFloatA(string v)
        {
            string pattern = @"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$";
            Regex reg = new Regex(pattern);
            return reg.IsMatch(v);
        }
        
        #endregion 



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王八八。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值