1 | {$INCLUDE Switches.inc}
|
---|
2 | unit PVSB;
|
---|
3 |
|
---|
4 | interface
|
---|
5 |
|
---|
6 | uses
|
---|
7 | {$IFDEF WINDOWS}Windows,{$ENDIF}
|
---|
8 | Classes, Controls, Forms, LCLIntf, LCLType, LMessages, Messages, SysUtils,
|
---|
9 | StdCtrls, Math;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TPVScrollbar }
|
---|
14 |
|
---|
15 | TPVScrollBar = class
|
---|
16 | private
|
---|
17 | FOnUpdate: TNotifyEvent;
|
---|
18 | ScrollBar: TScrollBar;
|
---|
19 | FMax: Integer;
|
---|
20 | function GetMax: Integer;
|
---|
21 | function GetPageSize: Integer;
|
---|
22 | function GetPosition: Integer;
|
---|
23 | procedure ScrollBarChanged(Sender: TObject);
|
---|
24 | procedure SetMax(AValue: Integer);
|
---|
25 | procedure SetPageSize(AValue: Integer);
|
---|
26 | procedure SetPosition(AValue: Integer);
|
---|
27 | public
|
---|
28 | constructor Create(Parent: TWinControl);
|
---|
29 | destructor Destroy; override;
|
---|
30 | procedure Init(Max, PageSize: Integer);
|
---|
31 | procedure SetPos(Pos: Integer);
|
---|
32 | function Process(const Msg: TMessage): boolean;
|
---|
33 | function ProcessMouseWheel(Delta: Integer): Boolean;
|
---|
34 | procedure Show(Visible: boolean);
|
---|
35 | procedure EndSB;
|
---|
36 | procedure SetBorderSpacing(Top, Right, Bottom: Integer);
|
---|
37 | property OnUpdate: TNotifyEvent read FOnUpdate write FOnUpdate;
|
---|
38 | property Position: Integer read GetPosition write SetPosition;
|
---|
39 | property Max: Integer read GetMax write SetMax;
|
---|
40 | property PageSize: Integer read GetPageSize write SetPageSize;
|
---|
41 | end;
|
---|
42 |
|
---|
43 |
|
---|
44 | implementation
|
---|
45 |
|
---|
46 | const
|
---|
47 | Count: Integer = 0;
|
---|
48 |
|
---|
49 | procedure TPVScrollBar.Init(Max, PageSize: Integer);
|
---|
50 | begin
|
---|
51 | ScrollBar.PageSize := PageSize;
|
---|
52 | ScrollBar.Min := 0;
|
---|
53 | Self.Max := Max;
|
---|
54 | ScrollBar.Position := 0;
|
---|
55 | ScrollBar.Visible := Max >= ScrollBar.PageSize;
|
---|
56 | end;
|
---|
57 |
|
---|
58 | procedure TPVScrollBar.SetPos(Pos: Integer);
|
---|
59 | begin
|
---|
60 | if Pos <> 0 then begin
|
---|
61 | ScrollBar.Position := Pos;
|
---|
62 | end;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | function TPVScrollBar.Process(const Msg: TMessage): boolean;
|
---|
66 | var
|
---|
67 | NewPos: integer;
|
---|
68 | begin
|
---|
69 | if Max < ScrollBar.PageSize then
|
---|
70 | result := false
|
---|
71 | else
|
---|
72 | begin
|
---|
73 | if (Msg.wParam and $ffff) in [SB_THUMBPOSITION, SB_THUMBTRACK] then
|
---|
74 | begin
|
---|
75 | result := ((Msg.wParam shr 16) and $ffff) <> ScrollBar.Position;
|
---|
76 | ScrollBar.Position := (Msg.wParam shr 16) and $ffff;
|
---|
77 | end else begin
|
---|
78 | case (Msg.wParam and $ffff) of
|
---|
79 | SB_LINEUP:
|
---|
80 | NewPos := ScrollBar.Position - 1;
|
---|
81 | SB_LINEDOWN:
|
---|
82 | NewPos := ScrollBar.Position + 1;
|
---|
83 | SB_PAGEUP:
|
---|
84 | NewPos := ScrollBar.Position - ScrollBar.PageSize;
|
---|
85 | SB_PAGEDOWN:
|
---|
86 | NewPos := ScrollBar.Position + ScrollBar.PageSize;
|
---|
87 | else
|
---|
88 | NewPos := ScrollBar.Position
|
---|
89 | end;
|
---|
90 | if NewPos < 0 then
|
---|
91 | NewPos := 0;
|
---|
92 | if NewPos > Max - ScrollBar.PageSize + 1 then
|
---|
93 | NewPos := Max - ScrollBar.PageSize + 1;
|
---|
94 | result := NewPos <> ScrollBar.Position;
|
---|
95 | if (NewPos <> ScrollBar.Position) or ((Msg.wParam and $ffff) = SB_ENDSCROLL) then
|
---|
96 | begin
|
---|
97 | ScrollBar.Position := NewPos;
|
---|
98 | end;
|
---|
99 | end;
|
---|
100 | end;
|
---|
101 | end;
|
---|
102 |
|
---|
103 | function TPVScrollBar.ProcessMouseWheel(Delta: Integer): Boolean;
|
---|
104 | var
|
---|
105 | NewPos: integer;
|
---|
106 | begin
|
---|
107 | if Max < ScrollBar.PageSize then Result := False
|
---|
108 | else begin
|
---|
109 | NewPos := ScrollBar.Position - Delta div 30;
|
---|
110 | if NewPos < 0 then NewPos := 0;
|
---|
111 | if NewPos > Max - ScrollBar.PageSize + 1 then
|
---|
112 | NewPos := Max - ScrollBar.PageSize + 1;
|
---|
113 | Result := NewPos <> ScrollBar.Position;
|
---|
114 | if NewPos <> ScrollBar.Position then begin
|
---|
115 | ScrollBar.Position := NewPos;
|
---|
116 | end;
|
---|
117 | end;
|
---|
118 | end;
|
---|
119 |
|
---|
120 | procedure TPVScrollBar.Show(Visible: boolean);
|
---|
121 | begin
|
---|
122 | if not Visible or (Max < ScrollBar.PageSize) then
|
---|
123 | ScrollBar.Visible := False
|
---|
124 | else ScrollBar.Visible := True;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TPVScrollBar.EndSB;
|
---|
128 | begin
|
---|
129 | if Max < ScrollBar.PageSize then
|
---|
130 | ScrollBar.Position := 0 // hidden
|
---|
131 | else begin
|
---|
132 | ScrollBar.Position := Max - ScrollBar.PageSize + 1;
|
---|
133 | end;
|
---|
134 | end;
|
---|
135 |
|
---|
136 | procedure TPVScrollBar.SetBorderSpacing(Top, Right, Bottom: Integer);
|
---|
137 | begin
|
---|
138 | ScrollBar.BorderSpacing.Top := Top;
|
---|
139 | ScrollBar.BorderSpacing.Right := Right;
|
---|
140 | ScrollBar.BorderSpacing.Bottom := Bottom;
|
---|
141 | end;
|
---|
142 |
|
---|
143 | { TPVScrollbar }
|
---|
144 |
|
---|
145 | procedure TPVScrollBar.ScrollBarChanged(Sender: TObject);
|
---|
146 | begin
|
---|
147 | if Assigned(FOnUpdate) then FOnUpdate(Self);
|
---|
148 | end;
|
---|
149 |
|
---|
150 | procedure TPVScrollBar.SetMax(AValue: Integer);
|
---|
151 | begin
|
---|
152 | FMax := AValue;
|
---|
153 | ScrollBar.Max := Math.Max(0, FMax);
|
---|
154 | end;
|
---|
155 |
|
---|
156 | procedure TPVScrollBar.SetPageSize(AValue: Integer);
|
---|
157 | begin
|
---|
158 | ScrollBar.PageSize := AValue;
|
---|
159 | end;
|
---|
160 |
|
---|
161 | function TPVScrollBar.GetPosition: Integer;
|
---|
162 | begin
|
---|
163 | Result := ScrollBar.Position;
|
---|
164 | end;
|
---|
165 |
|
---|
166 | function TPVScrollBar.GetMax: Integer;
|
---|
167 | begin
|
---|
168 | Result := FMax;
|
---|
169 | end;
|
---|
170 |
|
---|
171 | function TPVScrollBar.GetPageSize: Integer;
|
---|
172 | begin
|
---|
173 | Result := ScrollBar.PageSize;
|
---|
174 | end;
|
---|
175 |
|
---|
176 | procedure TPVScrollBar.SetPosition(AValue: Integer);
|
---|
177 | begin
|
---|
178 | ScrollBar.Position := AValue;
|
---|
179 | end;
|
---|
180 |
|
---|
181 | constructor TPVScrollBar.Create(Parent: TWinControl);
|
---|
182 | begin
|
---|
183 | Inc(Count);
|
---|
184 | ScrollBar := TScrollBar.Create(Parent);
|
---|
185 | ScrollBar.Kind := sbVertical;
|
---|
186 | ScrollBar.Name := 'PVSB' + IntToStr(Count);
|
---|
187 | ScrollBar.Align := alRight;
|
---|
188 | ScrollBar.OnChange := ScrollBarChanged;
|
---|
189 | ScrollBar.Parent := Parent;
|
---|
190 | end;
|
---|
191 |
|
---|
192 | destructor TPVScrollBar.Destroy;
|
---|
193 | begin
|
---|
194 | FreeAndNil(ScrollBar);
|
---|
195 | end;
|
---|
196 |
|
---|
197 | end.
|
---|