• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

Trackbar problem !

Hi there,

I would like someone if he can to overload the Trackbarr component and make another one and have only one extraoption. To display the position under the cursor. I don`t think it`s very hard for someone who know what`s he`s doing. I want the Delphi component to be overloaded... so don`t point me to other components, I`m using a skin engine and it sees only the Delphi components not sure why.

I would like something similar to this: http://www.cristianignat.com/diverse/trackbar.jpg

P.S. I need this ASAP if possible !

Thanks,
Cristian
0
crystyan
Asked:
crystyan
  • 3
  • 2
1 Solution
 
TheRealLokiSenior DeveloperCommented:
This is the best I can do on short notice
It's not "overloaded" it is descended (i.e. uses inheritence). I assume that is what you mean
Because of the TTrackbar, I couldn't show the final value (e.g. 100) to the right of the thumb.
It goes outside the edge of the component, so I have made it show those ones to the left of the thumb instead
I trust this is ok
It should be easy enough for you to "tweak" it to the exact position/colours you want.

hth, Loki

unit TextUnderTrackbar;

interface

uses classes, comctrls, stdctrls, dialogs, sysutils;

type
    TTextUnderTrackbar = class(TTrackbar)
    private
        ValueLabel: TLabel;
        procedure DrawAndPositionValueLabel;
    public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
        procedure Changed; override;
    end;

IMPLEMENTATION

{ TTextUnderTrackbar }

constructor TTextUnderTrackbar.Create(AOwner: TComponent);
    begin
        inherited Create(AOwner);
        ValueLabel := TLabel.Create(self);
        ValueLabel.Parent := self;
        ValueLabel.Top := 20;
        ValueLabel.Left := 0;
        ValueLabel.Width := 50; //ValueLabel.Canvas.TextWidth('888');
        ValueLabel.Height := 14; //ValueLabel.Canvas.TextHeight('888');
        DrawAndPositionValueLabel;
    end;

destructor TTextUnderTrackbar.Destroy;
    begin
        ValueLabel.Free;
        inherited;

    end;

procedure TTextUnderTrackbar.Changed;
    begin
        inherited Changed;
        DrawAndPositionValueLabel;
    end;

procedure TTextUnderTrackbar.DrawAndPositionValueLabel;
    var
        ActualTextLeft: double;
        totalrange: integer;
        tw: integer;
    begin
        ActualTextLeft := self.position - self.Min; // in case trackbar is 10-20 for example and not 0-100
        totalrange := self.max - self.min;
        ActualTextLeft := (ActualTextLeft / totalrange) * (self.width - 16); // the area before and after the actual bar
        ActualTextLeft := 8 + Trunc(ActualTextLeft) + 8; // to be past the thumb
        if self.parent = nil then
          tw := 20
        else
          tw := ValueLabel.Canvas.TextWidth(IntToStr(self.Position));
        if ActualTextLeft + tw > self.width then
          ActualTextLeft := ActualTextLeft - (tw + 16);
        ValueLabel.Left := Trunc(ActualTextLeft);
        ValueLabel.Caption := IntToStr(self.Position);
    end;

end.
0
 
crystyanAuthor Commented:
Hi there,

Thanks a lot for the help, but could you tell me where was the new component installed cuz I can`t find it in the tabs.

THANKS !
0
 
TheRealLokiSenior DeveloperCommented:
Hi, if you want to make it into a component that lives on the component palette then add the following around the "implementation" section

procedure Register;

implementation

procedure Register;
    begin
        RegisterComponents('Loki', [TTextUnderTrackbar]); // change "Loki" to whatever tab you want it in
    end;

you then need to add this unit to a package (e.g. DCLUrsxx.bpl where xx is your delphi version) just like adding any other component.

If you just want to see it working without making it a component
start a project 1
at the top put
uses TextUnderTrackBar;
(you may need to go "alt=project-add to project and browse to the pas file, or just put it in the same dir for this test)

in the public block put
    mytrackbar: TTextUnderTrackbar;

put a TButton on the form and double click it, put this code there
        mytrackbar := TTextUnderTrackbar.Create(form1);
        mytrackbar.Parent := form1;
        mytrackbar.Left := 96;
        mytrackbar.Top := 108;
        mytrackbar.Width := 150;
        mytrackbar.Height := 41;
        mytrackbar.Orientation := trHorizontal;
        mytrackbar.Frequency := 1;
        mytrackbar.Position := 0;
        mytrackbar.SelEnd := 0;
        mytrackbar.SelStart := 0;
        mytrackbar.TickMarks := tmBottomRight;
        mytrackbar.TickStyle := tsAuto;

btw, so far I've only made it work for horizontal trackbars, if you want it to work vertically as well, let me know
0
 
crystyanAuthor Commented:
Thanks,

If you can make me vertically too it would be awesome! Thanks.

Cristian
0
 
TheRealLokiSenior DeveloperCommented:
Change the "DrawAndPositionValueLabel" procedure to :-

procedure TTextUnderTrackbar.DrawAndPositionValueLabel;
    var
        ActualTextLeftOrTop: double;
        totalrange: integer;
        tworh: integer;
    begin
        ActualTextLeftOrTop := self.position - self.Min; // in case trackbar is 10-20 for example and not 0-100
        totalrange := self.max - self.min;

        if self.Orientation = trHorizontal then
        begin
            ActualTextLeftOrTop := (ActualTextLeftOrTop / totalrange) * (self.width - 16); // the area before and after the actual bar
            ActualTextLeftOrTop := 8 + Trunc(ActualTextLeftOrTop) + 8; // to be past the thumb
            if self.parent = nil then
              tworh := 20
            else
              tworh := ValueLabel.Canvas.TextWidth(IntToStr(self.Position));
            if ActualTextLeftOrTop + tworh > self.width then
              ActualTextLeftOrTop := ActualTextLeftOrTop - (tworh + 16);
            ValueLabel.Left := Trunc(ActualTextLeftOrTop);
            ValueLabel.Top := 20;
        end
        else
        begin
            ActualTextLeftOrTop := (ActualTextLeftOrTop / totalrange) * (self.height - 16); // the area before and after the actual bar
            ActualTextLeftOrTop := 8 + Trunc(ActualTextLeftOrTop) + 8; // to be past the thumb
            if self.parent = nil then
              tworh := 20
            else
              tworh := ValueLabel.Canvas.TextHeight(IntToStr(self.Position));
            if ActualTextLeftOrTop + tworh > self.height then
              ActualTextLeftOrTop := ActualTextLeftOrTop - (tworh + 16);
            ValueLabel.Left := 20;
            ValueLabel.Top := Trunc(ActualTextLeftOrTop);
        end;
        ValueLabel.Caption := IntToStr(self.Position);
    end;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now