ArcObjects Library Reference  

FeatureLayerSymbology

About the Implementing a property page for an ArcGIS Engine application Sample

[C#]

FeatureLayerSymbology.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Messaging;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;

namespace SymbolSelector
{
  /// <summary>
  /// Summary description for FeatureLayerSymbology.
  /// </summary>
  [Guid("256bd27b-6e24-4cf5-bc5b-46ea93dc952a")]
  [ClassInterface(ClassInterfaceType.None)]
  [ProgId("SymbolSelector.FeatureLayerSymbology")]
  public sealed class FeatureLayerSymbology : 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);
      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);
      ControlsCommands.Unregister(regKey);

    }

    #endregion
    #endregion

    private IHookHelper m_pHookHelper = null;

    public FeatureLayerSymbology()
    {
      base.m_category = ".NET Samples";
      base.m_caption = "FeatureLayer symbol properties";
      base.m_message = "Show FeatureLayer symbol properties";
      base.m_toolTip = "Show FeatureLayer symbol properties";
      base.m_name = base.m_category + "_" + base.m_caption;

      try
      {
        //
        // TODO: change bitmap name if necessary
        //
        string bitmapResourceName = GetType().Name + ".bmp";
        base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
      }
    }

    #region Overriden 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 (m_pHookHelper == null)
        m_pHookHelper = new HookHelperClass();

      m_pHookHelper.Hook = hook;
    }

    /// <summary>
    /// Occurs when this command is clicked
    /// </summary>
    public override void OnClick()
    {
      try
      {
        IMapControl4 mapControl = null;

        if (m_pHookHelper.Hook is IMapControl4)
          mapControl = (IMapControl4)m_pHookHelper.Hook;
        else if (m_pHookHelper.Hook is IToolbarControl2)
        {
          IToolbarControl2 toolbarControl = (IToolbarControl2)m_pHookHelper.Hook;
          mapControl = (IMapControl4)toolbarControl.Buddy;
        }

        if (null == mapControl.CustomProperty || !(mapControl.CustomProperty is IFeatureLayer))
          return;

        IFeatureLayer featureLayer = (IFeatureLayer)mapControl.CustomProperty;


        //Launch the layer's properties
        Type typ;
        object obj;
        Guid[] g;

        // METHOD 1: Instantiating a COM object and displaying its property pages
        // ONLY WORKS ON TRUE COM OBJECTS!  .NET objects that have rolled their own
        // ISpecifyPropertyPages implementation will error out when you try to cast
        // the instantiated object to your own ISpecifyPropertyPages implementation.

        // Get the typeinfo for the ActiveX common dialog control
        typ = Type.GetTypeFromProgID("MSComDlg.CommonDialog");

        // Create an instance of the control.  We pass it to the property frame function
        // so the property pages have an object from which to get current settings and apply
        // new settings.
        obj = Activator.CreateInstance(typ);
        // This handy function calls IPersistStreamInit->New on COM objects to initialize them
        ActiveXMessageFormatter.InitStreamedObject(obj);

        // Get the property pages for the control using the direct CAUUID method
        // This only works for true COM objects and I demonstrate it here only
        // to show how it is done.  Use the static method
        // PropertyPage.GetPagesForType() method for real-world use.
        ISpecifyPropertyPages pag = (ISpecifyPropertyPages)obj;
        CAUUID cau = new CAUUID(0);
        pag.GetPages(ref cau);
        g = cau.GetPages();

        // Instantiating a .NET object and displaying its property pages
        // WORKS ON ALL OBJECTS, .NET or COM    

        // Create an instance of the .NET control, MyUserControl
        typ = Type.GetTypeFromProgID("SymbolSelector.PropertySheet");

        // Retrieve the pages for the control
        g = PropertyPage.GetPagesForType(typ);

        // Create an instance of the control that we can give to the property pages
        obj = Activator.CreateInstance(typ);

        ((SymbolSelector.PropertySheet)obj).OnFeatureLayerRendererChanged += new FeatureLayerRendererChanged(OnFeatureLayerRendererChanged);

        //add the yahoo layer to the property-sheet control
        ((SymbolSelector.PropertySheet)obj).FeatureLayer = featureLayer;

        // Display the OLE Property page for the control
        object[] items = new object[] { obj };

        PropertyPage.CreatePropertyFrame(IntPtr.Zero, 500, 500, "FeatureLayer Symbology", items, g);
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message);
      }
    }

    void OnFeatureLayerRendererChanged(object sender, EventArgs args)
    {
      m_pHookHelper.ActiveView.ContentsChanged();

      //Refresh the display
      m_pHookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, m_pHookHelper.ActiveView.ScreenDisplay.DisplayTransformation.FittedBounds);
      m_pHookHelper.ActiveView.ScreenDisplay.UpdateWindow();
    }

    #endregion
  }
}

[Visual Basic .NET]

FeatureLayerSymbology.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Messaging
Imports System.Windows.Forms
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Carto

