Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
A Trackbar like on the desktop volume control Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Alec Bergamini

Has it ever bothered you that the TTrackbar in Delphi doesn't look the way you 


Solve 1:

The TTrackBar in Delphi is very wide. This is unlike the trackbar you see used 
throughout the Windows 9X and 2000 desktops. For some reason I found this annoying. 

After doing some research and a bit of experimenting I discovered the reason. When 
Delphi creates the trackbar from the windows common controls store it specifies a 
style of TBS_ENABLESELRANGE (see TTrackBar.CreateParams in ComCtrls.pas). The 
TBS_ENABLESELRANGE style is what allows the Trackbar to display a selection range 
and support the three properties SelEnd, SelRange and SelStart.. 

This range feature is nice but under normal usage isnít used much. In most cases 
all we want is a slider that we can set a min an max value and then set and track 
the current position (like the desktop volume control). 

Anyway, it turns out that if the trackbar is created without this 
TBS_ENABLESELRANGE style set, then itís thickness reverts back to the skinny slider 
seen on the desktop. See the complete code below for ToaTrackbar. 

The new ToaTrackBar is very simple. It just adds a boolean SelRange property which 
specifies if you want to use the range features of the component. This property 
defaults to true so it is exactly like the current TTrackBar component. If you can 
live without the range feature then you can set SelRange to false which has a 
visible slimming effect. In the code, setting SelRange to false simply recreates 
the control without the TBS_ENABLESELRANGE style. 

To Use - Once youíve installed the component code below into your IDE then go to 
the Samples tab on the component palette find the oaTrackBar and drop it on a form. 
Now set the new SelRange property to false, TickMarks to tmBoth and TickStyle to 
tsNone and presto, you have a trackbar just like the desktops volume control. 

1   unit oaTrackBar;
2   { Freeware by Alec Bergamini O&A Productions
3     Setting the SelRange to false, TickMarks to tmBoth and TickStyle
4     tsNone gives you the same look as the desktop volume slider.}
5   interface
7   uses
8     Windows, Messages, SysUtils, Classes, Controls, ComCtrls, CommCtrl;
10  type
11    ToaTrackBar = class(TTrackBar)
12    private
13      fSelRange: Boolean;
14      procedure SetSelRange(const Value: Boolean);
15    protected
16      procedure CreateParams(var Params: TCreateParams); override;
17    public
18      constructor Create(AOwner: TComponent); override;
19    published
20      property SelRange: Boolean read fSelRange write SetSelRange default True;
21    end;
23  procedure register;
25  implementation
27  procedure register;
28  begin
29    RegisterComponents('Samples', [ToaTrackBar]);
30  end;
32  { ToaTrackBar }
34  constructor ToaTrackBar.Create(AOwner: TComponent);
35  begin
36    inherited;
37    fSelRange := True;
38  end;
40  procedure ToaTrackBar.CreateParams(var Params: TCreateParams);
41  begin
42    inherited;
43    with Params do
44    begin
45      if not fSelRange then
46        Style := Style and not TBS_ENABLESELRANGE;
47    end;
48  end;
50  procedure ToaTrackBar.SetSelRange(const Value: Boolean);
51  begin
52    if Value <> fSelRange then
53    begin
54      fSelRange := Value;
55      RecreateWnd;
56    end;
57  end;
59  end.

Solve 2:

60  procedure TForm1.Button1Click(Sender: TObject);
61  var
62    h1: integer;
63  begin
64    h1 := getwindowlong(trackbar1.handle, GWL_STYLE);
65    h1 := h1 xor $20 {numeric value of TBS_ENABLESELRANGE};
66    setwindowlong(trackbar1.handle, GWL_STYLE, h1);
67  end;

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC