Creating and Using Annotations (Visual Basic)

Note: This topic is for Document/Medical only.

Take the following steps to add code that demonstrates the creation and deletion, saving and loading, and copying and pasting of annotation objects. This code also demonstrates the related events. Message boxes prompt for user actions that trigger events.

1. Start with the project that you created in Loading and Displaying an Image.

2. image\btncmd.gif At the top of your main form, add four command buttons and name them as follows:

Name

Caption

AnnToggle

Start

IOToggle

Save

ClipboardToggle Copy

Realize Realize

3. Code the AnnToggle control's Click procedure as follows. In online help, you can use the Edit pull-down menu to copy the block of code.

Private Sub AnnToggle_Click()

'Use the button to toggle between design mode and run mode
If AnnToggle.Caption = "Start" Then
    Lead1.AnnUserMode = ANNUSERMODE_DESIGN
    Lead1.AnnTool = ANNTOOL_BUTTON
    'Make run mode the next thing.
    AnnToggle.Caption = "Run Mode"
    MsgBox "In design mode now. Draw a button object."
ElseIf AnnToggle.Caption = "Design Mode" Then
    Lead1.AnnUserMode = ANNUSERMODE_DESIGN
    Lead1.AnnTool = ANNTOOL_BUTTON
    'Make run mode the next thing.
    AnnToggle.Caption = "Run Mode"
Else 'The button takes us to run mode
    Lead1.AnnUserMode = ANNUSERMODE_RUN
    MsgBox "Click on your new button"
    AnnToggle.Caption = "Design Mode"
End If

End Sub

4. Code the IOToggle control's Click procedure as follows:

Private Sub IOToggle_Click()

Dim nRet As Integer
'Disable errors so that we can trap our own.
Lead1.EnableMethodErrors = False

'Use the button to toggle between saving and loading annotations
If IOToggle.Caption = "Save" Then
    'Do nothing if there are no annotations.
    If Lead1.AnnContainer = 0 Then Exit Sub
    'Save the selected annotations.
    nRet = Lead1.AnnSave("c:\lead\images\tmp.ann", ANNFMT_NATIVE, True, SAVE_OVERWRITE, 0)
    If nRet = 0 Then
        'Make loading the next thing.
        IOToggle.Caption = "Load"
        MsgBox "Use the right mouse button to delete any annotations, then click Load"
    Else
        MsgBox "Error code: " + Str(nRet)
    End If
Else 'We are loading annotations
    'Make sure we are in design mode
    Lead1.AnnUserMode = ANNUSERMODE_DESIGN
    'Load the annotations.
    nRet = Lead1.AnnLoad("c:\lead\images\tmp.ann", 1)
    If nRet = 0 Then
        IOToggle.Caption = "Save"
    Else
        MsgBox "No annotations to load"
    End If
End If
End Sub

5. Code the ClipboardToggle control's Click procedure as follows. (Note that Copy, Cut, and Paste are available as automated popup menu options. This code is for tailoring your application.)

Private Sub ClipboardToggle_Click()

Dim nRet As Integer
'Disable errors so that we can trap our own.
Lead1.EnableMethodErrors = False

'Use the button to toggle between copying and pasting annotations
If ClipboardToggle.Caption = "Copy" Then
    'Do nothing if there are no annotations.
    If Lead1.AnnContainer = 0 Then Exit Sub 

    'Copy the selected annotations to the clipboard.
    nRet = Lead1.AnnCopy(ANNFMT_NATIVE, True, True)
    If nRet = 0 Then
        'Make pasting the next thing.
        ClipboardToggle.Caption = "Paste"
        MsgBox "Use the right mouse button to delete any annotations, then click Paste"
   Else
        MsgBox "Error code: " + Str(nRet)
    End If
Else 'We are pasting annotations
    'Make sure we are in design mode
    Lead1.AnnUserMode = ANNUSERMODE_DESIGN
    'Paste the annotations
    If Lead1.AnnPasteReady Then
        Lead1.AnnPaste
        ClipboardToggle.Caption = "Copy"
    Else
        MsgBox "No annotations to paste"
    End If
End If

End Sub

6. Code the Realize control's Click procedure (which renders the annotation objects to the bitmap) as follows:

Private Sub Realize_Click()
Lead1.AnnRealize False
Lead1.ForceRepaint
MsgBox "Annotations are now rendered to the bitmap"
End Sub

7. Code the LEAD1 control's AnnCreate procedure as follows:

Private Sub Lead1_AnnCreate(ByVal hObject As OLE_HANDLE)
' Update the tag if we need one.
Static NewTag As Integer
If Lead1.AnnGetType(hObject) = ANNOBJECT_BUTTON Or Lead1.AnnGetType(hObject) = ANNOBJECT_HOTSPOT Then
    NewTag = NewTag + 1
    Lead1.AnnSetTag hObject, NewTag
End If

End Sub

8. Code the LEAD1 control's AnnDrawn procedure as follows:

Private Sub Lead1_AnnDrawn(ByVal hObject As OLE_HANDLE)
Lead1.AnnTool = ANNTOOL_SELECT
MsgBox "Use the right mouse button to modify this object; then click on Run Mode to test it."

End Sub

9. Code the LEAD1 control's AnnClicked procedure as follows:

Private Sub Lead1_AnnClicked(ByVal hObject As OLE_HANDLE)
    MsgBox "This is what we do for the button tagged " + Str(Lead1.AnnGetTag(hObject))
End Sub

10. Code the LEAD1 control's AnnDestroy procedure as follows:

Private Sub Lead1_AnnDestroy(ByVal hObject As OLE_HANDLE)
MsgBox "The object tagged " + Str(Lead1.AnnGetTag(hObject)) + " is deleted."
End Sub

11. Run your program to test it.