ArcObjects Library Reference  (SystemUI)    

ITool Example

[Visual Basic 6.0]

This sample shows how to create a custom tool, MyTool, using Visual Basic. This custom tool illustrates when certain mouse and keyboard events occur by displaying a message box or a message in the statusbar when each event is fired. For example, when you click on the tool, a message box is displayed. The statusbar message will change when you move the mouse, press or release the left mouse button, or press or release a key on the keyboard. If you double-click, the Select Graphics Tool will become the active tool. If you right click, a custom context menu will be displayed.

How to use:

  1. Create a new ActiveX dll Visual Basic project.
  2. In the VB project, add reference to ESRI SystemUI,
    ESRI Framework, ESRI ArcMapUI Libraries.
  3. Paste this code into a class module.
  4. In the Resource Editor in VB, add a bitmap and give it an Id of 101.
    Also add a cursor and give it an Id of 102.
  5. Compile the project to create the dll.
  6. In the Customize dialog in ArcMap, click Add from file and navigate to the dll you just created.
    The custom tool will be added to ArcMap.
  7. In the Commands panel of the Customize dialog, select MyCustomTools in the Categories list.
    In the Commands list, select MyTool and drag it to a toolbar.
  8. Test the tool.
 '======================================================
 Option Explicit
 
 ' Implement the ICommand and ITool interfaces
 Implements ICommand
 Implements ITool
 
 Dim m_pApp As IApplication      'ArcMap application
 Dim m_pBitmap As IPictureDisp       'Bitmap for the command
 Dim m_pCursor As IPictureDisp       'Cursor for the command
 
 
 Private Sub Class_Initialize()
     'Load the button image and cursor from the resource file.
     Set m_pBitmap = LoadResPicture(101, 0)
     Set m_pCursor = LoadResPicture(102, 2)
 End Sub
 
 Private Property Get ICommand_Bitmap() As esriSystem.OLE_HANDLE
     ' Set the bitmap of the command. The m_pBitmap variable is set
     ' in Class_Initialize.
     ICommand_Bitmap = m_pBitmap
 End Property
 
 Private Property Get ICommand_Caption() As String
     ' Set the string that appears when the command is used as a
     ' menu item.
     ICommand_Caption = "MyTool"
 End Property
 
 Private Property Get ICommand_Category() As String
     ' Set the category of this command. This determines where the
     ' command appears in the Commands panel of the Customize dialog.
     ICommand_Category = "MyCustomTools"
 End Property
 
 Private Property Get ICommand_Checked() As Boolean
     
 End Property
 
 Private Property Get ICommand_Enabled() As Boolean
     ' Add some logic here to specify when the command should
     ' be in for command to be enabled. In this example, the command
     ' is always enabled.
     ICommand_Enabled = True
 End Property
 
 Private Property Get ICommand_HelpContextID() As Long
 
 End Property
 
 Private Property Get ICommand_HelpFile() As String
 
 End Property
 
 Private Property Get ICommand_Message() As String
     'Set the message string that appears in the statusbar of the
     'application when the mouse passes over the command.
     ICommand_Message = "This is my custom tool"
 End Property
 
 Private Property Get ICommand_Name() As String
     ' Set the internal name of this command. By convention, this
     ' name string contains the category and caption of the command.
     ICommand_Name = "MyCustomTool_MyTool"
 End Property
 
 Private Sub ICommand_OnClick()
     ' Add some code to do some action when the command is clicked. In this
     ' example, a message box is displayed.
     MsgBox "Clicked on my command"
 End Sub
 
 Private Sub ICommand_OnCreate(ByVal hook As Object)
     ' The hook argument is a pointer to Application object.
     ' Establish a hook to the application
     Set m_pApp = hook
 End Sub
 
 Private Property Get ICommand_Tooltip() As String
     'Set the string that appears in the screen tip.
     ICommand_Tooltip = "MyTool"
 End Property
 
 Private Property Get ITool_Cursor() As esriSystem.OLE_HANDLE
     ' Set the cursor of the command. The m_pCursor variable is set
     ' in Class_Initialize
     ITool_Cursor = m_pCursor
 End Property
 
 Private Function ITool_Deactivate() As Boolean
     ' Deactivate the tool. If set to False (the default), you cannot interact
     ' with any other tools because this tool cannot be interrupted by another
     ' tool.
     ITool_Deactivate = True
 End Function
 
 Private Function ITool_OnContextMenu(ByVal X As Long, ByVal Y As Long) As Boolean
     ' Add some code to show a custom context menu when there is a right click.
     ' This example creates a new context menu with one macro item
     Dim pShortCut As ICommandBar
     Dim pitem As ICommandItem
     ' Create a new context menu
     Set pShortCut = m_pApp.Document.CommandBars.Create("MyShortCut", esriCmdBarTypeShortcutMenu)
     ' Add an item to it
     Set pitem = pShortCut.CreateMacroItem("MyMacro", 4)
     ' Display the menu
     pShortCut.Popup
     ' Let the application know that you handled the OnContextMenu event.
     ' If you don't do this, the standard context menu will be displayed after
     ' this custom context menu.
     ITool_OnContextMenu = True
 End Function
 
 Private Sub ITool_OnDblClick()
     ' Add some code to do some action on double-click.
     ' This example makes the builtin Select Graphics Tool the active tool.
     Dim pSelectTool As ICommandItem
     Dim pCommandBars As ICommandBars
     ' The identifier for the Select Graphics Tool
     Dim u As New UID
     u = "\{C22579D1-BC17-11D0-8667-0000F8751720\"
     'Find the Select Graphics Tool
     Set pCommandBars = m_pApp.Document.CommandBars
     Set pSelectTool = pCommandBars.Find(u)
     'Set the current tool of the application to be the Select Graphics Tool
     Set m_pApp.CurrentTool = pSelectTool
 End Sub
 
 Private Sub ITool_OnKeyDown(ByVal keyCode As Long, ByVal Shift As Long)
     ' Add some code to do some action when a keyboard button is pressed.
     ' This example changes the statusbar message.
     m_pApp.StatusBar.Message(0) = "ITool_OnKeyDown"
 End Sub
 
 Private Sub ITool_OnKeyUp(ByVal keyCode As Long, ByVal Shift As Long)
     ' Add some code to do some action when a keyboard button is released.
     ' This example changes the statusbar message.
     m_pApp.StatusBar.Message(0) = "ITool_OnKeyUp"
 End Sub
 
 Private Sub ITool_OnMouseDown(ByVal Button As Long, ByVal Shift As Long, ByVal X As Long, ByVal Y As Long)
     ' Add some code to do some action when the mouse button is pressed.
     ' This example displays the X and Y coordinates of the
     ' left mouse button click in the statusbar message in ArcMap.
     ' Button, X, and Y are passed in as arguments to this sub procedure.
 
     ' Check to see if left button is pressed
     If Button = 1 Then
         ' Convert x and y to map units. pApp is set in ICommand_OnCreate.
         Dim pPoint As IPoint
         Dim pMxApp As IMxApplication
         Set pMxApp = m_pApp
         Set pPoint = pMxApp.Display.DisplayTransformation.ToMapPoint(X, Y)
         ' Set the statusbar message.
         m_pApp.StatusBar.Message(0) = Str(pPoint.X) & "," & Str(pPoint.Y)
     End If
 End Sub
 
 Private Sub ITool_OnMouseMove(ByVal Button As Long, ByVal Shift As Long, ByVal X As Long, ByVal Y As Long)
     ' Add some code to do some action when the mouse is moved.
     ' This example changes the statusbar message.
     m_pApp.StatusBar.Message(0) = "ITool_OnMouseMove"
 End Sub
 
 Private Sub ITool_OnMouseUp(ByVal Button As Long, ByVal Shift As Long, ByVal X As Long, ByVal Y As Long)
     ' Add some code to do some action when the mouse button is released.
     ' This example changes the statusbar message.
     m_pApp.StatusBar.Message(0) = "ITool_OnMouseUp"
 
 End Sub
 
 Private Sub ITool_Refresh(ByVal hDC As esriSystem.OLE_HANDLE)
 
 End Sub 

[Visual Basic .NET, C#, C++]
No example is available for Visual Basic .NET, C#, or C++. To view a Visual Basic 6.0 example, click the Language Filter button Language Filter in the upper-left corner of the page.