Creating and Using Annotations (Delphi 4.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), and Press Ok.

3.

If you didn’t install LEADEventSink install it.

 

On the Component pull-down menu, use install component …, and browse to LEADXX\Include\LEvntSnk.pas, and press Ok.

4.

From the ActiveX controls, Add the LEvntSnk control to your form.

5.

Add the following variable’s declaration to the Form1 Private section.

   RasterAnn: LEADRasterAnnotation;
   RasAnnToolbar: LEADRasterAnnToolBar;
   NewTag: Integer;

6.

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

      RasterAnn:= CoLEADRasterAnnotation.Create();
     RasAnnToolbar:= CoLEADRasterAnnToolBar.Create();
   LEADEventSink1.Connect (RasterAnn, _LEADRasterAnnotationEvents);
     //Unlock annotation support
     LEADRasterView1.Raster.UnlockSupport (L_SUPPORT_DOCUMENT, WideString('TestKey'));

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

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

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

7.

At the beginning of the Unit1 file, add LTANNLib_TLB, ActiveX to the uses section. For example:

Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, LTANNLib_TLB;

8.

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

 

Name

Caption

 

AnnToggle

Start

 

IOToggle

Save

 

ClipboardToggle

Copy

 

Realize

Realize

9.

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.

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

10.

Code the IOToggle control's Click procedure as follows:

procedure TForm1.IOToggleClick(Sender: TObject);
var
   nRet: Integer;
begin
   //Disable errors so that we can trap our own.
   RasterAnn.EnableMethodErrors := False;
   //Use the button to toggle between saving and loading annotations
     if (IOToggle.Caption = 'Save') then
   begin
      //Do nothing if there are no annotations.
      If (RasterAnn.AnnContainer = 0) then
         Exit;
      //Save the all annotations.
      nRet := RasterAnn.AnnSave ('c:\temp\tmp.ann', ANN_FMT_NATIVE, False, SAVE_OVERWRITE, 1);

      if (nRet = 0) then
      begin
         IOToggle.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
      RasterAnn.AnnUserMode := ANN_USERMODE_DESIGN;
      //Load the annotations.
        nRet := RasterAnn.AnnLoad ('c:\temp\tmp.ann', 1);
      if (nRet = 0) then
         IOToggle.Caption := 'Save'
      else
         ShowMessage ('No annotations to load');
   end;
end;

11.

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.)

procedure TForm1.ClipboardToggleClick(Sender: TObject);
var
   nRet: Integer;
begin
   //Disable errors so that we can trap our own.
   RasterAnn.EnableMethodErrors := False;
   //Use the button to toggle between copying and pasting annotations
   if (ClipboardToggle.Caption = 'Copy') then
   begin
      //Do nothing if there are no annotations.
       If (RasterAnn.AnnContainer = 0) then
         Exit;
       //Copy all annotations to the clipboard.
       nRet := RasterAnn.AnnCopy (ANN_FMT_NATIVE, False, True);
       if (nRet = 0) then
      begin
         //Make pasting the next thing.
           ClipboardToggle.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
       RasterAnn.AnnUserMode := ANN_USERMODE_DESIGN;
       //Paste the annotations
       if RasterAnn.AnnPasteReady then
      begin
         RasterAnn.AnnPaste;
           ClipboardToggle.Caption := 'Copy';
      end
       else
         ShowMessage ('No annotations to paste');
   end;
end;

12.

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

procedure TForm1.RealizeClick(Sender: TObject);
var
   sRet: Smallint;
begin
   RasterAnn.AnnRealize (False);
   LEADRasterView1.ForceRepaint (sRet);
   ShowMessage ('Annotations are now rendered to the bitmap');
end;

13.

Code the LEADEventSink1 OnInvoke event as follows:

procedure TForm1.LEADEventSink1Invoke(Sender: TObject; DispID: Integer;
  const IID: TGUID; LocaleID: Integer; Flags: Word; Params: tagDISPPARAMS;
  varResult, ExcepInfo, ArgErr: Pointer);
var
   lTag: Longint;
   hObject: Integer;
   ObjectType: AnnObjectType;
begin
   hObject:= OleVariant(Params.rgvarg^[0]);
   case (DispID) of
      LEADRASTERANNOTATIONEVENTS_ANNCREATE: 
      begin
         //Update the tag if we need one.
         RasterAnn.AnnGetType (hObject);
         ObjectType:= RasterAnn.AnnType;
         if ((ObjectType = ANN_OBJECT_BUTTON) Or (ObjectType = ANN_OBJECT_HOTSPOT)) then
         begin
            NewTag:= NewTag + 1;
            RasterAnn.AnnSetTag (hObject, NewTag);
         end;
      end;
      LEADRASTERANNOTATIONEVENTS_ANNDESTROY: 
      begin
         RasterAnn.AnnGetTag (hObject);
      lTag:= RasterAnn.AnnTag;
         ShowMessage ('The object tagged ' + IntToStr(lTag) + ' is deleted.');
      end;


      LEADRASTERANNOTATIONEVENTS_ANNCLICKED:
      begin
         RasterAnn.AnnGetTag (hObject);
      lTag:= RasterAnn.AnnTag;
         ShowMessage ('This is what we do for the button tagged ' + IntToStr(lTag));
      end;

      LEADRASTERANNOTATIONEVENTS_ANNDRAWN:
      begin
         RasterAnn.AnnTool := ANNTOOL_SELECT;
         ShowMessage ('Use the right mouse button to modify this object; then click on Run Mode to test it.');
      end;
   end;
end;

14.

Run your program to test it.