Using an application as the ToolbarControl buddy
To set an application as the ToolbarControl buddy, perform the following steps:
-
Define the buddy application (for example, a form) as implementing IToolbarBuddy.
-
Set the application as the ToolbarControl buddy.
-
Add the OpenMxFile command to the toolbar. This can be done manually using the ToolbarControl Property Pages or programmatically using the ToolbarControl AddItem method. This step is typically performed at application start-up.
-
Override IToolbarBuddy.CurrentTool to set the CurrentTool property of, for example, the MapControl or PageLayoutControl.
The ToolbarControl uses the buddy application to set the CurrentTool.
See the following code example:
[C#]
//Set the ToolbarControl buddy to be the application.
axToolbarControl1.SetBuddyControl(this);
//Add items to the ToolbarControl.
axToolbarControl1.AddItem(new OpenMxFile(), - 1, 0, false, - 1,
esriCommandStyles.esriCommandStyleIconAndText);
public ITool CurrentTool
{
get
{
//Get the current tool.
return m_currentTool;
}
set
{
//Set the current tool.
m_currentTool = value;
axMapControl1.CurrentTool = m_currentTool;
axPageLayoutControl1.CurrentTool = m_currentTool;
}
}
[VB.NET]
'Set the application as the buddy.
AxToolbarControl1.SetBuddyControl(Me)
'Add items to the ToolbarControl.
AxToolbarControl1.AddItem(New OpenMxFile, 0, 0, False, 0, esriCommandStyles.esriCommandStyleIconAndText)
Public Property CurrentTool() As ESRI.ArcGIS.SystemUI.ITool Implements ESRI.ArcGIS.Controls.IToolbarBuddy.CurrentTool
Get
'Get the current tool.
CurrentTool = m_pCurrentTool
End Get
Set(ByVal Value As ESRI.ArcGIS.SystemUI.ITool)
'Set the current tool.
m_pCurrentTool = Value
AxMapControl1.CurrentTool = m_pCurrentTool
AxPageLayoutControl1.CurrentTool = m_pCurrentTool
End Set
End Property
Each item's Command.OnCreate method is passed the ToolbarControl as the hook. The following code shows how an OpenMxFile command can access the application from the hook's buddy and how the command operates on a MapControl and PageLayoutControl simultaneously:
[C#]
public sealed class OpenMxFile: BaseCommand
{
private ApplicationAsToolbarBuddy.Form1 m_application;
public OpenMxFile()
{
base.m_caption = "OpenMxFile";
base.m_message = "OpenMxFile";
base.m_toolTip = "OpenMxFile";
base.m_enabled = true;
base.m_bitmap = new System.Drawing.Bitmap(GetType()
.Assembly.GetManifestResourceStream(GetType(), "folder_open.bmp"));
}
public override void OnCreate(object hook)
{
if (hook is IToolbarControl)
{
IToolbarControl toolbarControl = (IToolbarControl)hook;
m_application = (ApplicationAsToolbarBuddy.Form1)toolbarControl.Buddy;
}
}
public override void OnClick()
{
System.Windows.Forms.OpenFileDialog openFileDialog = new
System.Windows.Forms.OpenFileDialog();
openFileDialog.Title = "Open Map Document";
openFileDialog.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog.ShowDialog();
//Exit if no map document is selected.
string filePath = openFileDialog.FileName;
if (filePath == "")
return ;
//Validate the map document.
if (m_application.axMapControl1.CheckMxFile(filePath))
{
//Load the map document into the MapControl and PageLayoutControl.
m_application.axMapControl1.LoadMxFile(filePath, "", "");
m_application.axPageLayoutControl1.LoadMxFile(filePath, "");
}
}
}
[VB.NET]
Public NotInheritable Class OpenMxFile
Inherits BaseCommand
Private m_pApplication As ApplicationBuddy
Public Sub New()
MyBase.New()
MyBase.m_caption = "OpenMxFile"
MyBase.m_message = "OpenMxFile"
MyBase.m_toolTip = "OpenMxFile"
MyBase.m_enabled = True
MyBase.m_bitmap = New System.Drawing.Bitmap(GetType(ApplicationBuddy).Assembly.GetManifestResourceStream(GetType(ApplicationBuddy), "folder_open.bmp"))
End Sub
Public Overrides Sub OnCreate(ByVal hook As Object)
If TypeOf hook Is IToolbarControl Then
Dim pToolbarControl As IToolbarControl
pToolbarControl = hook
m_pApplication = pToolbarControl.Buddy
End If
End Sub
Public Overrides Sub OnClick()
Dim pOpenFileDialog As New OpenFileDialog
pOpenFileDialog.Title = "Open Map Document"
pOpenFileDialog.Filter = "Map Documents (*.mxd)|*.mxd"
pOpenFileDialog.ShowDialog()
'Exit if no map document is selected.
Dim sFilePath As String
sFilePath = pOpenFileDialog.FileName
If sFilePath = "" Then Exit Sub
'Validate the map document.
If m_pApplication.axMapControl1.CheckMxFile(sFilePath) Then
'Load the map document into the MapControl and PageLayoutControl.
m_pApplication.axMapControl1.LoadMxFile(sFilePath)
m_pApplication.axPageLayoutControl1.LoadMxFile(sFilePath)
End If
End Sub
End Class
See Also:
ToolbarControl classIToolbarControl interface
IToolbarBuddy interface
Development licensing | Deployment licensing |
---|---|
Engine Developer Kit | Engine Runtime |
ArcView | |
ArcEditor | |
ArcInfo |