AnnMouseDown example for C++ Builder
//Global declarations
LEADRasterAnnotation * pRasterAnn= NULL;
LEADRasterAnnToolBar* pRasterAnnToolbar= NULL;
long hRectObject;
long hEllipseObject;
long x0;
long y0;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
CoCreateInstance(CLSID_LEADRasterAnnotation, NULL, CLSCTX_ALL, IID_ILEADRasterAnnotation, (void**)&pRasterAnn);
CoCreateInstance(CLSID_LEADRasterAnnToolBar, NULL, CLSCTX_ALL, IID_ILEADRasterAnnToolBar, (void**)&pRasterAnnToolbar);
pRasterAnn->AnnParentRasterView = LEADRasterView1->Raster;
LEADEventSink1->Connect (pRasterAnn, DIID__LEADRasterAnnotationEvents);
pRasterAnn->AnnUserMode = ANN_USERMODE_DESIGN ;
}
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if (pRasterAnn)
pRasterAnn->Release ();
if (pRasterAnnToolbar)
pRasterAnnToolbar-> Release();
}
/*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*/
void AdjustMousePos(int Shift, long x, long y)
{
int dx;
int dy;
if (Shift == 1)
{
// if shift key is down, force the creation of squares
dx = abs(x - x0);
dy = abs(y - y0);
if (dx > dy)
{
// adjust y to be as far from y0 as x is from x0
if (y > y0)
y = y0 + dx;
else
y = y0 - dx;
}
else
{
// adjust x to be as far from x0 as y is from y0
if (x > x0)
x = x0 + dy;
else
x = x0 - dy;
}
// set the mouse cursor and update its position
pRasterAnn->SetMousePos (x, y, false);
}
}
void __fastcall TForm1:: LEADEventSink1Invoke(TObject *Sender, int DispID,
const TGUID &IID, int LocaleID, WORD Flags, tagDISPPARAMS &Params,
Pointer varResult, Pointer ExcepInfo, Pointer ArgErr)
{
switch (DispID)
{
case LEADRASTERANNOTATIONEVENTS_ANNMOUSEDOWN:
{
int x = (OleVariant)(Params.rgvarg[1]);
int y = (OleVariant)(Params.rgvarg[0]);
x0 = x;
y0 = y;
switch (pRasterAnn->AnnTool)
{
case ANN_TOOL_USER_FIRST:
{
//create the objects
pRasterAnn->AnnSetSelected (pRasterAnn->AnnContainer, false, true);
//Add an undo node to undo the creation of these objects.
pRasterAnn->AnnAddUndoNode ();
pRasterAnn->AnnCreate (ANN_OBJECT_RECT, true, true);
hRectObject= pRasterAnn->AnnObject;
pRasterAnn->AnnCreate (ANN_OBJECT_ELLIPSE, true, true);
hEllipseObject= pRasterAnn->AnnObject;
// set the automation defaults to the objects newly created
pRasterAnn->AnnSetAutoDefaults (hRectObject, 0);
pRasterAnn->AnnSetAutoDefaults (hEllipseObject, 0);
// start defining them from the x, y coordinate
pRasterAnn->AnnDefine (hRectObject, x, y, ANN_DEFINE_BEGINSET);
pRasterAnn->AnnDefine (hEllipseObject, x, y, ANN_DEFINE_BEGINSET);
}
break;
}
}
break;
case LEADRASTERANNOTATIONEVENTS_ANNMOUSEMOVE:
{
int Button = (OleVariant)(Params.rgvarg[3]);
int Shift = (OleVariant)(Params.rgvarg[2]);
int x = (OleVariant)(Params.rgvarg[1]);
int y = (OleVariant)(Params.rgvarg[0]);
if (Button == 1)
{
switch (pRasterAnn->AnnTool)
{
case ANN_TOOL_USER_FIRST:
{
//update the objects from the x, y coordinate
pRasterAnn->AnnDefine (hRectObject, x, y, ANN_DEFINE_UPDATE);
pRasterAnn->AnnDefine (hEllipseObject, x, y, ANN_DEFINE_UPDATE);
}
break;
case ANN_TOOL_RECT:
AdjustMousePos (Shift, x, y);
break;
}
}
}
break;
case LEADRASTERANNOTATIONEVENTS_ANNMOUSEUP:
{
int Shift = (OleVariant)(Params.rgvarg[2]);
int x = (OleVariant)(Params.rgvarg[1]);
int y = (OleVariant)(Params.rgvarg[0]);
switch (pRasterAnn->AnnTool)
{
case ANN_TOOL_USER_FIRST:
{
pRasterAnn->AnnDefine (hRectObject, x, y, ANN_DEFINE_END);
pRasterAnn->AnnDefine (hEllipseObject, x, y, ANN_DEFINE_END);
pRasterAnn->AnnSetSelected (hRectObject, true, false);
pRasterAnn->AnnSetSelected (hEllipseObject, true, false);
pRasterAnn->AnnGroup (pRasterAnn->AnnContainer, ANN_FLAG_RECURSE + ANN_FLAG_SELECTED, AnsiToOLESTR(""));
hEllipseObject = 0;
hRectObject = 0;
}
break;
case ANN_TOOL_RECT:
AdjustMousePos (Shift, x, y);
break;
}
}
break;
}
}