Creating and Using Annotations (Delphi 4.0)
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 Delphi. |
2. |
In the File menu, choose New Application. |
3. |
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. |
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 the Windows-based help file, you can copy the block of code and paste it into your application. |
procedure TForm1.FormShow(Sender: TObject);
begin
{ Unlock DOCUMENT support. }
{ Note that these are sample keys, 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 := RGB(255, 255, 125);
LEADAnn1.PaintDither := pdDiffusion;
LEADAnn1.PaintPalette := ppAuto;
LEADAnn1.AutoRepaint := True;
LEADAnn1.AutoSize := False;
LEADAnn1.AutoSetRects := True;
LEADAnn1.PaintSizeMode := smFit;
end;
7. |
Code the LoadLead button's click procedure as follows: |
procedure TForm1.LeadLoadClick (Sender: TObject);
begin
LEADAnn1.Load ('D:\pictures\test.cmp', 0, 0, 1);
end;
8. |
At the beginning of the Unit1 file, add LEADUNT, LEADTYP to the uses section. In order to use the constants in Delphi, you must add the LEADDEF unit to the uses section. For example: |
|
uses |
|
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, LEADVCL, StdCtrls, LEADDEF, LEADTYP |
9. |
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 |
10. |
Code the AnnToggle control's Click procedure as follows. |
procedure TForm1.AnnToggleClick (Sender: TObject);
begin
//Use the button to toggle between design mode and run mode
if ( AnnToggle.Caption = 'Start' ) then
begin
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.') ;
end
else
begin
if ( AnnToggle.Caption = 'Design Mode' ) then
begin
LEADAnn1.AnnUserMode:= ANNUSERMODE_DESIGN;
LEADAnn1.AnnTool:= ANNTOOL_BUTTON;
//Make run mode the next thing.
AnnToggle.Caption:= 'Run Mode';
end
else
begin
//The button takes us to run mode
LEADAnn1.AnnUserMode:= ANNUSERMODE_RUN;
ShowMessage ( 'Click on your new button' ) ;
AnnToggle.Caption:= 'Design Mode';
end;
end;
end;
11. |
Code the IOToggle control's Click procedure as follows: |
procedure TForm1.IOToggleClick(Sender: TObject);
var
nRet: Integer;
Msg: String;
begin
//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' ) then
begin
//Do nothing if there are no annotations.
if LEADAnn1.AnnContainer = 0 then
Exit ;
//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 )then
begin
//Make loading the next thing.
IOToggle.Caption:= 'Load';
ShowMessage ( 'Use the right mouse button to delete any annotations, then click Load' ) ;
end
else
begin
Msg:= 'Error code: ' + IntToStr(nRet);
ShowMessage ( Msg ) ;
end;
end
else
begin
//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) then
IOToggle.Caption:= 'Save'
else
ShowMessage ( 'No annotations to load' ) ;
end;
end;
12. |
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;
Msg: String;
begin
//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') then
begin
//Do nothing if there are no annotations.
if ( LEADAnn1.AnnContainer = 0 ) then
Exit;
//Copy the selected annotations to the clipboard.
nRet:= LEADAnn1.AnnCopy(ANNFMT_NATIVE, True, True);
if ( nRet = SUCCESS ) 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
begin
Msg:= 'Error code: ' + IntToStr(nRet);
ShowMessage ( Msg );
end;
end
else
begin
//We are pasting annotations
//Make sure we are in design mode
LEADAnn1.AnnUserMode:= ANNUSERMODE_DESIGN;
//Paste the annotations
if ( LEADAnn1.AnnPasteReady ) then
begin
LEADAnn1.AnnPaste ( ) ;
ClipboardToggle.Caption:= 'Copy';
end
Else
ShowMessage ( 'No annotations to paste' ) ;
end;
end;
13. |
Code the Realize control's Click procedure (which renders the annotation objects to the bitmap) as follows: |
procedure TForm1.RealizeClick(Sender: TObject);
begin
LEADAnn1.AnnRealize (False);
LEADAnn1.ForceRepaint ();
ShowMessage ('Annotations are now rendered to the bitmap');
End;
14. |
Add the following declaration to the private section of Form1 |
NewTag: Integer;
15. |
Code the LEADAnn1 control's OnAnnCreate event as follows: |
procedure TForm1.LEADAnn1AnnCreate (hObject: L_HANDLE);
begin
//Update the tag if we need one.
if ((LEADAnn1.AnnGetType (hObject) = ANNOBJECT_BUTTON )Or
(LEADAnn1.AnnGetType (hObject) = ANNOBJECT_HOTSPOT)) then
begin
NewTag:= NewTag + 1;
LEADAnn1.AnnSetTag ( hObject, NewTag ) ;
end;
end;
16. |
Code the LEADAnn1 control's OnAnnDrawn event as follows: |
procedure TForm1.LEADAnn1AnnDrawn (hObject: L_HANDLE);
begin
LEADAnn1.AnnTool:= ANNTOOL_SELECT;
ShowMessage ( 'Use the right mouse button to modify this object; then click on Run Mode to test it.' ) ;
end;
17. |
Code the LEADAnn1 control's OnAnnClicked event as follows: |
procedure TForm1.LEADAnn1AnnClicked(hObject: L_HANDLE);
var
Msg: String;
begin
Msg:= 'This is what we do for the button tagged ' + IntToStr(LEADAnn1.AnnGetTag (hObject)) ;
ShowMessage (Msg);
end;
18. |
Code the LEADAnn1 control's OnAnnDestroy Event as follows: |
procedure TForm1.LEADAnn1AnnDestroy(hObject: L_HANDLE);
var
Msg: String;
begin
Msg:= 'The object tagged ' + IntToStr (LEADAnn1.AnnGetTag (hObject)) + ' is deleted.';
ShowMessage (Msg);
end;
19. |
Run your program to test it. |