[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Trackbar problem !

Posted on 2006-05-17
5
Medium Priority
?
344 Views
Last Modified: 2010-04-05
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
Comment
Question by:crystyan
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16705745
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
 

Author Comment

by:crystyan
ID: 16706957
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
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16712365
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
 

Author Comment

by:crystyan
ID: 16712381
Thanks,

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

Cristian
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 2000 total points
ID: 16712555
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 4 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question