Recent

Author Topic: Problem with MouseDown event on a PaintBox  (Read 1973 times)

Tomi

  • Full Member
  • ***
  • Posts: 102
Problem with MouseDown event on a PaintBox
« on: April 01, 2022, 10:47:05 am »
Hello!

I found a big problem recently: I have two PaintBoxes on the main Form and I would like allow to user to draw on it with the mouse.
But I realized that the real mouse coordintes are much bigger than the coordinates of this second blue PaintBox so the MouseDown event never executed when I press the left mouse button.
I don't know, how MouseDown perceives clicks? I thought, it happens automatically - or not? Should I calculate the real coordinates and tell this to MouseDown somehow?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormResize(Sender: TObject);
  2. var ablakxszazalek,ablakyszazalek: real;
  3. begin
  4.   Kep3d.left:=8;
  5.   Kep3d.top:=8;
  6.   ablakxszazalek:=(100*Form1.width)/regiform1szel; //Resizing PaintBoxes with correct ratio
  7.   ablakyszazalek:=(100*Form1.height)/regiform1mag; //windowypercent := (100*Form1.height)/oldform1height
  8.   Kep3d.width:=round((regikep3dszel*ablakxszazalek)/100);
  9.   Kep3d.height:=round((regikep3dmag*ablakyszazalek)/100);
  10.   //
  11.   Alaprajz.left:=Kep3d.left+Kep3d.width+8;
  12.   Alaprajz.top:=Kep3d.top;
  13.   Alaprajz.width:=Kep3d.width;
  14.   Alaprajz.height:=Kep3d.height;
  15.   //
  16.   regiform1szel:=Form1.width;
  17.   regiform1mag:=Form1.height;
  18.   regikep3dszel:=Kep3d.width;
  19.   regikep3dmag:=Kep3d.height;
  20. end;
  21.  
  22. procedure TForm1.Kep3dPaint(Sender: TObject);
  23. begin
  24.   Kep3d.color:=clGreen;
  25.   Kep3d.canvas.fillrect(0,0,Kep3d.width,Kep3d.height);
  26. end;
  27.  
  28. procedure TForm1.FormCreate(Sender: TObject);
  29. begin
  30.   regiform1szel:=Form1.width;
  31.   regiform1mag:=Form1.height;
  32.   regikep3dszel:=Kep3d.width;
  33.   regikep3dmag:=Kep3d.height;
  34.   targy2ddb:=0;
  35. end;
  36.  
  37. procedure TForm1.AlaprajzPaint(Sender: TObject);
  38. var i: integer;
  39.   j:byte;
  40. begin
  41.   Alaprajz.color:=clBlue;
  42.   Alaprajz.canvas.fillrect(0,0,Alaprajz.width,Alaprajz.height);
  43.   Alaprajz.canvas.pen.color:=clBlack;
  44.   Alaprajz.canvas.pen.width:=1;
  45.   for i:=0 to targy2ddb-1 do
  46.   begin
  47.     for j:=0 to a2dtargyak[i].pontjaidb-1 do
  48.     begin
  49.       Alaprajz.canvas.MoveTo(a2dtargyak[i].pontjaitomb[j,0],a2dtargyak[i].pontjaitomb[j,2]);
  50.       if j<a2dtargyak[i].pontjaidb-1 then
  51.         Alaprajz.canvas.LineTo(a2dtargyak[i].pontjaitomb[j+1,0],a2dtargyak[i].pontjaitomb[j+1,2])
  52.       else
  53.         Alaprajz.canvas.LineTo(a2dtargyak[i].pontjaitomb[0,0],a2dtargyak[i].pontjaitomb[0,2]);
  54.     end;
  55.   end;
  56. end;
  57.  
  58. procedure TForm1.AlaprajzMouseDown(Sender: TObject; Button: TMouseButton;
  59.   Shift: TShiftState; X, Y: Integer);
  60. begin
  61.   egy2dtargylerako('kocka'); //This function creates a draw
  62.   if (X>Alaprajz.left) and (X<Alaprajz.left+Alaprajz.width) then Alaprajz.canvas.textout(1,1,'KATT!!!');
  63.   //with Alaprajz.canvas do invalidate;
  64. end;
