Extending ArcObjects
ArcGISToolbar1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
namespace ArcMapClassLibrary1
{
[Guid("61ac18ce-2818-4708-b4f0-3131ee59d0f2")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ArcMapClassLibrary1.DockableWindow1")]
public partial class DockableWindow1 : UserControl, IDockableWindowDef
{
private IApplication m_application;
private IHookHelper m_hookHelper;
private IMap pmap;
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);
//
// TODO: Add any COM registration code here
//
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);
//
// TODO: Add any COM unregistration code here
//
}
#region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxDockableWindows.Register(regKey);
}
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxDockableWindows.Unregister(regKey);
}
#endregion
#endregion
public DockableWindow1()
{
InitializeComponent();
}
#region IDockableWindowDef Members
string IDockableWindowDef.Caption
{
get
{
//TODO: Replace with locale-based initial title bar caption
return "My C# Dockable Window";
}
}
int IDockableWindowDef.ChildHWND
{
get { return this.Handle.ToInt32(); }
}
string IDockableWindowDef.Name
{
get
{
//TODO: Replace with any non-localizable string
return this.Name;
}
}
void IDockableWindowDef.OnCreate(object hook)
{
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
m_application = hook as IApplication;
pmap =(IMap ) m_application.Document ;
}
void IDockableWindowDef.OnDestroy()
{
//TODO: Release resources and call dispose of any ActiveX control initialized
}
object IDockableWindowDef.UserData
{
get { return null; }
}
#endregion
private void labelPlaceholder_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
IEnumLayer layers =m_hookHelper.FocusMap.Layers ;
ILayer lay;
while ((lay = layers.Next ())!= null)
{
textBox1.Text = textBox1.Text + lay.Name + "\r\n";
}
}
}
}
DockableWindow1.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.ADF.BaseClasses;
namespace ArcMapClassLibrary1
{
/// <summary>
/// Summary description for ArcGISToolbar1.
/// </summary>
[Guid("7bc2e29a-274f-4dd4-90c1-74d952372805")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ArcMapClassLibrary1.ArcGISToolbar1")]
public sealed class ArcGISToolbar1 : BaseToolbar
{
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);
//
// TODO: Add any COM registration code here
//
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);
//
// TODO: Add any COM unregistration code here
//
}
#region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommandBars.Register(regKey);
}
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommandBars.Unregister(regKey);
}
#endregion
#endregion
public ArcGISToolbar1()
{
//
// TODO: Define your toolbar here by adding items
//
//AddItem("esriArcMapUI.ZoomInTool");
//BeginGroup(); //Separator
//AddItem("{FBF8C3FB-0480-11D2-8D21-080009EE4E51}", 1); //undo command
//AddItem(new Guid("FBF8C3FB-0480-11D2-8D21-080009EE4E51"), 2); //redo command
AddItem("ArcMapClassLibrary1.CommandDockableWindow1Command", 1);
}
public override string Caption
{
get
{
//TODO: Replace bar caption
return "My C# Toolbar";
}
}
public override string Name
{
get
{
//TODO: Replace bar ID
return "ArcGISToolbar1";
}
}
}
}
DockableWindow1Command.cs
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
namespace ArcMapClassLibrary1
{
/// <summary>
/// Summary description for dockable window toggle command
/// </summary>
[Guid("28f47c6b-49f6-49cd-a488-23438e1ffdde")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ArcMapClassLibrary1.CommandDockableWindow1Command")]
public sealed class DockableWindow1Command : BaseCommand
{
private IApplication m_application;
private IDockableWindow m_dockableWindow;
private const string DockableWindowGuid = "{61ac18ce-2818-4708-b4f0-3131ee59d0f2}";
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);
//
// TODO: Add any COM registration code here
//
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);
//
// TODO: Add any COM unregistration code here
//
}
#region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommands.Register(regKey);
}
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommands.Unregister(regKey);
}
#endregion
#endregion
public DockableWindow1Command()
{
//
// TODO: Define values for the public properties
//
base.m_category = "DeveloperTemplate"; //localizable text
base.m_caption = "Show/Hide Window"; //localizable text
base.m_message = "Command toggles dockable window visibility (C#)"; //localizable text
base.m_toolTip = "Toggle Dockable Window (C#)"; //localizable text
base.m_name = "DeveloperTemplate_DockableWindow1Command"; //unique id, non-localizable (e.g. "MyCategory_ArcMapCommand")
try
{
//
// TODO: change bitmap name if necessary
//
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overridden Class Methods
/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (hook != null)
m_application = hook as IApplication;
if (m_application != null)
{
SetupDockableWindow();
base.m_enabled = m_dockableWindow != null;
}
else
base.m_enabled = false;
}
/// <summary>
/// Toggle visiblity of dockable window and show the visible state by its checked property
/// </summary>
public override void OnClick()
{
if (m_dockableWindow == null)
return;
if (m_dockableWindow.IsVisible())
m_dockableWindow.Show(false);
else
m_dockableWindow.Show(true);
base.m_checked = m_dockableWindow.IsVisible();
}
public override bool Checked
{
get
{
return m_dockableWindow != null && m_dockableWindow.IsVisible();
}
}
#endregion
private void SetupDockableWindow()
{
if (m_dockableWindow == null)
{
IDockableWindowManager dockWindowManager = m_application as IDockableWindowManager;
if (dockWindowManager != null)
{
UID windowID = new UIDClass();
windowID.Value = DockableWindowGuid;
m_dockableWindow = dockWindowManager.GetDockableWindow(windowID);
}
}
}
}
}