How to precisely know the insertion position of a block

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.

You can use something like this:

void __fastcall TForm3::atDiagram1MouseMove(TObject *Sender, TShiftState Shift, int X,
		  int Y)
{
	if((atDiagram1->MouseState==msToInsert) || (atDiagram1->MouseState==msInserting)){
		TCustomDiagramBlock *Bloc= dynamic_cast <TCustomDiagramBlock *> (atDiagram1->InsertDControl);
		if(Bloc!=NULL){
			Contour->Width=Bloc->Width;
			Contour->Height=Bloc->Height;
			Contour->Visible=true;
			TDot dot = atDiagram1->CanvasToClient(atDiagram1->SnapCanvasPoint(Dot(X, Y)));
			Contour->Top = dot.Y;
			Contour->Left = dot.X;
		}
		else{
			Contour->Visible=false;
		}
	}
	else{
		Contour->Visible=false;
	}
}

Yes, thanks, the code is simpler but the problem of insertion within one or two grid steps remains.

That is a bug we have solved and the fix will be included in next release.

OK, Thanks.

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.