This code works for me 9.3.1 sp1 vista64using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Carto;
using System.Diagnostics;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;
namespace Forums
{
public class AddLayerExtCmd : BaseCommand
{
IApplication m_app;
public AddLayerExtCmd()
{
m_caption = "Add LayerExtension";
}
public override void OnClick()
{
MyLayerExtension.AddExtensions(((IMxDocument)m_app.Document).FocusMap);
}
public override void OnCreate(object hook)
{
m_app = (IApplication)hook;
}
}
public class MyLayerExtension: ILayerExtensionDraw
{
public static void AddExtensions(IMap map)
{
if (map.LayerCount == 0)
throw new Exception("no layers in map");
IEnumLayer enumLayer = map.get_Layers(null, true);
ILayer layer;
while ((layer = enumLayer.Next()) != null)
{
ILayerExtensions exts = layer as ILayerExtensions;
if (exts == null)
continue;
for (int i = exts.ExtensionCount - 1; i > -1; i--)
{
if (exts.get_Extension(i) is MyLayerExtension)
{
Debug.Print("removing old extension from {0}", layer.Name);
exts.RemoveExtension(i);
}
}
exts.AddExtension(new MyLayerExtension());
}
}
#region ILayerExtensionDraw Members
public void AfterLayerDraw(ILayer pLayer, esriDrawPhase DrawPhase,
IDisplay pDisplay, ITrackCancel pTrackCancel)
{
Debug.Print("Afterlayerdraw {0} {1}", pLayer.Name, DrawPhase);
}
public void BeforeLayerDraw(ILayer pLayer, esriDrawPhase DrawPhase,
IDisplay pDisplay, ITrackCancel pTrackCancel)
{
Debug.Print("Beforelayerdraw {0} {1}", pLayer.Name, DrawPhase);
}
#endregion
}
}