Recent

Author Topic: jump to mouseposition at click for ttrackbar  (Read 7063 times)

Kunstbanause

  • New Member
  • *
  • Posts: 47
jump to mouseposition at click for ttrackbar
« on: June 09, 2010, 05:31:24 pm »
Hi,
I found a code for a ttrackbar to jump to the position of the mouse at click (http://www.delphipraxis.net/107882-beim-klick-auf-eine-trackbar-zur-klickposition-springen.html)

Code: [Select]
procedure TForm1.Trackbar1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  ChannelRect, SliderRect: TRect;
  SliderWidth: Word;
  Pt: TPoint;
begin
  FillChar(SliderRect, SizeOf(SliderRect), 0);
  FillChar(ChannelRect, SizeOf(ChannelRect), 0);
  SendMessage(Trackbar1.Handle, TBM_GETTHUMBRECT, 0, DWord(@SliderRect));
  SendMessage(Trackbar1.Handle, TBM_GETCHANNELRECT, 0, DWord(@ChannelRect));

  if Trackbar1.Orientation = trHorizontal then
  begin
    Pt:= Classes.Point(X, Y);
    SliderWidth := (SliderRect.Right - SliderRect.Left) div 2;
    ChannelRect.Top := SliderRect.Top;
    ChannelRect.Bottom := SliderRect.Bottom;
  end
  else
  begin
    Pt:= Classes.Point(X, Y);
    SliderWidth := (SliderRect.Bottom - SliderRect.Top) div 2;
    ChannelRect.Top := SliderRect.Left;
    ChannelRect.Bottom := SliderRect.Right;
  end;

  if PtInRect(SliderRect, Classes.Point(X, Y)) then
    inherited
  else
  if Windows.PtInRect(ChannelRect, Pt) then
  begin
    inc(ChannelRect.Left, SliderWidth);
    dec(ChannelRect.Right, SliderWidth);
    Trackbar1.Position := round((Pt.X - ChannelRect.Left) * Trackbar1.Max /
                           (ChannelRect.Right - ChannelRect.Left));
  end;

  inherited;
end;

unfortunately it uses "SendMessage", which I can't use in Linux. Is there any chance I can jump to the click position directly outside of Windows (for the moment, Linux would be sufficient)?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1890
Re: jump to mouseposition at click for ttrackbar
« Reply #1 on: June 09, 2010, 05:59:53 pm »
This?

Code: [Select]
procedure TForm1.TrackBar1Click(Sender:TObject);
begin
TrackBar1.Position:=((ScreenToClient(Mouse.CursorPos).X-TrackBar1.Left+4)*TrackBar1.Max) div TrackBar1.ClientWidth;
end;