 
					
				
		
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
namespace RestartSketch
{
  /// <summary>
  /// Editor Extension to add a context menu item
  /// </summary>
  public class RSExt : ESRI.ArcGIS.Desktop.AddIns.Extension
  {
    private IEditEvents_Event m_editEvents;
    public RSExt()
    {
    }
    protected override void OnStartup()
    {
      m_editEvents = ArcMap.Editor as IEditEvents_Event;
      m_editEvents.OnStartEditing += new IEditEvents_OnStartEditingEventHandler(m_editEvents_OnStartEditing);
     }
    void m_editEvents_OnStartEditing()
    {
      //Add to various sketch context menus by guid and bottom up position
      //AddtoContextMenu("{02D578D0-42AB-11d2-84D6-0000F875B9C6}", 3); //straight constructor context menu
      //AddtoContextMenu("{066022E8-A0FD-48e5-AF5D-F5C77E88DD42}", 3); //trace constructor
      //AddtoContextMenu("{68A337DF-751B-45dd-AA39-91194BE88D7C}", 3); //other constructors
      AddtoContextMenu("{FD799450-472C-11d2-84D8-0000F875B9C6}", 3); //over a sketch
    }
    private void AddtoContextMenu(string cmg, int pos)
    {
      // Get the context menu
      UID contextMenuUID = new UIDClass();
      contextMenuUID.Value = cmg;
      ICommandBar contextMenu = ArcMap.Application.Document.CommandBars.Find(contextMenuUID) as ICommandBar;
      if (contextMenu != null)
      {
        // Get the command item you want to add
        UID commandUID = new UIDClass();
        commandUID.Value = "ESRI_RestartSketch_RestartSketchCmd";
        
        //Check if it is already present on the context menu...
        ICommandItem myCmdItem = contextMenu.Find(commandUID);
        if (myCmdItem == null)
        {
          myCmdItem = contextMenu.Add(commandUID, contextMenu.Count - pos);
          //myCmdItem.Group = true;
          myCmdItem.Refresh();
        }
      }
    }
  }
}
					
				
			
			
				
			
			
				
			
			
			
			
			
			
		
protected override void OnStartup()
{
    WireEvents();
}
private void WireEvents()
{
    
    ArcMap.Events.OpenDocument +=
            new ESRI.ArcGIS.ArcMapUI.IDocumentEvents_OpenDocumentEventHandler(Events_OpenDocument);
    ArcMap.Events.NewDocument +=
            new ESRI.ArcGIS.ArcMapUI.IDocumentEvents_NewDocumentEventHandler(Events_NewDocument);
}
void Events_OpenDocument()
{
    System.Diagnostics.Debug.WriteLine("**** Events_OpenDocument ****");
    LoadContextMenu();
}
void Events_NewDocument()
{
    System.Diagnostics.Debug.WriteLine("**** Events_NewDocument ****");
    LoadContextMenu();
}
private void LoadContextMenu()
{
    ICommandBars cmdBars = ArcMap.Application.Document.CommandBars;
    UID contextMenuUID = new UIDClass();
    contextMenuUID.Value = "{BF643199-9062-11D2-AE71-080009EC732A}"; //esriArcMapUI.FeatureLayerContextMenu
    ICommandBar contextMenu = cmdBars.Find(contextMenuUID, false, true) as ICommandBar;
    if (contextMenu != null)
    {
        // Get the command item you want to add
        UID commandUID = new UIDClass();
        commandUID.Value = "Test_TestParaTextButton"; // Add-in button ID
        //Check if it is already present on the context menu.
        ICommandItem myCmdItem = contextMenu.Find(commandUID, false);
        if (myCmdItem == null)
        {
            Object pos = contextMenu.Count - 1;
            myCmdItem = contextMenu.Add(commandUID, ref pos);
            //myCmdItem.Group = true;
            myCmdItem.Refresh();
        }
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("**** contextMenu is null ****");
    }
}
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Framework
Public Class CycleThrough
    Inherits ESRI.ArcGIS.Desktop.AddIns.Extension
    Public Sub New()
    End Sub
    Protected Overrides Sub OnStartup()
        WireEvents()
    End Sub
    Protected Overrides Sub OnShutdown()
    End Sub
    Private Sub WireEvents()
        Dim docEvents As IDocumentEvents_Event
        Dim mapStatusEvents As IApplicationStatusEvents_Event
        docEvents = TryCast(My.ArcMap.Application.Document, IDocumentEvents_Event)
        AddHandler docEvents.NewDocument, AddressOf OnNewDocument
        AddHandler docEvents.OpenDocument, AddressOf OnOpenDocument
        If TypeOf My.ArcMap.Application.Document.Parent Is IMxApplication Then
            mapStatusEvents = TryCast(My.ArcMap.Application.Document.Parent, IApplicationStatusEvents_Event)
            AddHandler mapStatusEvents.Initialized, AddressOf OnInitialized
        End If
    End Sub
    Private Sub OnNewDocument()
        SetupContext()
    End Sub
    Private Sub OnOpenDocument()
        SetupContext()
    End Sub
    Private Sub OnInitialized()
        SetupContext()
    End Sub
    Private Sub SetupContext()
        Dim contextMenuGUID As New UID
        contextMenuGUID.Value = "{BF643199-9062-11D2-AE71-080009EC732A}"
        Dim myContextMenu As ICommandBar = My.ArcMap.Application.Document.CommandBars.Find(contextMenuGUID, False, False)
        If myContextMenu IsNot Nothing Then
            Dim commandGUID As New UID
            commandGUID.Value = "LayerCycle_CycleButton"
            Dim myCommandItem As ICommandItem = myContextMenu.Find(commandGUID)
            If myCommandItem Is Nothing Then
                myCommandItem = myContextMenu.Add(commandGUID, 9) 'the 9 is where I happen to want the button to be, it could be nothing or any other value 
                myCommandItem.Refresh()
            End If
        End If
    End Sub
End Class
