Creating and Using Annotations (Delphi 6.0)

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.

On the Project pull-down menu, use the Import Type library… and select the LEAD Raster Annotation Library (14.5). Press OK.

3.

Select LEAD Raster Annotation, from the ActiveX tab and add it to your form.

4.

Declare the following variable in the private section of Unit1.

NewTag: Integer;

5.

Add the following code to the btnLoadImage click’s procedure.

   //Unlock annotation support
   LEADRasterView1.Raster.UnlockSupport (L_SUPPORT_DOCUMENT, WideString('TestKey')); 

   //Connect the raster annotation object to the RasterView control
   LEADRasterAnnotation1.AnnParentRasterView:= LEADRasterView1.Raster

   //Enable left button drawing of annotations
   LEADRasterAnnotation1.AnnAutoDrawEnable:= True; 

   //Enable right-click to display annotation menus
   LEADRasterAnnotation1.AnnAutoMenuEnable:= True; 

6.

At the top of your main form, add four buttons and name them as follows:

 

Name

Caption

 

btnAnnToggle

Start

 

btnIOToggle

Save

 

btnClipboardToggle

Copy

 

btnRealize

Realize

7.

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

procedure TForm1.btnAnnToggleClick(Sender: TObject);
begin
   //Use the button to toggle between design mode and run mode
   if (btnAnnToggle.Caption = 'Start') then
   begin
       LEADRasterAnnotation1.AnnUserMode:= ANN_USERMODE_DESIGN; 
       LEADRasterAnnotation1.AnnTool:= ANN_TOOL_BUTTON; 
       //Make run mode the next thing. 
       btnAnnToggle.Caption:= 'Run Mode'; 
       ShowMessage ('In design mode now. Draw a button object.'); 
   end
   else
   begin
      if (btnAnnToggle.Caption = 'Design Mode') then
      begin
          LEADRasterAnnotation1.AnnUserMode:= ANN_USERMODE_DESIGN; 
          LEADRasterAnnotation1.AnnTool:= ANN_TOOL_BUTTON; 
          //Make run mode the next thing. 
          btnAnnToggle.Caption:= 'Run Mode'; 
      end
      else //The button takes us to run mode
      begin
          LEADRasterAnnotation1.AnnUserMode:= ANN_USERMODE_RUN; 
          ShowMessage ('Click on your new button'); 
          btnAnnToggle.Caption:= 'Design Mode'; 
      end; 
   end; 
end; 

8.

Code the btnIOToggle control's Click procedure as follows:

procedure TForm1.btnIOToggleClick(Sender: TObject); 
var
   nRet: Integer; 
begin
   //Disable errors so that we can trap our own. 
   LEADRasterAnnotation1.EnableMethodErrors := False; 
   //Use the button to toggle between saving and loading annotations
   if (btnIOToggle.Caption = 'Save') then
   begin
      //Do nothing if there are no annotations. 
      If (LEADRasterAnnotation1.AnnContainer = 0) then
         Exit; 
      //Save all annotations. 
      nRet := LEADRasterAnnotation1.AnnSave ('c:\temp\tmp.ann', ANN_FMT_NATIVE, False, SAVE_OVERWRITE, 1); 
      if (nRet = 0) then
      begin
         btnIOToggle.Caption := 'Load'; 
         ShowMessage ('Use the right mouse button to delete any annotations, then click Load'); 
      end
      else
         ShowMessage ('Error code: ' + IntToStr(nRet)); 
   end
   else
   begin
      //We are loading annotations
      //Make sure we are in design mode
      LEADRasterAnnotation1.AnnUserMode := ANN_USERMODE_DESIGN; 
      //Load the annotations. 
        nRet := LEADRasterAnnotation1.AnnLoad ('c:\temp\tmp.ann', 1); 
      if (nRet = 0) then
         btnIOToggle.Caption := 'Save'
      else
         ShowMessage ('No annotations to load'); 
   end; 
end; 

9.

Code the btnClipboardToggle 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.)

procedure TForm1.btnClipboardToggleClick(Sender: TObject); 
var
   nRet: Integer; 
