ArcObjects Library Reference  

SymbolSelectorPropPage

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

[C#]

SymbolSelectorPropPage.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;

namespace SymbolSelector
{
  [Guid("795FC5F5-4365-4cce-99C4-B5A3DDD1060A")]
  [ComVisible(true)]
  [ProgId("SymbolSelector.SymbolSelectorPropPage")]
  [ClassInterface(ClassInterfaceType.None)]
  public partial class SymbolSelectorPropPage : PropertyPage
  {
    //private AxMapControl m_axMapPreview = null;
    private AxSymbologyControl m_axSymbologyControl = null;
    public IStyleGalleryItem m_styleGalleryItem;


    public SymbolSelectorPropPage()
    {
      InitializeComponent();

      this.CreateControl();
    }

    protected override void OnPageDeactivate()
    {
      base.OnPageDeactivate();

      //unwire the OnItemSelected event
      m_axSymbologyControl.OnItemSelected -= new ISymbologyControlEvents_Ax_OnItemSelectedEventHandler(OnItemSelected);

      //dispose the control
      m_axSymbologyControl.Dispose();
      m_axSymbologyControl = null;
    }

    protected override void OnPageApply()
    {
      base.OnPageApply();

      PropertySheet propSheet = Objects[0] as PropertySheet;

      IFeatureLayer layer = propSheet.FeatureLayer;
      if (null == layer)
        return;

      if (m_styleGalleryItem == null) return;

      IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer)layer;

      //Create a new renderer
      ISimpleRenderer simpleRenderer = new SimpleRendererClass();
      //Set its symbol from the styleGalleryItem
      simpleRenderer.Symbol = (ISymbol)m_styleGalleryItem.Item;
      //Set the renderer into the geoFeatureLayer
      geoFeatureLayer.Renderer = (IFeatureRenderer)simpleRenderer;

      //Make the PropertyPage class fire an event notifying that the layer's renderer has changed
      propSheet.FireFeatureLayerRendererChanged();
    }

    protected override void OnPageActivate(IntPtr hWndParent, Rectangle Rect, bool bModal)
    {
      try
      {
        base.OnPageActivate(hWndParent, Rect, bModal);

        LoadStyle();
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message);
      }
    }

    private void SetFeatureClassStyle(esriSymbologyStyleClass styleClass, ISymbol symbol)
    {
      m_styleGalleryItem = null;

      //Get and set the style class
      m_axSymbologyControl.StyleClass = styleClass;
      ISymbologyStyleClass symbologyStyleClass = m_axSymbologyControl.GetStyleClass(styleClass);

      //Create a new server style gallery item with its style set
      IStyleGalleryItem styleGalleryItem = new ServerStyleGalleryItem();
      styleGalleryItem.Item = symbol;
      styleGalleryItem.Name = "mySymbol";

      //Add the item to the style class and select it
      symbologyStyleClass.AddItem(styleGalleryItem, 0);
      symbologyStyleClass.SelectItem(0);
    }

    private void PreviewImage()
    {
      //Get and set the style class 
      ISymbologyStyleClass symbologyStyleClass = m_axSymbologyControl.GetStyleClass(m_axSymbologyControl.StyleClass);

      //Preview an image of the symbol
      stdole.IPictureDisp picture = symbologyStyleClass.PreviewItem(m_styleGalleryItem, pictureBox1.Width, pictureBox1.Height);
      System.Drawing.Image image = System.Drawing.Image.FromHbitmap(new System.IntPtr(picture.Handle));
      pictureBox1.Image = image;
    }

    private void OnItemSelected(object sender, ESRI.ArcGIS.Controls.ISymbologyControlEvents_OnItemSelectedEvent e)
    {
      //Preview the selected item
      m_styleGalleryItem = (IStyleGalleryItem)e.styleGalleryItem;
      PreviewImage();

      //set the dirty flag
      if (!IsPageActivating)
        PageIsDirty = true;
    }

    private void SymbolSelectorPropPage_Shown(object sender, EventArgs e)
    {
      m_axSymbologyControl = new AxSymbologyControl();
      m_axSymbologyControl.Parent = this;
      m_axSymbologyControl.SetBounds(16, 38, 298, 369);
      m_axSymbologyControl.Refresh();
      m_axSymbologyControl.Update();
      m_axSymbologyControl.OnItemSelected += new ISymbologyControlEvents_Ax_OnItemSelectedEventHandler(OnItemSelected);

      LoadStyle();
    }

    private void LoadStyle()
    {
      try
      {
        if (null == m_axSymbologyControl)
          return;

        PropertySheet propSheet = Objects[0] as PropertySheet;

        IFeatureLayer layer = propSheet.FeatureLayer;
        if (null == layer)
          return;

        //Get the ArcGIS install location
        string sInstall = ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Path;

        //Load the ESRI.ServerStyle file into the SymbologyControl
        m_axSymbologyControl.LoadStyleFile(sInstall + "\\Styles\\ESRI.ServerStyle");

        IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer)layer;
        ISimpleRenderer simpleRenderer = (ISimpleRenderer)geoFeatureLayer.Renderer;

        //set SymbologyStyle based upon feature type
        switch (layer.FeatureClass.ShapeType)
        {
          case esriGeometryType.esriGeometryPoint:
            SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassMarkerSymbols, simpleRenderer.Symbol);
            break;
          case esriGeometryType.esriGeometryPolyline:
            SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassLineSymbols, simpleRenderer.Symbol);
            break;
          case esriGeometryType.esriGeometryPolygon:
            SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassFillSymbols, simpleRenderer.Symbol);
            break;
        }
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message);
      }
    }

    private void btnMoreSymbols_Click(object sender, EventArgs e)
    {
      //load all available styles from the registry
      //Get the ArcGIS install location
        string sInstall = ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Path;
      string path = System.IO.Path.Combine(sInstall, "Styles");

      string[] styleNames = System.IO.Directory.GetFiles(path, "*.ServerStyle");

      MenuItem[] items = new MenuItem[styleNames.Length + 1];

      for (int i = 0; i < styleNames.Length; i++)
      {
        items[i] = new MenuItem(System.IO.Path.GetFileNameWithoutExtension( styleNames[i] ));
        //add the path as the item's name
        items[i].Name = styleNames[i];
        items[i].Click += new EventHandler(SymbolSelectorPropPage_Click);
      }
      items[styleNames.Length] = new MenuItem("More...", new EventHandler(AddStyleFromFile));

      ContextMenu menu = new ContextMenu(items);
      menu.Show(this, btnMoreSymbols.Location);
    }

    void SymbolSelectorPropPage_Click(object sender, EventArgs e)
    {
      MenuItem selectedItem = (MenuItem)sender;
      
      //Load the style file into the SymbologyControl
      m_axSymbologyControl.LoadStyleFile(selectedItem.Name);
      m_axSymbologyControl.Refresh();
    }

    void AddStyleFromFile(object sender, EventArgs e)
    {
      OpenFileDialog ofd = new OpenFileDialog();
      ofd.CheckPathExists = true;
      ofd.CheckFileExists = true;
      ofd.RestoreDirectory = true;
      ofd.Multiselect = false;
      ofd.Title = "Select Style file";
      ofd.Filter = "ESRI Style Set Files (*.ServerStyle)|*.ServerStyle";

      if (ofd.ShowDialog() == DialogResult.OK)
      {
        //Load the style file into the SymbologyControl
        m_axSymbologyControl.LoadStyleFile(ofd.FileName);
        m_axSymbologyControl.Refresh();
      }

    }

    private void btnReset_Click(object sender, EventArgs e)
    {
      m_axSymbologyControl.Clear();
      LoadStyle();
      m_axSymbologyControl.Refresh();
    }
  }
}
[Visual Basic .NET]

