Loading and Saving Annotation Objects From/To Databases (Delphi 4.0)

Note:

This topic is for Document/Medical only.

Take the following steps to create and run a program that loads and saves Annotation objects from/to a DataBase.

NOTE: You need to create a DataBase file (Annotations.mdb) with the following field to run this tutorial:

FieldName

Type

AnnBlob

OLE Object  

 

1.

Start Delphi.

2.

On the Delphi 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 with this tutorial.

3.

Select the LEAD Annotation control on the VCL toolbar. Size and position the control as you want it to appear at run time.

4.

From the Delphi ADO tab, add an "ADOTable" component to your form.

5.

From the Delphi DataAccess tab, add a "DataSource" component to your form.

6.

Using the Object Inspector, set the following:

LEADAnn1.DataSource = DataSource1
DataSource1.DataSet = ADOTable1

7.

Add six buttons to your form away from the LEADAnn control and name them as follows:

Name

Caption

btnConnect

Connect To DataBase

btnDisconnect

Disconnect From DataBase

btnAdd

Add New Record

btnDelete

Delete Current Record

btnLoadNext

Load Next Record

btnLoadPrevious

Load Previous Record

8.

Handle the Form1 OnCreate event, and code the FormCreate procedure as follows:

procedure TForm1.FormCreate(Sender: TObject);
var
   nRet: Integer;
   strDataBasePath: String;
   strConnectionString: String;
begin
   try
   { Note that these are sample keys, which will not work in your toolkit. }
      LEADAnn1.UnlockSupport(L_SUPPORT_DOCUMENT, 'TestKey');
      LEADAnn1.Load('C:\Program Files\LEAD Technologies, Inc\LEADTOOLS14\Images\Image2.cmp', 0, 1, 1);
      LEADAnn1.AnnUserMode:= ANNUSERMODE_DESIGN;
      nRet:= LEADAnn1.AnnToolbar.CreateTB(0, 0, ANNTOOLALIGN_LEFT + ANNTOOLALIGN_TOP);
      if(nRet = SUCCESS)then
      begin
         LEADAnn1.AnnToolBar.Visible:= True;
         //change the sDBPath to point to your database!
         strDataBasePath := 'C:\Program Files\LEAD Technologies, Inc\LEADTOOLS14\Images\Annotations.mdb';
         strConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + strDataBasePath + ';Persist Security Info=False';
         ADOTable1.ConnectionString:= strConnectionString;
         ADOTable1.TableName:= 'Annotations';
         DataSource1.DataSet:= ADOTable1;
         LEADAnn1.DataSource:= DataSource1;
      end;
      finally
      UpdateButtons();
   end;
end;

9.

Handle the Form1 OnClose event, and code the FormClose procedure as follows:

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   // DeActivate the connection if it is already active.
   if(ADOTable1.State = dsBrowse)then
      ADOTable1.Active:= False;
end;

10.

Handle the btnConnect OnClick event, and code the btnConnectClick procedure as follows:

procedure TForm1.btnConnectClick(Sender: TObject);
begin
   try
      // Activate the connection.
      ADOTable1.Active:= True;
      finally
      UpdateButtons();
   end;
end;

11.

Handle the btnDisconnect OnClick event, and code the btnDisconnectClick procedure as follows:

procedure TForm1.btnDisconnectClick(Sender: TObject);
begin
   try
      // DeActivate the connection.
      if(ADOTable1.State = dsBrowse)then
         ADOTable1.Active:= False;
      finally
      UpdateButtons();
   end;
end;

12.

Handle the btnAdd OnClick event, and code the btnAddClick procedure as follows:

procedure TForm1.btnAddClick(Sender: TObject);
var
   hMem: L_HANDLE;
   phMem: L_PHANDLE;
   uAnnSize: L_INT32;
   annStream: TMemoryStream;
   nSavedBytes: Integer;
