Recent

Author Topic: Problem with drawing and deleting rectangle  (Read 174 times)

Tomi

  • Full Member
  • ***
  • Posts: 130
Problem with drawing and deleting rectangle
« on: November 06, 2024, 03:32:24 pm »
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:

Code: Pascal  [Select][+][-]
  1. type Ttiles = class(TCustomimage)
  2.   private
  3.   public
  4. end;

and a TScrollBox contains them (tilelist).
I load these images as a follows in a mouse event of a button:

Code: Pascal  [Select][+][-]
  1. if (imageOpener.Execute) and (numbersourcetiles<255) then
  2. begin
  3.      sourcetileimg:=TPicture.Create;
  4.      sourcetileimg.LoadFromFile(imageOpener.FileName);
  5.      //
  6.      setlength(tile,numbersourcetiles+1);
  7.      tile[numbersourcetiles]:=Ttiles.Create(tilelist);
  8.      tile[numbersourcetiles].Parent:=tilelist;
  9.      tile[numbersourcetiles].OnMouseUp:=@srctileclick;
  10.      tile[numbersourcetiles].OnPaint:=@tilePaint;
  11.      if numbersourcetiles>0 then tile[numbersourcetiles].Top:=tile[numbersourcetiles-1].Top+tile[numbersourcetiles-1].height+2;
  12.      tile[numbersourcetiles].width:=sourcetileimg.width;
  13.      tile[numbersourcetiles].height:=sourcetileimg.height;
  14.      tile[numbersourcetiles].canvas.copyrect(Rect(0,0,tile[numbersourcetiles].width,tile[numbersourcetiles].height),sourcetileimg.bitmap.canvas,Rect(0,0,sourcetileimg.width,sourcetileimg.height));
  15.      inc(numbersourcetiles);
  16.      freeandnil(sourcetileimg);
  17. end;

This is the srctileclick function:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.srctileclick(Sender: TObject;
  2.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  3. var i: byte;
  4. begin
  5.   if numbersourcetiles>0 then
  6.   begin
  7.     for i:=0 to numbersourcetiles-1 do
  8.     begin
  9.          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
  10.          begin
  11.             if thereistiletoput=true then
  12.             begin
  13.                  FreeAndNil(tiletoput);
  14.                  thereistiletoput:=false;
  15.             end;
  16.             if (GetKeyState(VK_CONTROL) and $80)<>0 then //Assign tile to grid:
  17.             begin
  18.                  X:=tile[i].ScreenToClient(mouse.cursorpos).X-(tile[i].ScreenToClient(mouse.cursorpos).X mod tilewidth);
  19.                  Y:=tile[i].ScreenToClient(mouse.cursorpos).Y-(tile[i].ScreenToClient(mouse.cursorpos).Y mod tileheight);
  20.             end;
  21.             tilex:=X;
  22.             tiley:=Y;
  23.             tiletoput:=Tbitmap.Create;
  24.             tiletoput.width:=tilewidth;
  25.             tiletoput.height:=tileheight;
  26.             tiletoput.canvas.copyrect(Rect(0,0,tilewidth,tileheight),tile[i].canvas,Rect(tilex,tiley,tilex+tilewidth,tiley+tileheight));
  27.             thereistiletoput:=true;
  28.             actualtileimage:=i;
  29.             tile[actualtileimage].invalidate;
  30.             Break;
  31.          end;
  32.     end;
  33.   end;
  34. end;

And the tilePaint function:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.tilePaint(Sender: TObject);
  2. var fromx,fromy: word;
  3. begin
  4.   if thereistiletoput=true then
  5.   begin
  6.      if (GetKeyState(VK_CONTROL) and $80)<>0 then
  7.      begin
  8.          fromx:=tilex-(tilex mod tilewidth);
  9.          fromy:=tiley-(tiley mod tileheight);
  10.      end
  11.      else
  12.      begin
  13.          fromx:=tilex;
  14.          fromy:=tiley;
  15.      end;
  16.      tile[actualtileimage].canvas.drawfocusrect(Rect(fromx,fromy,fromx+tilewidth,fromy+tileheight));
  17.   end;
  18. 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?
« Last Edit: November 06, 2024, 03:34:17 pm by Tomi »

 

TinyPortal © 2005-2018