About the Custom Root Object (Style Gallery) Sample
[C#]
clsGxStyleGalleryClass.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Catalog;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Display;
namespace CustomRootObject_CS
{
[Guid("2e04e777-0673-4e90-b494-3ad3fefefd01")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("CustomRootObject_CS.clsGxStyleGalleryClass")]
public class clsGxStyleGalleryClass : ESRI.ArcGIS.Catalog.IGxObject, ESRI.ArcGIS.Catalog.IGxObjectContainer
{
#region Member Variable
private CustomRootObject_CS.clsGxStyleGallery m_pParent;
private IGxCatalog m_pCatalog;
private IStyleGalleryClass m_pClass;
private IGxObjectArray m_pChildren;
private bool m_childrenLoaded;
#endregion
#region Constructors
public clsGxStyleGalleryClass()
: base()
{
m_pChildren = new GxObjectArray();
m_childrenLoaded = false;
}
public IStyleGalleryClass StyleGalleryClass
{
set
{
m_pClass = value;
}
}
#endregion
public void PreviewItem(IStyleGalleryItem pItem, int hDC, tagRECT r)
{
//Draw a representation of the item to the given DC.
try
{
m_pClass.Preview(pItem.Item, hDC, ref r);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
}
#region IGxObject Implementations
public void Attach(ESRI.ArcGIS.Catalog.IGxObject Parent, ESRI.ArcGIS.Catalog.IGxCatalog pCatalog)
{
m_pParent = (CustomRootObject_CS.clsGxStyleGallery)Parent;
m_pCatalog = pCatalog;
}
public string BaseName
{
get
{
return m_pClass.Name;
}
}
public string Category
{
get
{
return "Style Gallery Class";
}
}
public ESRI.ArcGIS.esriSystem.UID ClassID
{
get
{
return null;
}
}
public void Detach()
{
//It is our responsibility to detach all our children before deleting them.
//This is to avoid circular referencing problems.
int i = 0;
int tempFor1 = m_pChildren.Count;
for (i = 0; i <= tempFor1; i++)
{
m_pChildren.Item(i).Detach();
}
m_pParent = null;
m_pCatalog = null;
}
public string FullName
{
get
{
return m_pClass.Name;
}
}
public ESRI.ArcGIS.esriSystem.IName InternalObjectName
{
get
{
return null;
}
}
public bool IsValid
{
get
{
return true;
}
}
public string Name
{
get
{
return m_pClass.Name;
}
}
public ESRI.ArcGIS.Catalog.IGxObject Parent
{
get
{
return m_pParent;
}
}
public void Refresh()
{
//Unload and reload the children.
m_pChildren.Empty();
m_childrenLoaded = false;
LoadChildren();
}
#endregion
#region IGxObjectContainer Implementations
public ESRI.ArcGIS.Catalog.IGxObject AddChild(ESRI.ArcGIS.Catalog.IGxObject child)
{
return null;
}
public bool AreChildrenViewable
{
get
{
return true;
}
}
public ESRI.ArcGIS.Catalog.IEnumGxObject Children
{
get
{
LoadChildren();
return (IEnumGxObject)m_pChildren;
}
}
public void DeleteChild(ESRI.ArcGIS.Catalog.IGxObject child)
{
}
public bool HasChildren
{
get
{
return true;
}
}
#endregion
private void LoadChildren()
{
if (m_childrenLoaded)
return;
//Our children are GxContainer objects that represent the actual style items
//of a certain type.
IEnumStyleGalleryItem pEnumItems = null;
pEnumItems = m_pParent.StyleGallery.get_Items(m_pClass.Name, "ESRI.style", "");
IStyleGalleryItem pItem = null;
pItem = pEnumItems.Next();
while (pItem != null)
{
clsGxStyleGalleryItem pGxItem = null;
pGxItem = new clsGxStyleGalleryItem();
pGxItem.StyleGalleryItem = pItem;
IGxObject pGxObject = null;
pGxObject = pGxItem;
pGxObject.Attach(this, m_pCatalog);
m_pChildren.Insert(-1, pGxObject);
pItem = pEnumItems.Next();
}
m_childrenLoaded = true;
}
}
}
[Visual Basic .NET]
clsGxStyleGalleryClass.vb
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Catalog
Imports ESRI.ArcGIS.Display
Imports System.Runtime.InteropServices
<ComClass(clsGxStyleGalleryClass.ClassId, clsGxStyleGalleryClass.InterfaceId, clsGxStyleGalleryClass.EventsId), _
ProgId("CustomRootObjectVBNET.clsGxStyleGalleryClass")> _
Public Class clsGxStyleGalleryClass
Implements ESRI.ArcGIS.Catalog.IGxObject
Implements ESRI.ArcGIS.Catalog.IGxObjectContainer
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "42d4d338-936c-4a71-a02d-577047740007"
Public Const InterfaceId As String = "afa9eff2-7195-48ba-b025-d7daf973c453"
Public Const EventsId As String = "a971370b-baf7-4634-b71d-9839eeb9ea00"
#End Region
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
#Region "Member Variable"
Private m_pParent As clsGxStyleGallery
Private m_pCatalog As IGxCatalog
Private m_pClass As IStyleGalleryClass
Private m_pChildren As IGxObjectArray
Private m_childrenLoaded As Boolean
#End Region
Public Sub New()
MyBase.New()
m_pChildren = New GxObjectArray
m_childrenLoaded = False
End Sub
Public WriteOnly Property StyleGalleryClass() As IStyleGalleryClass
Set(ByVal value As IStyleGalleryClass)
m_pClass = value
End Set
End Property
Public Sub PreviewItem(ByVal pItem As IStyleGalleryItem, ByVal hDC As Long, ByVal r As tagRECT)
'Draw a representation of the item to the given DC.
m_pClass.Preview(pItem.Item, hDC, r)
End Sub
Public Sub Attach(ByVal Parent As ESRI.ArcGIS.Catalog.IGxObject, ByVal pCatalog As ESRI.ArcGIS.Catalog.IGxCatalog) Implements ESRI.ArcGIS.Catalog.IGxObject.Attach
m_pParent = Parent
m_pCatalog = pCatalog
End Sub
Public ReadOnly Property BaseName() As String Implements ESRI.ArcGIS.Catalog.IGxObject.BaseName
Get
BaseName = m_pClass.Name
End Get
End Property
Public ReadOnly Property Category() As String Implements ESRI.ArcGIS.Catalog.IGxObject.Category
Get
Category = "Style Gallery Class"
End Get
End Property
Public ReadOnly Property ClassID1() As ESRI.ArcGIS.esriSystem.UID Implements ESRI.ArcGIS.Catalog.IGxObject.ClassID
Get
ClassID1 = Nothing
End Get
End Property
Public Sub Detach() Implements ESRI.ArcGIS.Catalog.IGxObject.Detach
'It is our responsibility to detach all our children before deleting them.
'This is to avoid circular referencing problems.
Dim i As Long
For i = 0 To m_pChildren.Count
m_pChildren.Item(i).Detach()
Next
m_pParent = Nothing
m_pCatalog = Nothing
End Sub
Public ReadOnly Property FullName() As String Implements ESRI.ArcGIS.Catalog.IGxObject.FullName
Get
FullName = m_pClass.Name
End Get
End Property
Public ReadOnly Property InternalObjectName() As ESRI.ArcGIS.esriSystem.IName Implements ESRI.ArcGIS.Catalog.IGxObject.InternalObjectName
Get
InternalObjectName = Nothing
End Get
End Property
Public ReadOnly Property IsValid() As Boolean Implements ESRI.ArcGIS.Catalog.IGxObject.IsValid
Get
IsValid = True
End Get
End Property
Public ReadOnly Property Name() As String Implements ESRI.ArcGIS.Catalog.IGxObject.Name
Get
Name = m_pClass.Name
End Get
End Property
Public ReadOnly Property Parent() As ESRI.ArcGIS.Catalog.IGxObject Implements ESRI.ArcGIS.Catalog.IGxObject.Parent
Get
Parent = m_pParent
End Get
End Property
Public Sub Refresh() Implements ESRI.ArcGIS.Catalog.IGxObject.Refresh
'Unload and reload the children.
m_pChildren.Empty()
m_childrenLoaded = False
LoadChildren()
End Sub
Public Function AddChild(ByVal child As ESRI.ArcGIS.Catalog.IGxObject) As ESRI.ArcGIS.Catalog.IGxObject Implements ESRI.ArcGIS.Catalog.IGxObjectContainer.AddChild
End Function
Public ReadOnly Property AreChildrenViewable() As Boolean Implements ESRI.ArcGIS.Catalog.IGxObjectContainer.AreChildrenViewable
Get
AreChildrenViewable = True
End Get
End Property
Public ReadOnly Property Children() As ESRI.ArcGIS.Catalog.IEnumGxObject Implements ESRI.ArcGIS.Catalog.IGxObjectContainer.Children
Get
LoadChildren()
Children = m_pChildren
End Get
End Property
Public Sub DeleteChild(ByVal child As ESRI.ArcGIS.Catalog.IGxObject) Implements ESRI.ArcGIS.Catalog.IGxObjectContainer.DeleteChild
End Sub
Public ReadOnly Property HasChildren() As Boolean Implements ESRI.ArcGIS.Catalog.IGxObjectContainer.HasChildren
Get
HasChildren = True
End Get
End Property
Private Sub LoadChildren()
If m_childrenLoaded Then Exit Sub
'Our children are GxContainer objects that represent the actual style items
'of a certain type.
Dim pEnumItems As IEnumStyleGalleryItem
Dim pItem As IStyleGalleryItem
Try
pEnumItems = m_pParent.StyleGallery.Items(m_pClass.Name, "ESRI.style", "")
pItem = pEnumItems.Next
While (Not pItem Is Nothing)
Dim pGxItem As clsGxStyleGalleryItem
pGxItem = New clsGxStyleGalleryItem
pGxItem.StyleGalleryItem = pItem
Dim pGxObject As IGxObject
pGxObject = pGxItem
pGxObject.Attach(Me, m_pCatalog)
m_pChildren.Insert(-1, pGxObject)
pItem = pEnumItems.Next
End While
m_childrenLoaded = True
Catch ex As Exception
Finally
pEnumItems = Nothing
pItem = Nothing
End Try
End Sub
End Class