begin
   //Disable errors so that we can trap our own. 
   LEADRasterAnnotation1.EnableMethodErrors := False; 
   //Use the button to toggle between copying and pasting annotations
   if (btnClipboardToggle.Caption = 'Copy') then
   begin
      //Do nothing if there are no annotations. 
       If (LEADRasterAnnotation1.AnnContainer = 0) then
         Exit; 
       //Copy all annotations to the clipboard. 
       nRet := LEADRasterAnnotation1.AnnCopy (ANN_FMT_NATIVE, False, True); 
       if (nRet = 0) then
      begin
         //Make pasting the next thing. 
           btnClipboardToggle.Caption := 'Paste'; 
           ShowMessage ('Use the right mouse button to delete any annotations, then click Paste'); 
      end
      else
         ShowMessage ('Error code: ' + IntToStr(nRet)); 
   end
   else //We are pasting annotations
   begin
      //Make sure we are in design mode
       LEADRasterAnnotation1.AnnUserMode := ANN_USERMODE_DESIGN; 
       //Paste the annotations
       if ( LEADRasterAnnotation1.AnnPasteReady ) then
      begin
         LEADRasterAnnotation1.AnnPaste
           btnClipboardToggle.Caption := 'Copy'; 
      end
       else
         ShowMessage ('No annotations to paste'); 
   end; 
end; 

10.

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

procedure TForm1.btnRealizeClick(Sender: TObject); 
begin
   LEADRasterAnnotation1.AnnRealize (False); 
   LEADRasterView1.ForceRepaint ();
   ShowMessage ('Annotations are now rendered to the bitmap'); 
end; 

11.

Handle the LEADRasterAnnotation1 OnAnnCreate event, and code the LEADRasterAnnotation1AnnCreate procedure as follows:

procedure TForm1.LEADRasterAnnotation1AnnCreate (Sender: TObject; 
  hAnnObject: Integer); 
var
   ObjectType: AnnObjectType; 
begin
   //Update the tag if we need one. 
   LEADRasterAnnotation1.AnnGetType (hAnnObject);
   ObjectType:= LEADRasterAnnotation1.AnnType;
   if ((ObjectType = ANN_OBJECT_BUTTON) Or (ObjectType = ANN_OBJECT_HOTSPOT)) then
   begin
      NewTag:= NewTag + 1; 
      LEADRasterAnnotation1.AnnSetTag (hAnnObject, NewTag); 
   end; 
end; 

12.

Handle the LEADRasterAnnotation1 OnAnnDestroy event, and code the LEADRasterAnnotation1AnnDestroy procedure as follows:

procedure TForm1.LEADRasterAnnotation1AnnDestroy(Sender: TObject;
  hAnnObject: Integer); 
var
   lTag: Integer; 
begin
   LEADRasterAnnotation1.AnnGetTag (hAnnObject);
   lTag:= LEADRasterAnnotation1.AnnTag;
   ShowMessage ('The object tagged ' + IntToStr(lTag) + ' is deleted.'); 
end; 

13.

Handle the LEADRasterAnnotation1 OnAnnClicked event, and code the LEADRasterAnnotation1AnnClicked procedure as follows:

procedure TForm1.LEADRasterAnnotation1AnnClicked(Sender: TObject; 
  hAnnObject: Integer); 
var
   lTag: Integer; 
begin
   LEADRasterAnnotation1.AnnGetTag (hAnnObject);
  lTag:= LEADRasterAnnotation1.AnnTag; 
   ShowMessage ('This is what we do for the button tagged ' + IntToStr(lTag)); 
end; 

14.

Handle the LEADRasterAnnotation1 OnAnnDrawn event, and code the LEADRasterAnnotation1AnnDrawn procedure as follows:

procedure TForm1.LEADRasterAnnotation1AnnDrawn(Sender: TObject; 
  hAnnObject: Integer); 
begin
   LEADRasterAnnotation1.AnnTool := ANN_TOOL_SELECT; 
   ShowMessage ('Use the right mouse button to modify this object; then click on Run Mode to test it.'); 
end; 

15.

Run your program to test it.