編寫ArcGIS程式時經常遇到向Toolbar中添加諸如TextBox等組件的需求,
本文介紹向ArcGIS Toolbar添加任意windows組件的方法。
新寫一個類CommandCustomControl,該類實現ICommand介面和IToolControl介面,
在引用CommandCustomControl表單中添加一個windows組件,
比如Label、textbox、combobox,然後傳入CommandCustomControl中。
調用方法如下:
Dim pText As New CommandCustomControl
pText.CustomControl = TextBox1
AxToolbarControl1.AddItem(pText)
Dim pCombo As New CommandCustomControl
pCombo.CustomControl = ComboBox1
AxToolbarControl1.AddItem(pCombo)
下面給出CommandCustomControl類的原始碼:
Imports ESRI.ArcGIS.SystemUI
Imports System.Windows.Forms
''' <summary>
''' 自訂的command,可以向toolbar中加入label、textbox等組件
''' </summary>
''' <remarks>該類實現ICommand介面和IToolControl介面,在引用CommandCustomControl表單中添加一個windows組件,比如Label、textbox、combobox,然後傳入CommandCustomControl中。</remarks>
Public Class CommandCustomControl
Implements IToolControl
Implements ICommand
#Region "變數和屬性"
Public pCompNotify As ICompletionNotify
Private m_Control As Control
''' <summary>
''' 傳入的control,如Label,textbox等等
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks>可以在此傳入control,或者在建構函式中建立control,當然最好是在外面傳入control,這樣可以在外面設計control,並捕獲control的相關事件</remarks>
Property CustomControl() As Control
Get
Return m_Control
End Get
Set(ByVal value As Control)
m_Control = value
End Set
End Property
#End Region
''' <summary>
''' 建構函式
''' </summary>
''' <remarks></remarks>
Sub New()
m_Control = New Label
m_Control.Text = "label"
End Sub
#Region "實現IToolControl"
Public ReadOnly Property hWnd() As Integer Implements ESRI.ArcGIS.SystemUI.IToolControl.hWnd
Get
Return m_Control.Handle
End Get
End Property
Public Function OnDrop(ByVal barType As ESRI.ArcGIS.SystemUI.esriCmdBarType) As Boolean Implements ESRI.ArcGIS.SystemUI.IToolControl.OnDrop
If barType = esriCmdBarType.esriCmdBarTypeToolbar Then
Return True
End If
End Function
Public Sub OnFocus(ByVal complete As ESRI.ArcGIS.SystemUI.ICompletionNotify) Implements ESRI.ArcGIS.SystemUI.IToolControl.OnFocus
pCompNotify = complete
End Sub
#End Region
#Region "實現ICommand"
Public ReadOnly Property Bitmap() As Integer Implements ESRI.ArcGIS.SystemUI.ICommand.Bitmap
Get
End Get
End Property
Public ReadOnly Property Caption() As String Implements ESRI.ArcGIS.SystemUI.ICommand.Caption
Get
End Get
End Property
Public ReadOnly Property Category() As String Implements ESRI.ArcGIS.SystemUI.ICommand.Category
Get
End Get
End Property
Public ReadOnly Property Checked() As Boolean Implements ESRI.ArcGIS.SystemUI.ICommand.Checked
Get
Return True
End Get
End Property
Public ReadOnly Property Enabled() As Boolean Implements ESRI.ArcGIS.SystemUI.ICommand.Enabled
Get
Return True
End Get
End Property
Public ReadOnly Property HelpContextID() As Integer Implements ESRI.ArcGIS.SystemUI.ICommand.HelpContextID
Get
End Get
End Property
Public ReadOnly Property HelpFile() As String Implements ESRI.ArcGIS.SystemUI.ICommand.HelpFile
Get
End Get
End Property
Public ReadOnly Property Message() As String Implements ESRI.ArcGIS.SystemUI.ICommand.Message
Get
End Get
End Property
Public ReadOnly Property Name() As String Implements ESRI.ArcGIS.SystemUI.ICommand.Name
Get
End Get
End Property
Public Sub OnClick() Implements ESRI.ArcGIS.SystemUI.ICommand.OnClick
End Sub
Public Sub OnCreate(ByVal hook As Object) Implements ESRI.ArcGIS.SystemUI.ICommand.OnCreate
End Sub
Public ReadOnly Property Tooltip() As String Implements ESRI.ArcGIS.SystemUI.ICommand.Tooltip
Get
End Get
End Property
#End Region
End Class
給出完整程式下載:/Files/wall/TestCommandCustomControl.rar