Recent

Author Topic: Get form position of trackbar pointer  (Read 240 times)

user5

  • Sr. Member
  • ****
  • Posts: 418
Get form position of trackbar pointer
« on: January 10, 2026, 11:25:05 pm »
    I want to use the attached code to get the X position of a trackbar pointer on the form, not its position on the trackbar. The code keeps producing the number 329 regardless of the actual position of the pointer. Can anyone see what I'm doing wrong?

Code: Pascal  [Select][+][-]
  1.  
uses Windows, CommCtrl, etc.

procedure TForm1.Button16Click(Sender: TObject);
var
  ThumbRect: TRect;
  ScreenPos: TPoint;
  xpos,ypos:integer;
  Result:TPoint;
  tempstr:string;
begin

  ZeroMemory(@ThumbRect, SizeOf(ThumbRect));
  // Send message to get the thumb's rectangle relative to the TrackBar
  SendMessage(TrackBar1.Handle, TBM_GETTHUMBRECT, 0, LPARAM(@ThumbRect));

  // Get the center top point of the thumb relative to the TrackBar
  ScreenPos := TrackBar1.ClientToScreen(Point(ThumbRect.Left + (ThumbRect.Width div 2), ThumbRect.Top));

  // Convert the screen coordinates to client coordinates of the Form
  Result := Form1.ScreenToClient(ScreenPos);

 xpos := Result.X;
 str(xpos,tempstr);
 edit5.text := tempstr;

end;


jamie

  • Hero Member
  • *****
  • Posts: 7493
Re: Get form position of trackbar pointer
« Reply #1 on: January 11, 2026, 12:16:42 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.TrackBar1Change(Sender: TObject);
  2. Var
  3.   R:Trect;
  4.   SPos:TPoint;
  5.   Result:TPoint;
  6. begin
  7.     Sendmessage(Trackbar1.handle,TBM_GETTHUMBRECT, 0,UintPtr(@R));
  8.     SPos := TrackBar1.ClientToScreen(TPoint.Create(R.Left+R.Width Div 2,R.Top));
  9.     Result := Form1.ScreenToClient(SPos);
  10.    Caption :=  Result.X.ToString+','+Result.Y.ToString;
  11. end;                                          
  12.  
  13. Works in windows, may not work in other targets.
  14.  
  15. Jamie
  16.  
The only true wisdom is knowing you know nothing

user5

  • Sr. Member
  • ****
  • Posts: 418
Re: Get form position of trackbar pointer
« Reply #2 on: January 11, 2026, 02:04:11 am »
    My original goal was to have the left side of a paintbox follow the pointer of a trackbar as the pointer is moved, as shown in the attached picture.
    I haven't tried your code out yet jamie but it looks great and thanks very much.
    The previous code I posted did not work for me but the code below does work. It uses percentages rather than screenshots.
    Excuse me for a previous posting error.     Enjoy.

   
Code: Pascal  [Select][+][-]
  1.   procedure TForm338.TrackBar1Change(Sender: TObject);
  2. var percentchg:real;
  3.     positionvar:integer;
  4.     tempstr:string;
  5. begin
  6.  
  7.  if edit1.color = clYellow then
  8.   begin
  9.  
  10.    //Get the percentage based on the pointer position on the trackbar.
  11.    percentchg := (trackbar1.position / bin4frsum);
  12.    //Multiply the percentage times the number of pixels between the start and
  13.    //end points of the trackbar in pixels to get a tentative position.
  14.    positionvar := round(338 * percentchg);
  15.    //Add the left position of the paintbox to the tentative position.
  16.    paintbox1.left := (positionvar + 95);
  17.  
  18.    str(trackbar1.Position,tempstr);
  19.    edit38.text := tempstr;
  20.    application.processmessages;
  21.  
  22.   end;
  23.  
  24. end;  


user5

  • Sr. Member
  • ****
  • Posts: 418
Re: Get form position of trackbar pointer
« Reply #3 on: January 11, 2026, 02:35:49 am »
    I tried out your code jamie and it worked fine.
    After looking again at the originally posted code that appeared to fail, I later realized that I had put in the
wrong trackbar number so that code may also be okay. Duh.

 

TinyPortal © 2005-2018