begin
   try
      // Add new record
      ADOTable1.Append();
      // Save the current annotations into memory
      hMem:= 0;
      LEADAnn1.AnnSaveMemory(@hMem, ANNFMT_NATIVE, False, @uAnnSize, SAVE_OVERWRITE, 1);
      // Globallock the data in memory.
      phMem:= GlobalLock(hMem);
      if(phMem <> Nil)then
      begin
         // write the Annotation data into a memory stream
         annStream:= TMemoryStream.Create();
         nSavedBytes:= annStream.Write(phMem^, uAnnSize);
         if(nSavedBytes = uAnnSize)then
         begin
            // Save the BLOB into DataBase
            TBlobField(ADOTable1.FieldByName('AnnBlob')).LoadFromStream(annStream);
            ADOTable1.FieldByName('AnnSize').AsInteger:= uAnnSize;
            // Apply the DataBase changes.
            ADOTable1.Post();
         end
         else
            ShowMessage('Error Writing To DataBase');
         // Unlock and Free the Global Data
         GlobalUnlock(hMem);
         GlobalFree(hMem);
      end;
      finally
      UpdateButtons();
   end;
end;

13.

Handle the btnDelete OnClick event, and code the btnDeleteClick procedure as follows:

procedure TForm1.btnDeleteClick(Sender: TObject);
begin
   try
      // Delete the Current Record data.
      ADOTable1.Delete();
      // Load the Annotations for the new active record.
      LoadAnnotations();
      finally
      UpdateButtons();
   end;
end;

14.

Handle the btnloadNext OnClick event, and code the btnLoadNextClick procedure as follows:

procedure TForm1.btnLoadNextClick(Sender: TObject);
begin
   try
      // Move to the next record
      ADOTable1.Next();
      // Load the Annotations for the new active record.
      LoadAnnotations();
      finally
      UpdateButtons();
   end;
end;

15.

Handle the btnLoadPrevious OnClick event, and code the btnLoadPreviousClick procedure as follows:

procedure TForm1.btnLoadPreviousClick(Sender: TObject);
begin
   try
      // Move to the previous record
      ADOTable1.Prior();
      // Load the Annotations for the new active record.
      LoadAnnotations();
      finally
      UpdateButtons();
   end;
end;

16.

Add the following methods declaration to the private section of Form1:

Procedure UpdateButtons();

Procedure LoadAnnotations();

17.

Add the code of UpdateButtons and LoadAnnotations methods to Form1.

Procedure TForm1.UpdateButtons();
begin
   // Enable/Disable the buttons according to the current state.
   btnConnect.Enabled:= Not ADOTable1.Active;
   btnDisconnect.Enabled:= ADOTable1.Active;
   btnAdd.Enabled:= ADOTable1.Active;
   btnDelete.Enabled:= ((ADOTable1.Active) And (ADOTable1.RecordCount > 0));
   btnLoadNext.Enabled:= ((ADOTable1.Active) And (ADOTable1.RecordCount > 0) And (ADOTable1.RecNo + 1 <=    ADOTable1.RecordCount));
   btnLoadPrevious.Enabled:= ((ADOTable1.Active) And (ADOTable1.RecNo - 1 > 0));
end;

 

Procedure TForm1.LoadAnnotations();
var
   annStream: TMemoryStream;
begin
   if(ADOTable1.RecordCount > 0)then
   begin
      // Save the current active record annotations data into MemoryStream.
      annStream:= TMemoryStream.Create();
      TBlobField(ADOTable1.FieldByName('AnnBlob')).SaveToStream(annStream);
      // If the load succeeded
      if(annStream.Size <> 0)then
      begin
         // Load the Annotations into the VCL Annotation control.
         LEADAnn1.AnnLoadMemory(L_HANDLE(GlobalHandle(annStream.Memory)), annstream.Size, 1);
      end
      else
         // Something wrong !
      ShowMessage('Error Reading From DataBase');
   end;
end;

18.

Add "LEADDef" and "LEADTyp" to the uses section of Unit1.

19.

Run your program to test it.