Namespace SymbolSelector
  ''' <summary>
  ''' Summary description for FeatureLayerSymbology.
  ''' </summary>
  <Guid("256bd27b-6e24-4cf5-bc5b-46ea93dc952a"), ClassInterface(ClassInterfaceType.None), ProgId("SymbolSelector.FeatureLayerSymbology")> _
  Public NotInheritable Class FeatureLayerSymbology : Inherits BaseCommand
	#Region "COM Registration Function(s)"
	<ComRegisterFunction(), ComVisible(False)> _
	Private Shared Sub RegisterFunction(ByVal registerType As Type)
	  ' Required for ArcGIS Component Category Registrar support
	  ArcGISCategoryRegistration(registerType)

	  '
	  ' TODO: Add any COM registration code here
	  '
	End Sub

	<ComUnregisterFunction(), ComVisible(False)> _
	Private Shared Sub UnregisterFunction(ByVal registerType As Type)
	  ' Required for ArcGIS Component Category Registrar support
	  ArcGISCategoryUnregistration(registerType)

	  '
	  ' TODO: Add any COM unregistration code here
	  '
	End Sub

	#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 Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
	  Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
	  ControlsCommands.Register(regKey)

	End Sub
	''' <summary>
	''' Required method for ArcGIS Component Category unregistration -
	''' Do not modify the contents of this method with the code editor.
	''' </summary>
	Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
	  Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
	  ControlsCommands.Unregister(regKey)

	End Sub

	#End Region
	#End Region

	Private m_pHookHelper As IHookHelper = Nothing

	Public Sub New()
	  MyBase.m_category = ".NET Samples"
	  MyBase.m_caption = "FeatureLayer symbol properties"
	  MyBase.m_message = "Show FeatureLayer symbol properties"
	  MyBase.m_toolTip = "Show FeatureLayer symbol properties"
	  MyBase.m_name = MyBase.m_category & "_" & MyBase.m_caption

	  Try
        MyBase.m_bitmap = New Bitmap(Me.GetType().Assembly.GetManifestResourceStream(Me.GetType().Name & ".bmp"))
      Catch ex As Exception
        System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
      End Try
	End Sub

#Region "Overridden Class Methods"

        ''' <summary>
        ''' Occurs when this command is created
        ''' </summary>
        ''' <param name="hook">Instance of the application</param>
        Public Overrides Sub OnCreate(ByVal hook As Object)
            If m_pHookHelper Is Nothing Then
                m_pHookHelper = New HookHelperClass()
            End If

            m_pHookHelper.Hook = hook
        End Sub

        ''' <summary>
        ''' Occurs when this command is clicked
        ''' </summary>
        Public Overrides Sub OnClick()
            Try
                Dim mapControl As IMapControl4 = Nothing

                If TypeOf m_pHookHelper.Hook Is IMapControl4 Then
                    mapControl = CType(m_pHookHelper.Hook, IMapControl4)
                ElseIf TypeOf m_pHookHelper.Hook Is IToolbarControl2 Then
                    Dim toolbarControl As IToolbarControl2 = CType(m_pHookHelper.Hook, IToolbarControl2)
                    mapControl = CType(toolbarControl.Buddy, IMapControl4)
                End If

                If Nothing Is mapControl.CustomProperty OrElse Not (TypeOf mapControl.CustomProperty Is IFeatureLayer) Then
                    Return
                End If

                Dim featureLayer As IFeatureLayer = CType(mapControl.CustomProperty, IFeatureLayer)


                'Launch the layer's properties
                Dim typ As Type
                Dim obj As Object
                Dim g As Guid()

                ' METHOD 1: Instantiating a COM object and displaying its property pages
                ' ONLY WORKS ON TRUE COM OBJECTS!  .NET objects that have rolled their own
                ' ISpecifyPropertyPages implementation will error out when you try to cast
                ' the instantiated object to your own ISpecifyPropertyPages implementation.

                ' Get the typeinfo for the ActiveX common dialog control
                typ = Type.GetTypeFromProgID("MSComDlg.CommonDialog")

                ' Create an instance of the control.  We pass it to the property frame function
                ' so the property pages have an object from which to get current settings and apply
                ' new settings.
                obj = Activator.CreateInstance(typ)
                ' This handy function calls IPersistStreamInit->New on COM objects to initialize them
                ActiveXMessageFormatter.InitStreamedObject(obj)

                ' Get the property pages for the control using the direct CAUUID method
                ' This only works for true COM objects and I demonstrate it here only
                ' to show how it is done.  Use the static method
                ' PropertyPage.GetPagesForType() method for real-world use.
                Dim pag As ISpecifyPropertyPages = CType(obj, ISpecifyPropertyPages)
                Dim cau As CAUUID = New CAUUID(0)
                pag.GetPages(cau)
                g = cau.GetPages()

                ' Instantiating a .NET object and displaying its property pages
                ' WORKS ON ALL OBJECTS, .NET or COM    

                ' Create an instance of the .NET control, MyUserControl
                typ = Type.GetTypeFromProgID("SymbolSelector.PropertySheet")

                ' Retrieve the pages for the control
                g = PropertyPage.GetPagesForType(typ)

                ' Create an instance of the control that we can give to the property pages
                obj = Activator.CreateInstance(typ)

                AddHandler (CType(obj, SymbolSelector.PropertySheet)).OnFeatureLayerRendererChanged, AddressOf OnFeatureLayerRendererChanged

                'add the yahoo layer to the property-sheet control
                CType(obj, SymbolSelector.PropertySheet).FeatureLayer = featureLayer

                ' Display the OLE Property page for the control
                Dim items As Object() = New Object() {obj}

                PropertyPage.CreatePropertyFrame(IntPtr.Zero, 500, 500, "FeatureLayer Symbology", items, g)
            Catch ex As Exception
                System.Diagnostics.Trace.WriteLine(ex.Message)
            End Try
        End Sub

        Private Sub OnFeatureLayerRendererChanged(ByVal sender As Object, ByVal args As EventArgs)
            m_pHookHelper.ActiveView.ContentsChanged()

            'Refresh the display
            m_pHookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, Nothing, m_pHookHelper.ActiveView.ScreenDisplay.DisplayTransformation.FittedBounds)
            m_pHookHelper.ActiveView.ScreenDisplay.UpdateWindow()
        End Sub

#End Region
  End Class
End Namespace