AnnMouseDown example for Delphi
//This example shows
//1.
//How to create a custom annotation (circle inside a square)
//Set the AnnTool property to ANN_TOOL_USER_FIRST to see how this works
//2.
//How to restrict the rectangle annotation to be a square
//Set AnnTool property to ANN_TOOL_RECTANGLE to see how this works
var
//Global declarations
RasterAnn: LEADRasterAnnotation;
RasterAnnToolbar: LEADRasterAnnToolBar;
hRectObject: Longint;
hEllipseObject: Longint;
x0: Longint;
y0: Longint;
Procedure AdjustMousePos(Shift: Integer; x: Longint; y: Longint);
var
dx: Integer;
dy: Integer;
begin
if (Shift = 1) then
begin
// if shift key is down, force the creation of squares
dx:= Abs(x - x0);
dy:= Abs(y - y0);
if (dx > dy) then
begin
// adjust y to be as far from y0 as x is from x0
if (y > y0) then
y:= y0 + dx
else
y:= y0 - dx;
end
else
begin
// adjust x to be as far from x0 as y is from y0
if (x > x0) then
x:= x0 + dy
else
x:= x0 - dy;
end;
// set the mouse cursor and update its position
RasterAnn.SetMousePos (x, y, False);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
RasterAnn:= CoLEADRasterAnnotation.Create ();
RasterAnnToolbar:= CoLEADRasterAnnToolBar.Create ();
RasterAnn.AnnParentRasterView := LEADRasterView1.Raster;
LEADEventSink1.Connect (RasterAnn, _LEADRasterAnnotationEvents);
RasterAnn.AnnUserMode:= ANN_USERMODE_DESIGN;
end;
procedure TForm1. LEADEventSink1Invoke(Sender: TObject; DispID: Integer;
const IID: TGUID; LocaleID: Integer; Flags: Word; Params: tagDISPPARAMS;
varResult, ExcepInfo, ArgErr: Pointer);
var
Button: Integer;
Shift: Integer;
x: longint;
y: longint;
begin
case (DispID) of
LEADRASTERANNOTATIONEVENTS_ANNMOUSEDOWN:
begin
x:= OleVariant(Params.rgvarg^[1]);
y:= OleVariant(Params.rgvarg^[0]);
x0:= x;
y0:= y;
Case (RasterAnn.AnnTool) of
ANN_TOOL_USER_FIRST:
begin
//create the objects
RasterAnn.AnnSetSelected (RasterAnn.AnnContainer, False, True);
//Add an undo node to undo the creation of these objects.
RasterAnn.AnnAddUndoNode ();
RasterAnn.AnnCreate (ANN_OBJECT_RECT, True, True);
hRectObject:= RasterAnn.AnnObject;
RasterAnn.AnnCreate (ANN_OBJECT_ELLIPSE, True, True);
hEllipseObject:= RasterAnn.AnnObject;
//set the automation defaults to the objects newly created
RasterAnn.AnnSetAutoDefaults (hRectObject, 0);
RasterAnn.AnnSetAutoDefaults (hEllipseObject, 0);
//start defining them from the x, y coordinate
RasterAnn.AnnDefine (hRectObject, x, y, ANN_DEFINE_BEGINSET);
RasterAnn.AnnDefine (hEllipseObject, x, y, ANN_DEFINE_BEGINSET);
end;
end;
end;
LEADRASTERANNOTATIONEVENTS_ANNMOUSEMOVE:
begin
Button:= OleVariant(Params.rgvarg^[3]);
Shift:= OleVariant(Params.rgvarg^[2]);
x:= OleVariant(Params.rgvarg^[1]);
y:= OleVariant(Params.rgvarg^[0]);
if (Button = 1) then
begin
Case (RasterAnn.AnnTool) of
ANN_TOOL_USER_FIRST:
begin
// update the objects from the x, y coordinate
RasterAnn.AnnDefine (hRectObject, x, y, ANN_DEFINE_UPDATE);
RasterAnn.AnnDefine (hEllipseObject, x, y, ANN_DEFINE_UPDATE);
end;
ANN_TOOL_RECT:
AdjustMousePos (Shift, x, y);
end;
end;
end;
LEADRASTERANNOTATIONEVENTS_ANNMOUSEUP:
begin
Shift:= OleVariant(Params.rgvarg^[2]);
x:= OleVariant(Params.rgvarg^[1]);
y:= OleVariant(Params.rgvarg^[0]);
Case (RasterAnn.AnnTool) of
ANN_TOOL_USER_FIRST:
begin
RasterAnn.AnnDefine (hRectObject, x, y, ANN_DEFINE_END);
RasterAnn.AnnDefine (hEllipseObject, x, y, ANN_DEFINE_END);
RasterAnn.AnnSetSelected (hRectObject, True, False);
RasterAnn.AnnSetSelected (hEllipseObject, True, False);
RasterAnn.AnnGroup (RasterAnn.AnnContainer, ANN_FLAG_RECURSE + ANN_FLAG_SELECTED, '');
hEllipseObject:= 0;
hRectObject:= 0;
end;
ANN_TOOL_RECT:
AdjustMousePos (Shift, x, y);
end;
end;
end;
end;