ArcEngine实现色带下拉框
生活随笔
收集整理的這篇文章主要介紹了
ArcEngine实现色带下拉框
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
效果如下:
利用ArcEngine實(shí)現(xiàn)色帶下拉框主要分兩步。第一步:拖一個SymbologyControl到界面上,設(shè)置為不可見,通過SymbologyControl讀取色帶。第二步:重繪Combobox,將讀取的色帶轉(zhuǎn)換為Image類型添加到Combobox中。
讀取色帶
添加至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;// 構(gòu)造函數(shù)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);// 統(tǒng)計(jì)唯一值IDataStatistics pDataStatistics = new DataStatistics();pDataStatistics.Field = "name";pDataStatistics.Cursor = pFeatureCursor as ICursor;IEnumerator pEnumerator = pDataStatistics.UniqueValues;int uniqueValuesCount = pDataStatistics.UniqueValueCount;// 設(shè)置渲染字段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();}// 創(chuàng)建色帶private IEnumColors CreateColorRamp(int size){bool ok = false;IColorRamp pColorRamp = colorRampDictionary[cmbColorRamp.SelectedIndex];pColorRamp.Size = size;pColorRamp.CreateRamp(out ok);return pColorRamp.Colors;}// 創(chuàng)建符號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;}} }總結(jié)
以上是生活随笔為你收集整理的ArcEngine实现色带下拉框的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python实现新版正方教务系统爬虫(二
- 下一篇: 五年级下学期班主任工作计划