source: trunk/LocalPlayer/PVSB.pas

Last change on this file was 508, checked in by chronos, 4 months ago
File size: 5.2 KB
Line 
1{$INCLUDE Switches.inc}
2unit PVSB;
3
4interface
5
6uses
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
12type
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
45implementation
46
47const
48 Count: Integer = 0;
49
50procedure TPVScrollBar.Init(Max, PageSize: Integer);
51begin
52 ScrollBar.PageSize := PageSize;
53 ScrollBar.Min := 0;
54 Self.Max := Max;
55 ScrollBar.Position := 0;
56 ScrollBar.Visible := Max >= ScrollBar.PageSize;
57end;
58
59procedure TPVScrollBar.SetPos(Pos: Integer);
60begin
61 if Pos <> 0 then begin
62 ScrollBar.Position := Pos;
63 end;
64end;
65
66function TPVScrollBar.Process(const Msg: TMessage): Boolean;
67var
68 NewPos: Integer;
69begin
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;
102end;
103
104function TPVScrollBar.ProcessMouseWheel(Delta: Integer): Boolean;
105var
106 NewPos: Integer;
107begin
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;
119end;
120
121procedure TPVScrollBar.Show(Visible: Boolean);
122begin
123 if not Visible or (Max < ScrollBar.PageSize) then
124 ScrollBar.Visible := False
125 else ScrollBar.Visible := True;
126end;
127
128procedure TPVScrollBar.EndSB;
129begin
130 if Max < ScrollBar.PageSize then
131 ScrollBar.Position := 0 // hidden
132 else begin
133 ScrollBar.Position := Max - ScrollBar.PageSize + 1;
134 end;
135end;
136
137procedure TPVScrollBar.SetBorderSpacing(Top, Right, Bottom: Integer);
138begin
139 ScrollBar.BorderSpacing.Top := Top;
140 ScrollBar.BorderSpacing.Right := Right;
141 ScrollBar.BorderSpacing.Bottom := Bottom;
142end;
143
144{ TPVScrollbar }
145
146procedure TPVScrollBar.ScrollBarChanged(Sender: TObject);
147begin
148 if Assigned(FOnUpdate) then FOnUpdate(Self);
149end;
150
151procedure TPVScrollBar.SetMax(AValue: Integer);
152begin
153 FMax := AValue;
154 ScrollBar.Max := Math.Max(0, FMax);
155end;
156
157procedure TPVScrollBar.SetPageSize(AValue: Integer);
158begin
159 ScrollBar.PageSize := AValue;
160end;
161
162function TPVScrollBar.GetPosition: Integer;
163begin
164 Result := ScrollBar.Position;
165end;
166
167function TPVScrollBar.GetMax: Integer;
168begin
169 Result := FMax;
170end;
171
172function TPVScrollBar.GetPageSize: Integer;
173begin
174 Result := ScrollBar.PageSize;
175end;
176
177procedure TPVScrollBar.SetPosition(AValue: Integer);
178begin
179 ScrollBar.Position := AValue;
180end;
181
182constructor TPVScrollBar.Create(Parent: TWinControl);
183begin
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;
192end;
193
194destructor TPVScrollBar.Destroy;
195begin
196 FreeAndNil(ScrollBar);
197end;
198
199end.
Note: See TracBrowser for help on using the repository browser.