Creating and Using Annotations (C++ Builder 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.

At the beginning of the Unit1.h file, include this file.

#include "LTANNLib_TLB.h"

4.

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.

5.

Open the LEvntSnk.hpp file and delete those lines from the TLEADAbstractEventSink class.

 private:
 void *__IDispatch; /* IDispatch */

public:
 operator IDispatch*(void) { return (IDispatch*)&__IDispatch; }
 operator IUnknown*(void) { return (IUnknown*)&__IDispatch; }
 and save the file.

6.

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

7.

Add the following variable’s decalaration to Unit1.h Private section.

LEADRasterAnnotation * pRasterAnn;
LEADRasterAnnToolBar * pRasAnnToolbar;
int NewTag; 

8.

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

 LEADRasterView1->Raster ->UnlockSupport (L_SUPPORT_DOCUMENT, AnsiToOLESTR("TestKey"));

 CoCreateInstance(CLSID_LEADRasterAnnotation, NULL, CLSCTX_ALL, IID_ILEADRasterAnnotation, (void**)&pRasterAnn);
CoCreateInstance(CLSID_LEADRasterAnnToolBar, NULL, CLSCTX_ALL, IID_ILEADRasterAnnToolBar, (void**)&pRasAnnToolbar);
 //Connect the raster annotation object to the RasterView control
 pRasterAnn->AnnParentRasterView = LEADRasterView1->Raster;


 //Enable left button drawing of annotations
 pRasterAnn->AnnAutoDrawEnable = True;

 //Enable right-click to display annotation menus
 pRasterAnn->AnnAutoMenuEnable = True;
pRasterAnn->AnnUserMode = ANN_USERMODE_DESIGN;
LEADEventSink1->Connect (pRasterAnn, DIID__LEADRasterAnnotationEvents);

9.

Code the Close event’s procedure as follows:

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
 if (pRasterAnn)
 pRasterAnn->Release ();
if (pRasAnnToolbar)
 pRasAnnToolbar-> Release();
}

10.

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

11.

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.

void __fastcall TForm1::AnnToggleClick(TObject *Sender)
{
 //Use the button to toggle between design mode and run mode
 if (AnnToggle->Caption == "Start")
{
 pRasterAnn->AnnUserMode = ANN_USERMODE_DESIGN;
 pRasterAnn->AnnTool = ANN_TOOL_BUTTON;
 //Make run mode the next thing.
 AnnToggle->Caption= "Run Mode";
 ShowMessage ("In design mode now. Draw a button object.");
}
else
{
 if (AnnToggle->Caption == "Design Mode")
{
  pRasterAnn->AnnUserMode= ANN_USERMODE_DESIGN;
  pRasterAnn->AnnTool= ANN_TOOL_BUTTON;
  //Make run mode the next thing.
  AnnToggle->Caption= "Run Mode";
}
else //The button takes us to run mode
{
  pRasterAnn->AnnUserMode = ANN_USERMODE_RUN;
  ShowMessage ("Click on your new button");
  AnnToggle->Caption= "Design Mode";
  }
}
}

12.

Code the IOToggle control's Click procedure as follows:

void __fastcall TForm1::IOToggleClick(TObject *Sender)
{
 int nRet;

 //Disable errors so that we can trap our own.
 pRasterAnn->EnableMethodErrors = False;
 //Use the button to toggle between saving and loading annotations
 if (IOToggle->Caption == "Save")
 {
 //Do nothing if there are no annotations.
if (pRasterAnn->AnnContainer == 0)
 return;
//Save the all annotations.
nRet = pRasterAnn->AnnSave (AnsiToOLESTR("c:\\temp\\tmp.ann"), ANN_FMT_NATIVE, False, SAVE_OVERWRITE, 1);
if (nRet == 0)
{
 IOToggle->Caption = "Load";
ShowMessage ("Use the right mouse button to delete any annotations, then click Load");
}
else
 ShowMessage ("Error code: " + IntToStr(nRet));
}
 else
{
 //We are loading annotations
 //Make sure we are in design mode
 pRasterAnn->AnnUserMode = ANN_USERMODE_DESIGN;
//Load the annotations.
 nRet = pRasterAnn->AnnLoad (AnsiToOLESTR("c:\\temp\\tmp.ann"), 1);
if (nRet == 0)
 IOToggle->Caption = "Save";
else
 ShowMessage ("No annotations to load");
}
}

