Link to home
Create AccountLog in
Delphi

Delphi

--

Questions

--

Followers

Top Experts

Avatar of ezpete
ezpete

trackbar and mouse position
Is there a way to move a trackbar to the position of the mouse pointer when you click on the trackbar.

trackbar position before click

---|----------------


mouse click on the trackbar near the end


------------------|--


I know the trackbar will step towards the pointer but I want to do it in one movement not steps.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of ezpeteezpete

ASKER

BTW I am using TJvTrackbar from the JEDI VCL which already has an OnMouseDown Event Property if that helps.

ASKER CERTIFIED SOLUTION
Avatar of CalvinDayCalvinDay

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

I went ahead and created a component that would require less code changes.
Save the following as a unit and then install the component. The new TrackBarC
will be found on the samples palette. It works for horizontal bars. I'll leave it
to you for vertical. This was a good example of making delphi work for you
especially when an existing property is not published (OnMouseDown).

unit TrackBarC;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls;

type
  TTrackBarC = class(TTrackBar)
  public
    property OnMouseDown;
    constructor Create(AOwner: TComponent); override;
    procedure MouseDownC(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TTrackBarC]);
end;

constructor TTrackBarC.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  OnMouseDown:=MouseDownC;
end;

procedure TTrackBarC.MouseDownC(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);
var
  Offset,Space:Integer;
begin
  Space:=11;
  Offset:=X-Space;
  Position:=Round(Min+Offset/(Width-2*Space)*(Max-Min));
end;

end.

Avatar of ezpeteezpete

ASKER

Thanks alot for your help. I just used your first example on the OnMouseDown event for the trackbar I already had and it worked great. I would have used your component but the trackbar I am using has an OnMouseUp event as well and I needed to keep it.

I think your component will come in handy though because it gives me an idea of how to create my own components for things like this so thanks again.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of The_SnifFThe_SnifF🇮🇱

I don't get the OnMouseDown, it won't show up.
Delphi

Delphi

--

Questions

--

Followers

Top Experts

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.