Creating and Using Annotations

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 C++ Builder.

2.

In the File menu, choose New Application.

3.

On the Builder toolbar, click the LEADTOOLS tab. If you have used a LEAD VCL control before, the icon appears on the toolbar. Otherwise, refer to Installing VCL before continuing this tutorial.

4.

Select the LEADAnn control on the VCL toolbar. Size and position the Control, as you want it to appear at run time.

5.

Add a command button to your form and name it as follows:

 

Name

Caption

LoadLEAD

Load Image

 

6.

Code the main form's FormShow procedure as follows. In online help, you can copy the block of code and paste it into your application.

void __fastcall TForm1::FormShow(TObject *Sender) 
{
  // Unlock GIF format support. 
  // Note that, this is a sample key, which will not work in your toolkit. 
  LEADAnn1->UnlockSupport (L_SUPPORT_DOCUMENT, "TestKey");
  LEADAnn1->AnnUserMode= ANNUSERMODE_NONE; 
  // Set defaults for displaying the image. 
  // These are all persistent properties that can be set in the properties box.
  LEADAnn1->BorderStyle = bsSingle; 
  LEADAnn1->BackColor = (TColor) RGB (255, 255, 125); 
  LEADAnn1->PaintDither = pdDiffusion; 
  LEADAnn1->PaintPalette = ppAuto; 
  LEADAnn1->AutoRepaint = True; 
  LEADAnn1->AutoSize = False; 
  LEADAnn1->AutoSetRects = True; 
  LEADAnn1->PaintSizeMode = smFit; 
}

7.

Code the LoadLEAD button's click procedure as follows:

void __fastcall TForm1::LoadLEADClick(TObject *Sender) 
{
   LEADAnn1->Load ("D:\\pictures\\Test.cmp", 0, 0, 1);
}

8.

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

 

9.

Code the AnnToggle control's Click procedure as follows.

void __fastcall TForm1::AnnToggleClick(TObject *Sender) 
{
   //Use the button to toggle between design mode and run mode
   if ( AnnToggle->Caption == "Start" ) 
   {
      LEADAnn1->AnnUserMode = ANNUSERMODE_DESIGN; 
      LEADAnn1->AnnTool= ANNTOOL_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" ) 
      {
          LEADAnn1->AnnUserMode = ANNUSERMODE_DESIGN; 
          LEADAnn1->AnnTool = ANNTOOL_BUTTON; 
          //Make run mode the next thing. 
          AnnToggle->Caption= "Run Mode";
      }
      else
      {
         //The button takes us to run mode
          LEADAnn1->AnnUserMode = ANNUSERMODE_RUN; 
          ShowMessage ( "Click on your new button" ) ; 
          AnnToggle->Caption= "Design Mode";
      }
   }
}

10.

Code the IOToggle control's Click procedure as follows:

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

   //Disable errors so that we can trap our own->
   LEADAnn1->EnableMethodErrors = false; 
   //Use the button to toggle between saving and loading annotations
     if (IOToggle->Caption == "Save" ) 
   {
      //Do nothing if there are no annotations. 
      if (LEADAnn1->AnnContainer == 0) 
         return ; 
      //Save the selected annotations. 
      nRet= LEADAnn1->AnnSave ("D:\\pictures\\tmp.ann", ANNFMT_NATIVE, true, SAVE_OVERWRITE, 0); 
      nRet= LEADAnn1->AnnSave ("D:\\pictures\\tmp.ann", ANNFMT_NATIVE, true); 
      if (nRet == SUCCESS ) 
      {
          //Make loading the next thing. 
          IOToggle->Caption= "Load";
          ShowMessage ( "Use the right mouse button to delete any annotations, then click Load" ) ; 
      }
      else
      {
         Msg= "Error code: " + IntToStr(nRet); 
         ShowMessage ( Msg ) ; 
      }
   }
     else
   {
      //We are loading annotations
      //Make sure we are in design mode
      LEADAnn1->AnnUserMode = ANNUSERMODE_DESIGN; 
      //Load the annotations. 
      nRet= LEADAnn1->AnnLoad("D:\\Pictures\\tmp.ann", 1); 
      if (nRet == SUCCESS) 
          IOToggle->Caption= "Save";
      else
          ShowMessage ( "No annotations to load" ) ; 
     }
}

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

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

   //Disable errors so that we can trap our own. 
   LEADAnn1->EnableMethodErrors = False;
   //Use the button to toggle between copying and pasting annotations
   if (ClipboardToggle->Caption == "Copy")
   {
      //Do nothing if there are no annotations. 
       if ( LEADAnn1->AnnContainer == 0 ) 
         return ; 
       //Copy the selected annotations to the clipboard. 
       nRet= LEADAnn1->AnnCopy (ANNFMT_NATIVE, True, True); 
       if ( nRet == SUCCESS ) 
      {

        //Make pasting the next thing. 
         ClipboardToggle->Caption= "Paste";
           ShowMessage ( "Use the right mouse button to delete any annotations, then click Paste" ); 
      }
      else
      {
         Msg= "Error code: " + IntToStr(nRet); 
         ShowMessage ( Msg ); 
      }

   }
   else
   {
      //We are pasting annotations
       //Make sure we are in design mode
       LEADAnn1->AnnUserMode = ANNUSERMODE_DESIGN; 
       //Paste the annotations
       if ( LEADAnn1->AnnPasteReady ) 
      {
         LEADAnn1->AnnPaste ();
           ClipboardToggle->Caption= "Copy";
      }
       else
         ShowMessage ( "No annotations to paste" ) ; 
   }
}

12.

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

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

13.

Add the following declaration to the private section of Form1

   int NewTag;

14.

Code the LEADAnn1 control's OnAnnCreate event as follows:

void __fastcall TForm1::LEADAnn1AnnCreate (L_HANDLE hObject) 
{
   //Update the tag if we need one. 
   if ((LEADAnn1->AnnGetType (hObject)== ANNOBJECT_BUTTON )|| 
       (LEADAnn1->AnnGetType (hObject)== ANNOBJECT_HOTSPOT)) 
   {
      NewTag ++;
       LEADAnn1->AnnSetTag (hObject, NewTag); 
   }
}

15.

Code the LEADAnn1 control's OnAnnDrawn event as follows:

void __fastcall TForm1::LEADAnn1AnnDrawn (L_HANDLE hObject) 
{
   LEADAnn1->AnnTool = ANNTOOL_SELECT; 
   ShowMessage ( "Use the right mouse button to modify this object; then click on Run Mode to test it." ) ; 
}

16.

Code the LEADAnn1 control's OnAnnClicked event as follows:

void __fastcall TForm1::LEADAnn1AnnClicked (L_HANDLE hObject) 
{
   AnsiString Msg; 

   Msg= "This is what we do for the button tagged " + IntToStr (LEADAnn1->AnnGetTag(hObject)) ; 
   ShowMessage (Msg); 
}

17.

Code the LEADAnn1 control's OnAnnDestroy Event as follows:

void __fastcall TForm1::LEADAnn1AnnDestroy (L_HANDLE hObject) 
{
   AnsiString Msg; 
   Msg= "The object tagged " + IntToStr (LEADAnn1->AnnGetTag (hObject)) + " is deleted."; 
   ShowMessage (Msg); 
}

18.

Run your program to test it.