Hello experts!
I would like draw a rectangle on images in my program with
DrawFocusRect() function, but something is wrong, because I can't delete the rectangle from its previous place when I have more than one images.
These images are part of an own type:
type Ttiles = class(TCustomimage)
private
public
end;
and a TScrollBox contains them (
tilelist).
I load these images as a follows in a mouse event of a button:
if (imageOpener.Execute) and (numbersourcetiles<255) then
begin
sourcetileimg:=TPicture.Create;
sourcetileimg.LoadFromFile(imageOpener.FileName);
//
setlength(tile,numbersourcetiles+1);
tile[numbersourcetiles]:=Ttiles.Create(tilelist);
tile[numbersourcetiles].Parent:=tilelist;
tile[numbersourcetiles].OnMouseUp:=@srctileclick;
tile[numbersourcetiles].OnPaint:=@tilePaint;
if numbersourcetiles>0 then tile[numbersourcetiles].Top:=tile[numbersourcetiles-1].Top+tile[numbersourcetiles-1].height+2;
tile[numbersourcetiles].width:=sourcetileimg.width;
tile[numbersourcetiles].height:=sourcetileimg.height;
tile[numbersourcetiles].canvas.copyrect(Rect(0,0,tile[numbersourcetiles].width,tile[numbersourcetiles].height),sourcetileimg.bitmap.canvas,Rect(0,0,sourcetileimg.width,sourcetileimg.height));
inc(numbersourcetiles);
freeandnil(sourcetileimg);
end;
This is the
srctileclick function:
procedure TForm1.srctileclick(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var i: byte;
begin
if numbersourcetiles>0 then
begin
for i:=0 to numbersourcetiles-1 do
begin
if (X>=tile[i].Left) and (X<=tile[i].Left+tile[i].width) and ((ScreenToClient(mouse.cursorpos).Y+tilelist.VertScrollBar.Position)-tilelist.Top>=tile[i].Top) and ((ScreenToClient(mouse.cursorpos).Y+tilelist.VertScrollBar.Position)-tilelist.Top<=tile[i].Top+tile[i].height) then
begin
if thereistiletoput=true then
begin
FreeAndNil(tiletoput);
thereistiletoput:=false;
end;
if (GetKeyState(VK_CONTROL) and $80)<>0 then //Assign tile to grid:
begin
X:=tile[i].ScreenToClient(mouse.cursorpos).X-(tile[i].ScreenToClient(mouse.cursorpos).X mod tilewidth);
Y:=tile[i].ScreenToClient(mouse.cursorpos).Y-(tile[i].ScreenToClient(mouse.cursorpos).Y mod tileheight);
end;
tilex:=X;
tiley:=Y;
tiletoput:=Tbitmap.Create;
tiletoput.width:=tilewidth;
tiletoput.height:=tileheight;
tiletoput.canvas.copyrect(Rect(0,0,tilewidth,tileheight),tile[i].canvas,Rect(tilex,tiley,tilex+tilewidth,tiley+tileheight));
thereistiletoput:=true;
actualtileimage:=i;
tile[actualtileimage].invalidate;
Break;
end;
end;
end;
end;
And the
tilePaint function:
procedure TForm1.tilePaint(Sender: TObject);
var fromx,fromy: word;
begin
if thereistiletoput=true then
begin
if (GetKeyState(VK_CONTROL) and $80)<>0 then
begin
fromx:=tilex-(tilex mod tilewidth);
fromy:=tiley-(tiley mod tileheight);
end
else
begin
fromx:=tilex;
fromy:=tiley;
end;
tile[actualtileimage].canvas.drawfocusrect(Rect(fromx,fromy,fromx+tilewidth,fromy+tileheight));
end;
end;
So, I would like put piece of images from an image onto the other's canvas and I would like make visual this piece for the user with this rectangle, but when I clicking the image, many rectangles remain on them.
What should I do? Is it possible to delete those unnecessary rectangles?