Author: Alec Bergamini
Has it ever bothered you that the TTrackbar in Delphi doesn't look the way you
expect?
Answer:
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 www.o2a.com
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
6
7 uses
8 Windows, Messages, SysUtils, Classes, Controls, ComCtrls, CommCtrl;
9
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;
22
23 procedure register;
24
25 implementation
26
27 procedure register;
28 begin
29 RegisterComponents('Samples', [ToaTrackBar]);
30 end;
31
32 { ToaTrackBar }
33
34 constructor ToaTrackBar.Create(AOwner: TComponent);
35 begin
36 inherited;
37 fSelRange := True;
38 end;
39
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;
49
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;
58
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;
|