ArcEngine实现色带下拉框

效果如下:
在这里插入图片描述在这里插入图片描述
利用ArcEngine实现色带下拉框主要分两步。第一步:拖一个SymbologyControl到界面上,设置为不可见,通过SymbologyControl读取色带。第二步:重绘Combobox,将读取的色带转换为Image类型添加到Combobox中。
读取色带

        private void InitSymbologyControl()
        {
            this.axSymbologyControl1.LoadStyleFile(Application.StartupPath + "\\ESRI.ServerStyle");
            this.axSymbologyControl1.StyleClass = esriSymbologyStyleClass.esriStyleClassColorRamps;
            this.pSymbologyStyleClass = axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassColorRamps);
        }

添加至Combobox

        private void InitColorRampCombobox()
        {
            this.cmbColorRamp.DrawMode = DrawMode.OwnerDrawFixed;
            this.cmbColorRamp.DropDownStyle = ComboBoxStyle.DropDownList;
            for (int i = 0; i < pSymbologyStyleClass.ItemCount; i++)
            {
                IStyleGalleryItem pStyleGalleryItem = pSymbologyStyleClass.GetItem(i);
                IPictureDisp pPictureDisp = pSymbologyStyleClass.PreviewItem(pStyleGalleryItem, cmbColorRamp.Width, cmbColorRamp.Height);
                Image image = Image.FromHbitmap(new IntPtr(pPictureDisp.Handle));
                cmbColorRamp.Items.Add(image);
            }
            cmbColorRamp.SelectedIndex = 0;
        }
        
        private void cmbColorRamp_DrawItem_1(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            e.Graphics.DrawImage(cmbColorRamp.Items[e.Index] as Image, e.Bounds);
        }

完整代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using stdole;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using System.Collections;

namespace WindowsFormsApplication1
{
    public partial class Form1 : DevComponents.DotNetBar.Metro.MetroForm
    {
        private ISymbologyStyleClass pSymbologyStyleClass;
        private Dictionary<int, IColorRamp> colorRampDictionary;

        // 构造函数
        public Form1()
        {
            InitializeComponent();
            InitSymbologyControl();
            InitColorRampCombobox();
            InitDictionary();
        }

        // 初始化符号库
        private void InitSymbologyControl()
        {
            this.axSymbologyControl1.LoadStyleFile(Application.StartupPath + "\\ESRI.ServerStyle");
            this.axSymbologyControl1.StyleClass = esriSymbologyStyleClass.esriStyleClassColorRamps;
            this.pSymbologyStyleClass = axSymbologyControl1.GetStyleClass(esriSymbologyStyleClass.esriStyleClassColorRamps);
        }

        // 初始化色带下拉框
        private void InitColorRampCombobox()
        {
            this.cmbColorRamp.DrawMode = DrawMode.OwnerDrawFixed;
            this.cmbColorRamp.DropDownStyle = ComboBoxStyle.DropDownList;
            for (int i = 0; i < pSymbologyStyleClass.ItemCount; i++)
            {
                IStyleGalleryItem pStyleGalleryItem = pSymbologyStyleClass.GetItem(i);
                IPictureDisp pPictureDisp = pSymbologyStyleClass.PreviewItem(pStyleGalleryItem, cmbColorRamp.Width, cmbColorRamp.Height);
                Image image = Image.FromHbitmap(new IntPtr(pPictureDisp.Handle));
                cmbColorRamp.Items.Add(image);
            }
            cmbColorRamp.SelectedIndex = 0;
        }

        // 初始化字典
        private void InitDictionary()
        {
            this.colorRampDictionary = new Dictionary<int, IColorRamp>();
            for (int i = 0; i < pSymbologyStyleClass.ItemCount; i++)
            {
                IStyleGalleryItem pStyleGalleryItem = pSymbologyStyleClass.GetItem(i);
                IColorRamp pColorRamp = pStyleGalleryItem.Item as IColorRamp;
                colorRampDictionary.Add(i, pColorRamp);
            }
        }

        // 重绘下拉框条目
        private void cmbColorRamp_DrawItem_1(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            e.Graphics.DrawImage(cmbColorRamp.Items[e.Index] as Image, e.Bounds);
        }

        // 渲染
        private void btnRenderer_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFeatureLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);

            // 统计唯一值
            IDataStatistics pDataStatistics = new DataStatistics();
            pDataStatistics.Field = "name";
            pDataStatistics.Cursor = pFeatureCursor as ICursor;
            IEnumerator pEnumerator = pDataStatistics.UniqueValues;
            int uniqueValuesCount = pDataStatistics.UniqueValueCount;

            // 设置渲染字段
            IUniqueValueRenderer pUniqueValueRenderer = new UniqueValueRenderer();
            pUniqueValueRenderer.FieldCount = 1;
            pUniqueValueRenderer.set_Field(0, "name");

            // 添加唯一值
            IEnumColors pEnumColors = CreateColorRamp(uniqueValuesCount);
            while (pEnumerator.MoveNext())
            {
                ISymbol pSymbol = CreateSymbol(pFeatureClass.ShapeType,pEnumColors.Next());
                string p = pEnumerator.Current.ToString();
                pUniqueValueRenderer.AddValue(p, "", pSymbol);
            }

            // 刷XINDITU
            IGeoFeatureLayer pGeoFeatureLayer = pFeatureLayer as IGeoFeatureLayer;
            pGeoFeatureLayer.Renderer = pUniqueValueRenderer as IFeatureRenderer;
            axMapControl1.ActiveView.Refresh();
        }

        // 创建色带
        private IEnumColors CreateColorRamp(int size)
        {
            bool ok = false;
            IColorRamp pColorRamp = colorRampDictionary[cmbColorRamp.SelectedIndex];
            pColorRamp.Size = size;
            pColorRamp.CreateRamp(out ok);
            return pColorRamp.Colors;
        }

        // 创建符号
        private ISymbol CreateSymbol(esriGeometryType geomerryType, IColor pColor)
        {
            ISymbol pSymbol = null;
            if (geomerryType == esriGeometryType.esriGeometryPoint)
            {
                ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbol();
                pSimpleMarkerSymbol.Color = pColor;
                pSymbol = pSimpleMarkerSymbol as ISymbol;
            }
            else if (geomerryType == esriGeometryType.esriGeometryPolyline)
            {
                ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbol();
                pSimpleLineSymbol.Color = pColor;
                pSymbol = pSimpleLineSymbol as ISymbol;
            }
            else
            {
                ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbol();
                pSimpleFillSymbol.Color = pColor;
                pSymbol = pSimpleFillSymbol as ISymbol;
            }
            return pSymbol;
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值