Hello,
I need to know exactly where the block I am inserting into the diagram is going to be placed.
To do this, I have created a "ghost" block (named Contour) with the same dimensions as the block that is going to be inserted, which moves with the "mouse move" event of the diagram and disappears when the insertion is done.
It works approximately, but not very well (the block is often inserted within one or two grid steps).
Here is the code :
void __fastcall TForm1::FormCreate(TObject *Sender)
{ Contour = new TDiagramBlock(atDiagram1);
Contour->Width = 50;
Contour->Height = 50;
Contour->Pen->Style = psDot;
Contour->Shape = bsRectangle;
Contour->Transparent = true;
Contour->Visible = false;
Contour->Diagram = atDiagram1;
}
void __fastcall TForm1::atDiagram1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
if(atDiagram1->MouseState==msToInsert){
TCustomDiagramBlock *Bloc= dynamic_cast <TCustomDiagramBlock *> (atDiagram1->InsertDControl);
if(Bloc!=NULL){
Contour->Width=Bloc->Width;
Contour->Height=Bloc->Height;
Contour->Visible=true;
int top = (Y+atDiagram1->VertScrollBar->Position)/atDiagram1->ScreenDPIRatio/atDiagram1->ZoomRatio;
int left= (X+atDiagram1->HorzScrollBar->Position)/atDiagram1->ScreenDPIRatio/atDiagram1->ZoomRatio;
Contour->Top= round(round(top / atDiagram1->SnapGrid->SizeY) * atDiagram1->SnapGrid->SizeY);
Contour->Left=round(round(left / atDiagram1->SnapGrid->SizeX) * atDiagram1->SnapGrid->SizeX);
}
else{
Contour->Visible=false;
}
}
else{
Contour->Visible=false;
}
}
Is there a way to improve this? Should I do otherwise?
Thank you.