Recent

Author Topic: Client coordinates in a ScrollBox  (Read 1948 times)

simsee

  • Full Member
  • ***
  • Posts: 236
Client coordinates in a ScrollBox
« on: October 16, 2023, 09:40:04 pm »
In the case of a ScrollBox, is it an expected behavior for event handlers like MouseMove to provide the X,Y coordinates of the mouse pointer without taking into account the position of the scrollbar sliders? In other words, I note that the coordinates refer to the origin of the client's physical window, not the logical one. (under Windows o.s.).

Thanks in advance for the usual help.
« Last Edit: October 16, 2023, 11:31:50 pm by simsee »

jamie

  • Hero Member
  • *****
  • Posts: 7660
Re: Client coordinates in a ScrollBox
« Reply #1 on: October 17, 2023, 11:12:20 pm »
yes
The only true wisdom is knowing you know nothing

simsee

  • Full Member
  • ***
  • Posts: 236
Re: Client coordinates in a ScrollBox
« Reply #2 on: October 18, 2023, 08:10:19 pm »
Thanks Jamie and sorry for the late response. I thought my question had been ignored.

My post arises from the fact that the MouseMove event handlers do not detect (in my case) the correct coordinates of the mouse pointer when it is over a graphic object in the scrollbox and the sliders are not in the initial position (=0).

Consider the following demo (compilable project attached):

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.     ScrollBox1: TScrollBox;
  16.     Shape1: TShape;
  17.     procedure ScrollBox1MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
  18.     procedure Shape1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.ScrollBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  35. begin
  36.   Caption:=(X+ScrollBox1.HorzScrollBar.Position).ToString+':'+(Y+ScrollBox1.VertScrollBar.Position).ToString;
  37. end;
  38.  
  39. procedure TForm1.Shape1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  40. begin
  41.   if Sender is TShape then
  42.     ScrollBox1MouseMove(Sender,Shift,TShape(Sender).Left+X,TShape(Sender).Top+Y);
  43. end;
  44.  
  45. end.

For example, the Y position of the mouse pointer when it is adjacent to the outside of the top side of the square is (wrongly) much smaller than when it is adjacent to the inside (see the blue points in the two screenshots).

Can you help me understand what I did wrong? Thank you.
« Last Edit: October 19, 2023, 12:28:12 am by simsee »

jamie

  • Hero Member
  • *****
  • Posts: 7660
Re: Client coordinates in a ScrollBox
« Reply #3 on: October 18, 2023, 11:51:54 pm »
When you drop a control on a scroll box, the LEFT, TOP of that control already accounts for the offset of the scrollbar.

  Your test code is adding the parent scrollbar position to it.

the X, Y values coming from the mouse always reports Client positions and does not care about the scrollbar, it makes believe there is no such thing. So, what you are getting is what you see in front of you, this also means the scrollbar will block part of the client.

 Maybe I miss understood the situation.

Jamie



The only true wisdom is knowing you know nothing

simsee

  • Full Member
  • ***
  • Posts: 236
Re: Client coordinates in a ScrollBox
« Reply #4 on: October 19, 2023, 12:23:36 am »
Dear Jamie, you certainly understood the situation better than me. Something is still not clear to me. The problem also occurs if TShape objects are created dynamically.

However I solved the problem using a different approach, based upon Mouse.CursorPos, as follows:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Upda]"]>BlockedusePos;
  2. begin
  3.   with ScreenToClient(Mouse.CursorPos) do
  4.     Caption:=(X+ScrollBox1.HorzScrollBar.Position).ToString+':'+(Y+ScrollBox1.VertScrollBar.Position).ToString;
  5. end;
  6.  
  7. procedure TForm1.ScrollBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  8. begin
  9.   Upda]"]>BlockedusePos;
  10. end;
  11.  
  12. procedure TForm1.Shape1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  13. begin
  14.   Upda]"]>BlockedusePos;
  15. end;
« Last Edit: October 19, 2023, 12:35:06 am by simsee »

jamie

  • Hero Member
  • *****
  • Posts: 7660
Re: Client coordinates in a ScrollBox
« Reply #5 on: October 19, 2023, 12:38:47 am »
Ok, hope you are using the TShape from the trunk? That has an excellent addition to it that allows you to specify a polygon enclosure so you can have custom shapes. Also, you can detect if the mouse is clicked within the shape and not the unpainted areas.

The only true wisdom is knowing you know nothing

simsee

  • Full Member
  • ***
  • Posts: 236
Re: Client coordinates in a ScrollBox
« Reply #6 on: October 19, 2023, 09:35:51 am »
Thanks Jamie. I'm using the current release. I didn't know there were these interesting new features in the trunk.

A problem with TShape is that non-rectangular shapes trigger events also when the mouse pointer is not on the surface of the shape, but is inside the rectangle that encloses it. I hope this problem has been resolved.

Going back to my demo, I suspect there is some bug in LCL, but I need to delve deeper.

simsee

  • Full Member
  • ***
  • Posts: 236
Re: Client coordinates in a ScrollBox
« Reply #7 on: October 19, 2023, 10:00:35 am »
In the meantime I fixed my first demo program as follows:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ScrollBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  2. begin
  3.   if Sender is TScrollBox then
  4.     begin
  5.       X:=X+ScrollBox1.HorzScrollBar.Position;
  6.       Y:=Y+ScrollBox1.VertScrollBar.Position;
  7.     end;
  8.   Caption:=X.ToString+':'+Y.ToString;
  9. end;
« Last Edit: October 19, 2023, 10:02:38 am by simsee »

 

TinyPortal © 2005-2018