SymbolSelectorPropPage.vb
' Copyright 2012 ESRI ' ' All rights reserved under the copyright laws of the United States ' and applicable international laws, treaties, and conventions. ' ' You may freely redistribute and use this sample code, with or ' without modification, provided you include the original copyright ' notice and use restrictions. ' ' See the use restrictions. ' 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