« Last Edit: April 07, 2022, 09:19:17 am by Tomi »

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Problem with MouseDown event on a PaintBox
« Reply #1 on: April 01, 2022, 01:10:44 pm »
X and Y in the mouse events are relative to the control which handles them. Unfortunately I do not understand your variable names. But assuming that "Alaprajz" is one of the paintboxes then you should not consider Alaprajz.Left in the coordinated calculations because the mouse coordinate X has its zero at the left side of the paintbox.

So instead of
Code: Pascal  [Select][+][-]
  1. if (X>Alaprajz.left) and (X<Alaprajz.left+Alaprajz.width) then Alaprajz.canvas.textout(1,1,'KATT!!!');
do
Code: Pascal  [Select][+][-]
  1. if (X>0) and (X<Alaprajz.width) then Alaprajz.canvas.textout(1,1,'KATT!!!');

Another issue is that you do not store the start and end points of the lines to be drawn. They are determined by OnMouseDown and OnMouseUp and used in the OnPaint event to draw the lines.

Tomi

  • Full Member
  • ***
  • Posts: 102
Re: Problem with MouseDown event on a PaintBox
« Reply #2 on: April 02, 2022, 11:12:25 am »
Thanks, Wp, it works! Now, I begin to understand the working of coordiantes of different forms.
I just have to determine the exact place of the coordinates when the user resize the PaintBox.

Tomi

  • Full Member
  • ***
  • Posts: 102
Re: Problem with MouseDown event on a PaintBox
« Reply #3 on: April 03, 2022, 09:47:23 am »
One more question: where should I put the Invalidate command when I draw on PaintBox?
Just because when I put it on FormPaint or AlaprajzPaint, I can't close the program with its close button and the screen are flickering.
Placed it elsewhere (e.g.: MouseDown) nothing happen. How can I solve this problem?

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Problem with MouseDown event on a PaintBox
« Reply #4 on: April 03, 2022, 12:21:41 pm »
You call Paintbox.Invalidate whenever you want the paintbox to be updated, for example in the OnMouseDown, OnMouseMove and OnMouseUp events of the paintbox. NEVER call it from the painting routine itself because this creates an endless loop: While painting another paint request is made, when this is handled yet another paint request is made, etc...

Maybe the attached simple demo project helps you to understand how this works. Simple press the left mouse button in the gray area (the paintbox), hold it down and drag it around. You'll see a line between the mouse-down position and the current position of the mouse. These coordinates are stored, and the Paintbox.OnPaint draws this line between them. Repainting (by Paintbox1.Invalidate) is requested in Paintbox.OnMouseDown (activate the FDragging flag; store the coordinates of the starting point; erase the old image), Paintbox.MouseMove (if FDragging is set draw the line), and Paintbox.OnMouseUp (reset the FDragging flag).

Tomi

  • Full Member
  • ***
  • Posts: 102
