selection rectangle (FMX, Win32)

  

I’m building an FMX app that has a TImage on it. At runtime the user can place rectangles on the TImage and move them around. Now i want to add the ability for the user to drag a selection rectangle to select multiple rectangles at once. I found this code but it has a method that seems to be depracated now:

Canvas->DrawFocusRect(DrawRect); // DrawRect is a TRect

This line of code gives be the following error:

[bcc32 Error] Unit1.cpp(28): E2316 ‘DrawFocusRect’ is not a member of ‘TCanvas’

I’m using C++ Builder 10.2 Update 3 and have the classic compiler selected. Is there a replacement for DrawFocusRect or a better way to implement a selection rectangle?

UPDATE 1: Ok, I’m getting the rectangle now but it leaves rectangles as it grows. In this screen snag i started at top left with mouse-down and let it up at bottom right.

How do i get rid of those prior rectangles? Here is my code so far:

//This is MouseDown event
Dragging = true; // Dragging is a bool defined in the class definition
DrawRect.left = X; // DrawRect is a TRect defined in the class private section
DrawRect.top = Y;
DrawRect.right = X;
DrawRect.bottom = Y;
Canvas->BeginScene();
Canvas->DrawDashRect(DrawRect, 0, 0, AllCorners, 0.5, TAlphaColor(TAlphaColorRec::Green));
Canvas->EndScene();

//This is MouseMove event
if (Dragging) {
Canvas->BeginScene();
Canvas->DrawDashRect(DrawRect, 0, 0, AllCorners, 0.5, TAlphaColor(TAlphaColorRec::Green));
Canvas->EndScene();
DrawRect.right = X;
DrawRect.bottom = Y;
Canvas->BeginScene();
Canvas->DrawDashRect(DrawRect, 0, 0, AllCorners, 0.5, TAlphaColor(TAlphaColorRec::Green));
Canvas->EndScene();
}

//This is MouseUp event
if (Dragging) {
Dragging = false;
Canvas->BeginScene();
Canvas->DrawDashRect(DrawRect, 0, 0, AllCorners, 0.5, TAlphaColor(TAlphaColorRec::Green));
Canvas->EndScene();
}

UPDATE 2: I am able to get rid of the prior rectangles now using Canvas->Clear(claWhite). The problem now is that this whites-out the entire form (all my controls are gone). So, i tried putting a Rectangle1 on my form and draw my selection rectangle on it. Rectangle1 is aligned to client (Form1), sent to back, Disabled, Opacity = 0, and Visible = false. So why does the following code still white-out everything and draw the rectangle? Note – the code is in the Image1 events, so maybe it is getting confused and drawing on Image1 instead of Rectangle1?

void __fastcall TForm1::Image1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
float X, float Y)
{
Dragging = true;
DrawRect.left = X;
DrawRect.top = Y;
DrawRect.right = X;
DrawRect.bottom = Y;
Rectangle1->Canvas->BeginScene();
Rectangle1->Canvas->DrawDashRect(DrawRect, 0, 0, AllCorners, 0.5, TAlphaColor(TAlphaColorRec::Green));
Rectangle1->Canvas->EndScene();
}

void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift, float X,
float Y)
{
if (Dragging) {
Rectangle1->Canvas->BeginScene();
Rectangle1->Canvas->Clear(claWhite);
Rectangle1->Canvas->DrawDashRect(DrawRect, 0, 0, AllCorners, 0.5, TAlphaColor(TAlphaColorRec::Green));
Rectangle1->Canvas->EndScene();
DrawRect.right = X;
DrawRect.bottom = Y;
Rectangle1->Canvas->BeginScene();
Rectangle1->Canvas->DrawDashRect(DrawRect, 0, 0, AllCorners, 0.5, TAlphaColor(TAlphaColorRec::Green));
Rectangle1->Canvas->EndScene();
}
}

void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
float X, float Y)
{
if (Dragging) {
Dragging = false;
Rectangle1->Canvas->BeginScene();
Rectangle1->Canvas->Clear(claWhite);
Rectangle1->Canvas->DrawDashRect(DrawRect, 0, 0, AllCorners, 0.5, TAlphaColor(TAlphaColorRec::Green));
Rectangle1->Canvas->EndScene();
ShowMessage(“DrawRect: X = ” + FloatToStr(DrawRect.Location.X) + “, Y = ” + FloatToStr(DrawRect.Location.Y)
+ “\n” + IntToStr(DrawRect.Width()) + “, height = ” + IntToStr(DrawRect.Height())) ;
}
}

Here is short video showing this for clarity.

Comments are closed.