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