AE+VS开发颜色符号系统之单一值符号(一)

在主窗体中创建菜单

windows窗体中创建单一值符号化菜单

添加单一符号化类(ArcGISClass)

新建SingleSymbolCmd类  类型是Base Command

 

在类中的public override void OnClick()函数添加启动符号化窗口代码

完整代码如下:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;

namespace Visualization
{
    /// <summary>
    /// Command that works in ArcMap/Map/PageLayout
    /// </summary>
    [Guid("ae485cc9-ff2f-4b2f-9a84-2e68be4a1f8d")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("Visualization.SingleSymbolCmd")]
    public sealed class SingleSymbolCmd : BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Register(regKey);
            ControlsCommands.Register(regKey);
        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Unregister(regKey);
            ControlsCommands.Unregister(regKey);
        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper = null;
        public SingleSymbolCmd()
        {
            base.m_category = "Visualization"; 
            base.m_caption = "单一符号化";   
            base.m_message = "单一符号化";  
            base.m_toolTip = "单一符号化";  
            base.m_name = "";   

            try
            {
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            try
            {
                m_hookHelper = new HookHelperClass();
                m_hookHelper.Hook = hook;
                if (m_hookHelper.ActiveView == null)
                    m_hookHelper = null;
            }
            catch
            {
                m_hookHelper = null;
            }

            if (m_hookHelper == null)
                base.m_enabled = false;
            else
                base.m_enabled = true;

            // TODO:  Add other initialization code
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
           if (m_hookHelper == null) return;
            if (m_hookHelper.FocusMap.LayerCount > 0)
            {
                SingleSymbol singleSymbol = new SingleSymbol(m_hookHelper);
                singleSymbol.Show(m_hookHelper as System.Windows.Forms.IWin32Window);
            }
        }

        #endregion
    }
}

public override void OnClick()函数添加代码为

        public override void OnClick()
        {
           if (m_hookHelper == null) return;
            if (m_hookHelper.FocusMap.LayerCount > 0)
            {
                SingleSymbol singleSymbol = new SingleSymbol(m_hookHelper);
                singleSymbol.Show(m_hookHelper as System.Windows.Forms.IWin32Window);
            }

        }

 

启动单一符号化类(ArcGISClass)

在单一符号化菜单点击事件中添加启动单一符号化类(ArcGISClass)

        private void 单一符号化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ICommand command = new Visualization.SingleSymbolCmd();
            command.OnCreate(axMapControl1.Object);
            command.OnClick();
        }

通过单一值符号化类实现点击菜单启动符号化窗体

创建单一值符号化窗体

选择符号化图层,点击符号化按钮,随机配色。

代码如下:
 

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 ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DisplayUI;

namespace Visualization
{
    public partial class SingleSymbol : Form
    {
        IHookHelper m_hookHelper = null;
        IActiveView m_activeView = null;
        IMap m_map = null;

        IFeatureLayer layer2Symbolize = null;

        public SingleSymbol(IHookHelper hookHelper)
        {
            InitializeComponent();
            m_hookHelper = hookHelper;
            m_activeView = m_hookHelper.ActiveView;
            m_map = m_hookHelper.FocusMap;       
        }

        private void SingleSymbol_Load(object sender, EventArgs e)
        {
            CbxLayersAddItems();
        }

        private void cbxLayers2Symbolize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxLayers2Symbolize.SelectedItem != null)
            {
                string strLayer2Symbolize = cbxLayers2Symbolize.SelectedItem.ToString();
                layer2Symbolize = GetFeatureLayer(strLayer2Symbolize);
            }
        }

        private void CbxLayersAddItems()
        {
            if (GetLayers() == null) return;
            IEnumLayer layers = GetLayers();
            layers.Reset();
            ILayer layer = layers.Next();
            while (layer != null)
            {
                if (layer is IFeatureLayer)
                {
                    cbxLayers2Symbolize.Items.Add(layer.Name);
                }
                layer = layers.Next();
            }
        }

        #region "GetLayers"
        private IEnumLayer GetLayers()
        {
            UID uid = new UIDClass();
            uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";// IFeatureLayer
            //uid.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}";  // IGeoFeatureLayer
            //uid.Value = "{6CA416B1-E160-11D2-9F4E-00C04F6BC78E}";  // IDataLayer
            if (m_map.LayerCount != 0)
            {
                IEnumLayer layers = m_map.get_Layers(uid, true);
                return layers;
            }
            return null;
        }
        #endregion

        #region "GetFeatureLayer"
        private IFeatureLayer GetFeatureLayer(string layerName)
        {
            //get the layers from the maps
            if (GetLayers() == null) return null;
            IEnumLayer layers = GetLayers();
            layers.Reset();

            ILayer layer = null;
            while ((layer = layers.Next()) != null)
            {
                if (layer.Name == layerName)
                    return layer as IFeatureLayer;
            }
            return null;
        }

        #endregion

        private void btnSymbolize_Click(object sender, EventArgs e)
        {
            if (layer2Symbolize == null) return;
            ISymbol symbol = GetSymbolByControl(layer2Symbolize.FeatureClass.ShapeType);
            if (symbol == null) return;
            ISimpleRenderer pSimpleRenderer = new SimpleRendererClass();
            pSimpleRenderer.Symbol = symbol;
            pSimpleRenderer.Label = "单一符号化";
            IGeoFeatureLayer pGeoFeatureL = layer2Symbolize as IGeoFeatureLayer;
            pGeoFeatureL.Renderer = pSimpleRenderer as IFeatureRenderer;
            m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, m_activeView.Extent);
        }

        private ISymbol GetSymbolByControl(esriGeometryType geometryType)
        {
            GetSymbolByControl symbolForm = null;
            ISymbol symbol = null;
            switch (geometryType)
            {
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
                    symbolForm = new GetSymbolByControl(esriSymbologyStyleClass.esriStyleClassMarkerSymbols);
                    break;
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
                    symbolForm = new GetSymbolByControl(esriSymbologyStyleClass.esriStyleClassLineSymbols);
                    break;
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
                    symbolForm = new GetSymbolByControl(esriSymbologyStyleClass.esriStyleClassFillSymbols);
                    break;
                default:
                    break;
            }
            symbolForm.ShowDialog();
            if (symbolForm.m_styleGalleryItem != null)
                symbol = symbolForm.m_styleGalleryItem.Item as ISymbol;
            symbolForm.Dispose();
            return symbol;
        }

        private ISymbol GetSymbolBySymbolSelector(esriGeometryType geometryType)
        {
            ISymbolSelector pSymbolSelector = new SymbolSelectorClass();
            ISymbol symbol = null;
            switch (geometryType)
            {
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
                    symbol = new SimpleMarkerSymbolClass();
                    break;
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
                    symbol = new SimpleLineSymbolClass();
                    break;
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
                    symbol = new SimpleFillSymbolClass();
                    break;
                default:
                    break;
            }
            pSymbolSelector.AddSymbol(symbol);
            bool response = pSymbolSelector.SelectSymbol(0);
            if (response)
            {
                symbol = pSymbolSelector.GetSymbolAt(0);
                return symbol;
            }
            return null;
        }

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

本文中代码大多来自示例代码!欢迎交流学习,源代码中TOC会在设置符号系统后实时刷新,移植到其他项目后TOC控件中的图例不及时刷新,加入新数据才会刷新一下,尚未找到根本原因。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

空中旋转篮球

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

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

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

打赏作者

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

抵扣说明:

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

余额充值