13.

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

void __fastcall TForm1::ClipboardToggleClick(TObject *Sender)
{
 int nRet;

 //Disable errors so that we can trap our own.
 pRasterAnn->EnableMethodErrors = False;
 //Use the button to toggle between copying and pasting annotations
 if (ClipboardToggle->Caption == "Copy")
{
 //Do nothing if there are no annotations.
 if (pRasterAnn->AnnContainer == 0)
 return;
 //Copy all annotations to the clipboard.
 nRet = pRasterAnn->AnnCopy (ANN_FMT_NATIVE, False, True);
 if (nRet == 0)
{
 //Make pasting the next thing.
 ClipboardToggle->Caption = "Paste";
 ShowMessage ("Use the right mouse button to delete any annotations, then click Paste");
}
 else
 ShowMessage ("Error code: " + IntToStr(nRet));
}
 else //We are pasting annotations
{
 //Make sure we are in design mode
 pRasterAnn->AnnUserMode = ANN_USERMODE_DESIGN;
 //Paste the annotations
 if (pRasterAnn->AnnPasteReady)
{
 pRasterAnn->AnnPaste ();
 ClipboardToggle->Caption = "Copy";
}
 else
 ShowMessage ("No annotations to paste");
 }
}

14.

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

void __fastcall TForm1::RealizeClick(TObject *Sender)
{
 pRasterAnn->AnnRealize (false);
 LEADRasterView1->ForceRepaint ();
 ShowMessage ("Annotations are now rendered to the bitmap");
}

15.

Code the LEADEventSink1 OnInvoke event as follows:

void __fastcall TForm1::LEADEventSink1Invoke (TObject *Sender, int DispID,
const TGUID &IID, int LocaleID, WORD Flags, tagDISPPARAMS &Params,
Pointer varResult, Pointer ExcepInfo, Pointer ArgErr)
{
 long lTag;
 int hObject;
 AnnObjectType ObjectType;

 switch (DispID)
{

 case LEADRASTERANNOTATIONEVENTS_ANNCREATE:
{
 hObject= (OleVariant)(Params.rgvarg[0]);
   //Update the tag if we need one.
   pRasterAnn->AnnGetType (hObject);
   ObjectType= pRasterAnn-> get_AnnType_( );
   if ((ObjectType == ANN_OBJECT_BUTTON) || (ObjectType == ANN_OBJECT_HOTSPOT))
{
 NewTag++;
    pRasterAnn->AnnSetTag (hObject, NewTag);
}
}
break;

case LEADRASTERANNOTATIONEVENTS_ANNDESTROY:
{
 hObject= (OleVariant)(Params.rgvarg[0]);
 pRasterAnn->AnnGetTag (hObject);
  lTag= pRasterAnn->get_AnnTag ( );
  ShowMessage ("The object tagged " + IntToStr((int)lTag) + " is deleted.");
}
break;

case LEADRASTERANNOTATIONEVENTS_ANNCLICKED:
{
 hObject= (OleVariant)(Params.rgvarg[0]);
 pRasterAnn->AnnGetTag (hObject);
  lTag= pRasterAnn->get_AnnTag ( );
  ShowMessage ("This is what we do for the button tagged " + IntToStr((int)lTag));
}
break;

 case LEADRASTERANNOTATIONEVENTS_ANNDRAWN:
{
 pRasterAnn->AnnTool = ANN_TOOL_SELECT;
ShowMessage ("Use the right mouse button to modify this object; then click on Run Mode to test it");
  }
}
}

16.

Run your program to test it.