Re: Problem with MouseDown event on a PaintBox
« Reply #5 on: April 04, 2022, 01:40:49 pm »
Ok, I understand that I should not put it in any painting event. But something is still wrong with my program, although I just use Invalidate at MouseDown - but where else?
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Kep3d: TPaintBox;
  16.     Alaprajz: TPaintBox;
  17.     procedure AlaprajzMouseDown(Sender: TObject; Button: TMouseButton;
  18.       Shift: TShiftState; X, Y: Integer);
  19.     procedure AlaprajzPaint(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure FormPaint(Sender: TObject);
  22.     procedure FormResize(Sender: TObject);
  23.     procedure Kep3dPaint(Sender: TObject);
  24.   private
  25.  
  26.   public
  27.  
  28.   end;
  29.  
  30.   type T2dtargyak = class(TObject)
  31.   private
  32.   public
  33.     pontjaitomb: array [0..7,0..2] of integer;
  34.     pontjaidb: byte;
  35.     neve: string;
  36.   end;
  37.  
  38. var
  39.   Form1: TForm1;
  40.   regiform1szel,regiform1mag,regikep3dszel,regikep3dmag: word;
  41.   targy2ddb: integer;
  42.   a2dtargyak: array of T2dtargyak;
  43.  
  44. implementation
  45.  
  46. {$R *.lfm}
  47.  
  48. { TForm1 }
  49.  
  50. procedure egy2dtargylerako(nev: string; xh: integer; yh: integer);
  51. begin
  52.   setlength(a2dtargyak,targy2ddb+1);
  53.   a2dtargyak[targy2ddb]:=T2dtargyak.create;
  54.   a2dtargyak[targy2ddb].neve:=nev;
  55.   case nev of
  56.   'kocka': begin //These are the coordinates of the cube (kocka)
  57.        a2dtargyak[targy2ddb].pontjaidb:=8;
  58.        a2dtargyak[targy2ddb].pontjaitomb[0,0]:=xh; //1. point (bottom left corner down)
  59.        a2dtargyak[targy2ddb].pontjaitomb[0,1]:=yh; //Y, height now
  60.        a2dtargyak[targy2ddb].pontjaitomb[0,2]:=yh; //Z
  61.        a2dtargyak[targy2ddb].pontjaitomb[1,0]:=a2dtargyak[targy2ddb].pontjaitomb[0,0]+14; //2. point (bottom right corner down)
  62.        a2dtargyak[targy2ddb].pontjaitomb[1,1]:=a2dtargyak[targy2ddb].pontjaitomb[0,1];
  63.        a2dtargyak[targy2ddb].pontjaitomb[1,2]:=a2dtargyak[targy2ddb].pontjaitomb[0,2];
  64.        a2dtargyak[targy2ddb].pontjaitomb[2,0]:=a2dtargyak[targy2ddb].pontjaitomb[0,0]+14; //4. point (upper right corner down)
  65.        a2dtargyak[targy2ddb].pontjaitomb[2,1]:=a2dtargyak[targy2ddb].pontjaitomb[0,1];
  66.        a2dtargyak[targy2ddb].pontjaitomb[2,2]:=a2dtargyak[targy2ddb].pontjaitomb[0,2]-14;
  67.        a2dtargyak[targy2ddb].pontjaitomb[3,0]:=a2dtargyak[targy2ddb].pontjaitomb[0,0]; //3. point (upper left corner down)
  68.        a2dtargyak[targy2ddb].pontjaitomb[3,1]:=a2dtargyak[targy2ddb].pontjaitomb[0,1];
  69.        a2dtargyak[targy2ddb].pontjaitomb[3,2]:=a2dtargyak[targy2ddb].pontjaitomb[0,2]-14;
  70.        a2dtargyak[targy2ddb].pontjaitomb[4,0]:=a2dtargyak[targy2ddb].pontjaitomb[0,0]; //5. point (bottom left corner up)
  71.        a2dtargyak[targy2ddb].pontjaitomb[4,1]:=a2dtargyak[targy2ddb].pontjaitomb[0,1]+14;
  72.        a2dtargyak[targy2ddb].pontjaitomb[4,2]:=a2dtargyak[targy2ddb].pontjaitomb[0,2];
  73.        a2dtargyak[targy2ddb].pontjaitomb[5,0]:=a2dtargyak[targy2ddb].pontjaitomb[0,0]+14; //6. point (bottom right corner up)
  74.        a2dtargyak[targy2ddb].pontjaitomb[5,1]:=a2dtargyak[targy2ddb].pontjaitomb[0,1]+14;
  75.        a2dtargyak[targy2ddb].pontjaitomb[5,2]:=a2dtargyak[targy2ddb].pontjaitomb[0,2];
  76.        a2dtargyak[targy2ddb].pontjaitomb[6,0]:=a2dtargyak[targy2ddb].pontjaitomb[0,0]+14; //8. point (upper right corner up)
  77.        a2dtargyak[targy2ddb].pontjaitomb[6,1]:=a2dtargyak[targy2ddb].pontjaitomb[0,1]+14;
  78.        a2dtargyak[targy2ddb].pontjaitomb[6,2]:=a2dtargyak[targy2ddb].pontjaitomb[0,2]-14;
  79.        a2dtargyak[targy2ddb].pontjaitomb[7,0]:=a2dtargyak[targy2ddb].pontjaitomb[0,0]; //7. point (upper left corner up)
  80.        a2dtargyak[targy2ddb].pontjaitomb[7,1]:=a2dtargyak[targy2ddb].pontjaitomb[0,1]+14;
  81.        a2dtargyak[targy2ddb].pontjaitomb[7,2]:=a2dtargyak[targy2ddb].pontjaitomb[0,2]-14;
  82.     end;
  83.   end;
  84.   inc(targy2ddb,1);
  85. end;
  86.  
  87. procedure TForm1.FormResize(Sender: TObject);
  88. var ablakxszazalek,ablakyszazalek: real;
  89.   i: integer;
  90.   j: byte;
  91. begin
  92.   Kep3d.left:=8;
  93.   Kep3d.top:=8;
  94.   //Kep3d.width:=regikep3dszel*round(Form1.width/regiform1szel);
  95.   //Kep3d.height:=regikep3dmag*round(Form1.height/regiform1mag);
  96.   ablakxszazalek:=(100*Form1.width)/regiform1szel;
  97.   ablakyszazalek:=(100*Form1.height)/regiform1mag;
  98.   Kep3d.width:=round((regikep3dszel*ablakxszazalek)/100);
  99.   Kep3d.height:=round((regikep3dmag*ablakyszazalek)/100);
  100.   //
  101.   Alaprajz.left:=Kep3d.left+Kep3d.width+8;
  102.   Alaprajz.top:=Kep3d.top;
  103.   Alaprajz.width:=Kep3d.width;
  104.   Alaprajz.height:=Kep3d.height;
  105.   //
  106.   regiform1szel:=Form1.width;
  107.   regiform1mag:=Form1.height;
  108.   regikep3dszel:=Kep3d.width;
  109.   regikep3dmag:=Kep3d.height;
  110.   //
  111.   for i:=0 to targy2ddb-1 do
  112.   begin //Recalculating the coordinates of 2D object when resizing window:
  113.     for j:=0 to a2dtargyak[i].pontjaidb-1 do
  114.     begin
  115.       a2dtargyak[i].pontjaitomb[j,0]:=round((a2dtargyak[i].pontjaitomb[j,0]*ablakxszazalek)/100);
  116.       a2dtargyak[i].pontjaitomb[j,1]:=round((a2dtargyak[i].pontjaitomb[j,1]*ablakyszazalek)/100);
  117.       a2dtargyak[i].pontjaitomb[j,2]:=round((a2dtargyak[i].pontjaitomb[j,2]*ablakyszazalek)/100);
  118.     end;
  119.   end;
  120. end;
  121.  
  122. procedure TForm1.Kep3dPaint(Sender: TObject);
  123. begin
  124.   Kep3d.color:=clGreen;
  125.   Kep3d.canvas.fillrect(0,0,Kep3d.width,Kep3d.height);
  126. //This PaintBox will draw the 3D cube later.
  127. end;
  128.  
  129. procedure TForm1.FormCreate(Sender: TObject);
  130. begin
  131.   regiform1szel:=Form1.width; //Old Form1 width
  132.   regiform1mag:=Form1.height; //and height.
  133.   regikep3dszel:=Kep3d.width;
  134.   regikep3dmag:=Kep3d.height;
  135.   targy2ddb:=0;
  136. end;
  137.  
  138. procedure TForm1.AlaprajzPaint(Sender: TObject);
  139. var i: integer;
  140.   j:byte;
  141. begin
  142.   Alaprajz.color:=clBlue;
  143.   Alaprajz.canvas.fillrect(0,0,Alaprajz.width,Alaprajz.height);
  144.   Alaprajz.canvas.pen.color:=clBlack;
  145.   Alaprajz.canvas.pen.width:=2;
  146.   for i:=0 to targy2ddb-1 do
  147.   begin
  148.     for j:=0 to a2dtargyak[i].pontjaidb-1 do
  149.     begin
  150.       if j<a2dtargyak[i].pontjaidb-1 then
  151.         canvas.Line(Alaprajz.left+a2dtargyak[i].pontjaitomb[j,0],Alaprajz.top+a2dtargyak[i].pontjaitomb[j,2],Alaprajz.left+a2dtargyak[i].pontjaitomb[j+1,0],Alaprajz.top+a2dtargyak[i].pontjaitomb[j+1,2])
  152.       else
  153.         canvas.Line(Alaprajz.left+a2dtargyak[i].pontjaitomb[j,0],Alaprajz.top+a2dtargyak[i].pontjaitomb[j,2],Alaprajz.left+a2dtargyak[i].pontjaitomb[0,0],Alaprajz.top+a2dtargyak[i].pontjaitomb[0,2]);
  154.     end;
  155.   end;
  156. end;
  157.  
  158. procedure TForm1.AlaprajzMouseDown(Sender: TObject; Button: TMouseButton;
  159.   Shift: TShiftState; X, Y: Integer);
  160. begin
  161.   if (X>0) and (X<Alaprajz.width) and (Y>0) and (Y<Alaprajz.height) then
  162.   begin
  163.     egy2dtargylerako('kocka',X,Y); //Here we place a 2D rectangle:
  164.     Alaprajz.invalidate;
  165.   end;
  166. end;
  167.  
  168. procedure TForm1.FormPaint(Sender: TObject);
  169. var pt: Tpoint;
  170. begin
  171.   pt:=ScreenToClient(mouse.cursorpos);
  172.   canvas.textout(1,Form1.height-20,inttostr(Form1.width)+'-'+inttostr(Alaprajz.left)+'-'+inttostr(mouse.cursorpos.X)+'-'+inttostr(pt.X)+'-'+inttostr(targy2ddb));
  173. end;
  174.  
  175. end.
  176.  

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Problem with MouseDown event on a PaintBox
« Reply #6 on: April 04, 2022, 04:51:50 pm »
I tried to construct a compilable project from this, and it did not work - maybe because of your bug, or because I did something wrong when copying the code. Please understand: you must post compilable code along with non-trivial questions. I simply do not understand what is going on, just from reading the code, in particular if I don't understand the identifiers.

So, please pack the .pas, .lfm, .lpi and .lpr files to a common zip (no compiler-created files please), and upload them via "Attachment and other options". And you would help me if you'd translate your identifiers to English.

Tomi

  • Full Member
  • ***
  • Posts: 102
Re: Problem with MouseDown event on a PaintBox
« Reply #7 on: April 05, 2022, 02:59:51 pm »
Okay, here is my compressed program (except project1.exe). The main problem is that I don't know which event is the suitable for Invalidate command to make visible the drawing on PaintBox named "Alaprajz" (blue rectangle).
If you put Invalidate in Form1.Paint, you will see the black rectangles after clicking on Alaprajz, but it is not the good solution.

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Problem with MouseDown event on a PaintBox
« Reply #8 on: April 05, 2022, 04:26:29 pm »
Thank you for not translating the identifiers. I am as stupid as before. I have no idea what this means, what this should do, what I should do. Would you understand this program? Sorry, I cannot help you.

Tomi

  • Full Member
  • ***
  • Posts: 102
Re: Problem with MouseDown event on a PaintBox
« Reply #9 on: April 06, 2022, 10:29:44 am »
Sorry, Wp, here is the completely rewritted program to english variable names.
The question is where should I put the Invalidate command to make visible drawings? Because when you click on the blue rectangle, nothing happen. To be more precisely, everything OK, but you cannot see the little black rectangles on the blue PaintBox.

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Problem with MouseDown event on a PaintBox
« Reply #10 on: April 06, 2022, 12:08:15 pm »
Thank you. But I guess that you did not even try to test whether the translated program compiles, right? There are still some of the old identifiers which are now unknown, of course. I created a new project by combining the old and new versions - I hope that it is correct.

Now let me analyze your code:

You click in the blue rectangle (the FloorPlan):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FloormapMouseDown(Sender: TObject; Button: TMouseButton;
  2.   Shift: TShiftState; X, Y: Integer);
  3. begin
  4.   if (X>0) and (X<Floormap.width) and (Y>0) and (Y<Floormap.height) then
  5.   begin
  6.     createa2dobj('cube',X,Y);
  7.     Floormap.invalidate;
  8.   end;
  9. end;
You create some object (a cube) and request the Floorplan paintbox to redraw it self next time when the OS is ready for it. This is correct.

Now let's look at the code in the OnPaint handler of the Floorplan:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FloorplanPaint(Sender: TObject);
  2. var i: integer;
  3.   j:byte;
  4. begin
  5.   Floormap.color:=clBlue;
  6.   Floormap.canvas.fillrect(0,0,Floormap.width,Floormap.height);
  7.   Floormap.canvas.pen.color:=clBlack;
  8.   Floormap.canvas.pen.width:=2;
  9.   for i:=0 to numof2dobj-1 do
  10.   begin
  11.     for j:=0 to a2dobject[i].numofitspoints-1 do
  12.     begin
  13.       if j<a2dobject[i].numofitspoints-1 then
  14.         canvas.Line(Floormap.left+a2dobject[i].itspointsarray[j,0],Floormap.top+a2dobject[i].itspointsarray[j,2],Floormap.left+a2dobject[i].itspointsarray[j+1,0],Floormap.top+a2dobject[i].itspointsarray[j+1,2])
  15.       else
  16.         canvas.Line(Floormap.left+a2dobject[i].itspointsarray[j,0],Floormap.top+a2dobject[i].itspointsarray[j,2],Floormap.left+a2dobject[i].itspointsarray[0,0],Floormap.top+a2dobject[i].itspointsarray[0,2]);
  17.     end;
  18.   end;
  19. end;
There are two issues
  • You draw lines by calling canvas.Line(...). Since the "canvas" is not specified any further it refers to the canvas of the form, rather than that of the paintbox. This means that you are painting on the form, but this is covered by the paintboxes, at least partly! Put a "Floorplan." in front of it and it will paint on the paintbox: Floorplan.Canvas.line(...)
  • The second issue refers to the coordinates. In the TPaintbox.OnPaint event, the paintbox draws itself, and it does this assuming that all coordinates are relative to itself, i.e. the origin of the coordinate system, in the top/left corner, has the coordinates (x=0, y=0). In your code, however, you add Floorplan.Left and Floorplan.Top to the x/y coordinates of the cube - this offsets the cube by the top/left position of the paintbox, and since the blue paintbox is located quite far at the right, the cube is moved out of the paintbox. Solution: Simply remove Floorplan.Left and Floorplan.Top.

    Here is the fixed event handler:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FloormapPaint(Sender: TObject);
  2. var i: integer;
  3.   j:byte;
  4. begin
  5.   Floormap.color:=clBlue;
  6.   Floormap.canvas.fillrect(0,0,Floormap.width,Floormap.height);
  7.   Floormap.canvas.pen.color:=clBlack;
  8.   Floormap.canvas.pen.width:=2;
  9.   for i:=0 to numof2dobj-1 do
  10.   begin
  11.     for j:=0 to a2dobject[i].numofitspoints-1 do
  12.     begin
  13.       if j<a2dobject[i].numofitspoints-1 then
  14.         Floormap.canvas.Line(a2dobject[i].itspointsarray[j,0],a2dobject[i].itspointsarray[j,2], a2dobject[i].itspointsarray[j+1,0], a2dobject[i].itspointsarray[j+1,2])
  15.       else
  16.         Floormap.canvas.Line(a2dobject[i].itspointsarray[j,0],a2dobject[i].itspointsarray[j,2], a2dobject[i].itspointsarray[0,0], a2dobject[i].itspointsarray[0,2]);
  17.     end;
  18.   end;
  19. end;  
    « Last Edit: April 06, 2022, 12:13:36 pm by wp »

    Tomi

    • Full Member
    • ***
    • Posts: 102
    Re: Problem with MouseDown event on a PaintBox
    « Reply #11 on: April 06, 2022, 02:51:43 pm »
    Oh, big thanks for you, Wp! I rewrote my code on the basis of your advices and it works! :D
    Otherwise, today I tried put this piece of code in my program, but it worked only at clicking in TForm1.AlaprajzMouseDown, but not with TForm1.FormResize, placed at the end of tht procedure:
    Code: Pascal  [Select][+][-]
    1. procedure TForm1.FloormapMouseDown(Sender: TObject; Button: TMouseButton;
    2.   Shift: TShiftState; X, Y: Integer);
    3. begin
    4.   if (X>0) and (X<Floormap.width) and (Y>0) and (Y<Floormap.height) then
    5.   begin
    6.     createa2dobj('cube',X,Y);
    7.     FloormapPaint(Sender); //<- This is the additional code
    8.   end;
    9. end;
    Finally, your code is the best solution.

     

    TinyPortal © 2005-2018