效果如下:
利用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);}// 刷XINDITUIGeoFeatureLayer 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;}}
}