SymbolSelectorPropPage.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.SystemUI

Namespace SymbolSelector
  <Guid("795FC5F5-4365-4cce-99C4-B5A3DDD1060A"), ComVisible(True), ProgId("SymbolSelector.SymbolSelectorPropPage"), ClassInterface(ClassInterfaceType.None)> _
  Public Partial Class SymbolSelectorPropPage : Inherits PropertyPage
	'private AxMapControl m_axMapPreview = null;
	Private m_axSymbologyControl As AxSymbologyControl = Nothing
	Public m_styleGalleryItem As IStyleGalleryItem


	Public Sub New()
	  InitializeComponent()

	  Me.CreateControl()
	End Sub

	Protected Overrides Sub OnPageDeactivate()
	  MyBase.OnPageDeactivate()

	  'unwire the OnItemSelected event
	  RemoveHandler m_axSymbologyControl.OnItemSelected, AddressOf OnItemSelected

	  'dispose the control
	  m_axSymbologyControl.Dispose()
	  m_axSymbologyControl = Nothing
	End Sub

	Protected Overrides Sub OnPageApply()
	  MyBase.OnPageApply()

	  Dim propSheet As PropertySheet = TryCast(Objects(0), PropertySheet)

	  Dim layer As IFeatureLayer = propSheet.FeatureLayer
	  If Nothing Is layer Then
		Return
	  End If

	  If m_styleGalleryItem Is Nothing Then
	  Return
	  End If

	  Dim geoFeatureLayer As IGeoFeatureLayer = CType(layer, IGeoFeatureLayer)

	  'Create a new renderer
	  Dim simpleRenderer As ISimpleRenderer = New SimpleRendererClass()
	  'Set its symbol from the styleGalleryItem
	  simpleRenderer.Symbol = CType(m_styleGalleryItem.Item, ISymbol)
	  'Set the renderer into the geoFeatureLayer
	  geoFeatureLayer.Renderer = CType(simpleRenderer, IFeatureRenderer)

	  'Make the PropertyPage class fire an event notifying that the layer's renderer has changed
	  propSheet.FireFeatureLayerRendererChanged()
	End Sub

	Protected Overrides Sub OnPageActivate(ByVal hWndParent As IntPtr, ByVal Rect As Rectangle, ByVal bModal As Boolean)
	  Try
		MyBase.OnPageActivate(hWndParent, Rect, bModal)

		LoadStyle()
	  Catch ex As Exception
		System.Diagnostics.Trace.WriteLine(ex.Message)
	  End Try
	End Sub

	Private Sub SetFeatureClassStyle(ByVal styleClass As esriSymbologyStyleClass, ByVal symbol As ISymbol)
	  m_styleGalleryItem = Nothing

	  'Get and set the style class
	  m_axSymbologyControl.StyleClass = styleClass
	  Dim symbologyStyleClass As ISymbologyStyleClass = m_axSymbologyControl.GetStyleClass(styleClass)

	  'Create a new server style gallery item with its style set
	  Dim styleGalleryItem As IStyleGalleryItem = New ServerStyleGalleryItem()
	  styleGalleryItem.Item = symbol
	  styleGalleryItem.Name = "mySymbol"

	  'Add the item to the style class and select it
	  symbologyStyleClass.AddItem(styleGalleryItem, 0)
	  symbologyStyleClass.SelectItem(0)
        End Sub

	Private Sub PreviewImage()
	  'Get and set the style class 
	  Dim symbologyStyleClass As ISymbologyStyleClass = m_axSymbologyControl.GetStyleClass(m_axSymbologyControl.StyleClass)

	  'Preview an image of the symbol
	  Dim picture As stdole.IPictureDisp = symbologyStyleClass.PreviewItem(m_styleGalleryItem, pictureBox1.Width, pictureBox1.Height)
	  Dim image As System.Drawing.Image = System.Drawing.Image.FromHbitmap(New System.IntPtr(picture.Handle))
	  pictureBox1.Image = image
	End Sub

	Private Sub OnItemSelected(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Controls.ISymbologyControlEvents_OnItemSelectedEvent)
	  'Preview the selected item
	  m_styleGalleryItem = CType(e.styleGalleryItem, IStyleGalleryItem)
	  PreviewImage()

	  'set the dirty flag
	  If (Not IsPageActivating) Then
		PageIsDirty = True
	  End If
	End Sub

	Private Sub SymbolSelectorPropPage_Shown(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Shown
	  m_axSymbologyControl = New AxSymbologyControl()
	  m_axSymbologyControl.Parent = Me
	  m_axSymbologyControl.SetBounds(16, 38, 298, 369)
	  m_axSymbologyControl.Refresh()
	  m_axSymbologyControl.Update()
	  AddHandler m_axSymbologyControl.OnItemSelected, AddressOf OnItemSelected

	  LoadStyle()
	End Sub

	Private Sub LoadStyle()
	  Try
		If Nothing Is m_axSymbologyControl Then
		  Return
		End If

		Dim propSheet As PropertySheet = TryCast(Objects(0), PropertySheet)

		Dim layer As IFeatureLayer = propSheet.FeatureLayer
		If Nothing Is layer Then
		  Return
		End If

		'Get the ArcGIS install location
                Dim sInstall As String = ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Path

		'Load the ESRI.ServerStyle file into the SymbologyControl
		m_axSymbologyControl.LoadStyleFile(sInstall & "\Styles\ESRI.ServerStyle")

		Dim geoFeatureLayer As IGeoFeatureLayer = CType(layer, IGeoFeatureLayer)
		Dim simpleRenderer As ISimpleRenderer = CType(geoFeatureLayer.Renderer, ISimpleRenderer)

		'set SymbologyStyle based upon feature type
		Select Case layer.FeatureClass.ShapeType
		  Case esriGeometryType.esriGeometryPoint
			SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassMarkerSymbols, simpleRenderer.Symbol)
		  Case esriGeometryType.esriGeometryPolyline
			SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassLineSymbols, simpleRenderer.Symbol)
		  Case esriGeometryType.esriGeometryPolygon
			SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassFillSymbols, simpleRenderer.Symbol)
		End Select
	  Catch ex As Exception
		System.Diagnostics.Trace.WriteLine(ex.Message)
	  End Try
	End Sub

	Private Sub btnMoreSymbols_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMoreSymbols.Click
            'load all available styles
            'Get the ArcGIS install location
            Dim sInstall As String = ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Path
	  Dim path As String = System.IO.Path.Combine(sInstall, "Styles")

	  Dim styleNames As String() = System.IO.Directory.GetFiles(path, "*.ServerStyle")

	  Dim items As MenuItem() = New MenuItem(styleNames.Length){}

	  Dim i As Integer = 0
	  Do While i < styleNames.Length
		items(i) = New MenuItem(System.IO.Path.GetFileNameWithoutExtension(styleNames(i)))
		'add the path as the item's name
		items(i).Name = styleNames(i)
		AddHandler items(i).Click, AddressOf SymbolSelectorPropPage_Click
		  i += 1
	  Loop
	  items(styleNames.Length) = New MenuItem("More...", New EventHandler(AddressOf AddStyleFromFile))

	  Dim menu As ContextMenu = New ContextMenu(items)
	  menu.Show(Me, btnMoreSymbols.Location)
	End Sub

	Private Sub SymbolSelectorPropPage_Click(ByVal sender As Object, ByVal e As EventArgs)
	  Dim selectedItem As MenuItem = CType(sender, MenuItem)

	  'Load the style file into the SymbologyControl
	  m_axSymbologyControl.LoadStyleFile(selectedItem.Name)
	  m_axSymbologyControl.Refresh()
	End Sub

	Private Sub AddStyleFromFile(ByVal sender As Object, ByVal e As EventArgs)
	  Dim ofd As OpenFileDialog = New OpenFileDialog()
	  ofd.CheckPathExists = True
	  ofd.CheckFileExists = True
	  ofd.RestoreDirectory = True
	  ofd.Multiselect = False
	  ofd.Title = "Select Style file"
	  ofd.Filter = "ESRI Style Set Files (*.ServerStyle)|*.ServerStyle"

	  If ofd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
		'Load the style file into the SymbologyControl
		m_axSymbologyControl.LoadStyleFile(ofd.FileName)
		m_axSymbologyControl.Refresh()
	  End If

	End Sub

	Private Sub btnReset_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReset.Click
	  m_axSymbologyControl.Clear()
	  LoadStyle()
	  m_axSymbologyControl.Refresh()
	End Sub
  End Class
End Namespace