Recent

Author Topic: Cursor position in Timage  (Read 3323 times)

barsoom

  • Jr. Member
  • **
  • Posts: 51
Cursor position in Timage
« on: August 16, 2021, 11:54:02 pm »
I am working with an image, and, for the moment, i need to know the position (coordinates) of the cursor in the image.

To do that, i have a Timage, and a label.
In the Timage, i added this code to the events 'OnmouseMove' and 'Onmouse leave'
Code: Pascal  [Select][+][-]
  1. // Procedure to show the (relative) coordinates of the cursor on the image
  2. procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  3.   Y: Integer);
  4. begin
  5.   Label1.Caption := Format('%d,%d', [X, Y]);  // way 1
  6.   //Label1.caption:= '('+IntToStr(X)+','+IntToStr(Y)+')'; // way 2
  7. end;  
  8.  
  9. // Procedure to clear the label when the mouse leave the image
  10. procedure TForm1.Image1MouseLeave(Sender: TObject);
  11. begin
  12.   Label1.caption := '';
  13. end;
  14.  

As you can see, in 'OnMouseMove' event i tried two different ways to show the coordinates in the label caption. However, although both ways 'work' somehow, the result is the same in boths, and here is the problem: The coordinates are only updated once the mouse stop on the image, but coordinates are not updated meanwhile the cursor is under movement on the image.

I googled a lot and the procedure seems to be ok, but i do not know why i have these behaviour.
Any idea about what i am doing wrong? Thanks!
« Last Edit: August 17, 2021, 12:10:27 am by barsoom »

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Cursor position in Timage
« Reply #1 on: August 17, 2021, 12:10:07 am »
On which OS/widgetset is this? On Windows, Ubuntu/gtk2 and Ubuntu/qt5 it works correctly.

barsoom

  • Jr. Member
  • **
  • Posts: 51
Re: Cursor position in Timage
« Reply #2 on: August 17, 2021, 12:34:03 am »
It is under OS Windows 10 64bits and Lazarus version 2.0.4 FPC 3.0.4 SVN 61665


wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Cursor position in Timage
« Reply #3 on: August 17, 2021, 12:43:30 am »
My oldest Lazarus of the v2.0.x series is v2.0.8, and it does not show the issue. And of the 1.x series I have v1.8.4, and it does not show the issue either.

So, I have no idea what's going on here. Please try to write a little demo project showing the issue and post it here. Maybe the bug is somewhere else.

barsoom

  • Jr. Member
  • **
  • Posts: 51
Re: Cursor position in Timage
« Reply #4 on: August 17, 2021, 01:18:36 am »
@thanks @wp,
One more time, you are right...

I tried a simple example project, and it worked... then, i added the image inside a scrollbox, as i have in my project... and it worked... BUT, when i changed the autoscroll option of the scrollbox to True, it stop to work correctly. Then, it show the values only when the mouse stop the movement.

The "funny" issue is that it affect to all the images loaded in the form!! Take a look to the example project i prepared. I load a image in two Timage containers. One is directly in the form, and the second one inside a scrollbox. Play with the checkboxes to chnage some properties of the scrollbox, and you will see how it affect to the position information update.

dsiders

  • Hero Member
  • *****
  • Posts: 1084
Re: Cursor position in Timage
« Reply #5 on: August 17, 2021, 01:21:47 am »
It is under OS Windows 10 64bits and Lazarus version 2.0.4 FPC 3.0.4 SVN 61665

Does not have any issues on 2.2.0-RC1 or using 2.3.0 from Git. Windows 8.1, 64-bit, FPC 3.2.0 and 3.2.2.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Cursor position in Timage
« Reply #6 on: August 17, 2021, 06:58:17 pm »
I can confirm the issue with both Laz 2.3 and Laz 1.8.4. It is required that the image loaded into the TPicture is quite large (at first I simply selected the application icon which is too small to see an effect). There is also some flicker of the scrollbox border when the mouse is moved. Therefore, I think that the scrollbox enforces a repaint of the image with every mouse-move. To check this, I added an OnPaint handler to the image which simply counts up a number with every repaint requested:
Code: Pascal  [Select][+][-]
  1. var
  2.   Counter: Integer = 0;
  3. procedure TForm1.Image2Paint(Sender: TObject);
  4. begin
  5.   inc(Counter);
  6.   WriteLn(TControl(Sender).Name, ': ', Counter);
  7. end;
In fact, the counter increments with every move of the mouse. When I hook this event handler also to Image1 (the one outside the scrollbox) I can see that the paint requests always originate from Image2, even when the mouse is outside Image2 - very strange...

I repeated the test on a VM with Ubuntu and found that there the repaints do not occur and the cursor display in the test program updates instantly.

Therefore, I guess that the issue is related to the windows widgetset only. I think I saw a similar bug report recently, but cannot find it at the moment.

barsoom

  • Jr. Member
  • **
  • Posts: 51
Re: Cursor position in Timage
« Reply #7 on: August 18, 2021, 10:37:10 pm »
Thanks @dsiders and @wp!

@wp, you are right, it occurs with relative big picture files.

I also tested other properties of the scrollbox to see how it affect this issue. If you define the range of each scrollbar, the described behaviour seems to "dissapear as well.

 

TinyPortal © 2005-2018