-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPaintScrollBar.pas
More file actions
270 lines (219 loc) · 6.72 KB
/
PaintScrollBar.pas
File metadata and controls
270 lines (219 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
(*
Greenfish Controls - PaintScrollBar
Copyright © 2013 Balázs Szalkai
This file is released under the zlib license:
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*)
unit PaintScrollBar;
interface
uses
{$IFNDEF LCL} Windows, Messages,
{$ELSE} LclIntf, LclType, LResources, {$ENDIF}
SysUtils, Classes, Controls, Graphics, Math;
type
TpsbPaintEvent = procedure(Sender: TObject; bm: TBitmap; Rect: TRect) of object;
TpsbCanScrollEvent = procedure(Sender: TObject; var NewPos: integer) of object;
TPaintScrollBar = class(TCustomControl)
private
FIsMouseDown: boolean;
FMinValue, FMaxValue, FPosition: integer;
FOnChange: TNotifyEvent;
FOnPaint: TpsbPaintEvent;
FOnCanScroll: TpsbCanScrollEvent;
procedure SetIsMouseDown(const Value: boolean);
procedure SetMinValue(const Value: integer);
procedure SetMaxValue(const Value: integer);
procedure SetPosition(const Value: integer);
protected
bmWork: TBitmap;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: integer); override;
procedure Paint; override;
property IsMouseDown: boolean read FIsMouseDown write SetIsMouseDown;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetSiteRect: TRect;
procedure SetParams(APosition, AMin, AMax: integer);
function xToPosition(x: integer): integer;
published
property MinValue: integer read FMinValue write SetMinValue;
property MaxValue: integer read FMaxValue write SetMaxValue;
property Position: integer read FPosition write SetPosition;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnPaint: TpsbPaintEvent read FOnPaint write FOnPaint;
property OnCanScroll: TpsbCanScrollEvent read FOnCanScroll write FOnCanScroll;
property Align;
property Anchors;
property Color;
property ParentColor;
property ShowHint;
property ParentShowHint;
property OnClick;
property OnDblClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;
procedure Register;
implementation
const
psbEdge = 3;
procedure TPaintScrollBar.SetIsMouseDown;
begin
if FIsMouseDown <> Value then
begin
FIsMouseDown := Value;
Invalidate;
end;
end;
procedure TPaintScrollBar.SetMinValue;
begin
SetParams(FPosition, Value, FMaxValue);
end;
procedure TPaintScrollBar.SetMaxValue;
begin
SetParams(FPosition, FMinValue, Value);
end;
procedure TPaintScrollBar.SetPosition;
begin
SetParams(Value, FMinValue, FMaxValue);
end;
procedure TPaintScrollBar.MouseDown;
begin
inherited;
if Button = mbLeft then
begin
IsMouseDown := True;
Position := xToPosition(X);
end;
end;
procedure TPaintScrollBar.MouseMove;
begin
inherited;
if IsMouseDown then Position := xToPosition(X);
end;
procedure TPaintScrollBar.MouseUp;
begin
inherited;
IsMouseDown := False;
end;
procedure TPaintScrollBar.Paint;
var
rSite: TRect;
a: array[0..2] of TPoint;
x: integer;
begin
bmWork.Width := Width;
bmWork.Height := Height;
with bmWork.Canvas do
begin
// draw background
{$IFDEF LCL}
Brush.Color := IfThen(Color = clDefault, GetDefaultColor(dctBrush), Color);
{$ELSE}
Brush.Color := Color;
{$ENDIF}
FillRect(ClientRect);
// draw site
rSite := GetSiteRect;
if Assigned(FOnPaint) then FOnPaint(Self, bmWork, rSite);
// draw cursor
Pen.Color := IfThen(IsMouseDown, clGray, clBlack);
Brush.Color := Pen.Color;
if MinValue < MaxValue then
x := rSite.Left + Round((rSite.Right-1 - rSite.Left) *
(Position - MinValue) / (MaxValue - MinValue)) else
x := rSite.Left;
a[0].x := x - psbEdge; a[0].y := 0;
a[1].x := x; a[1].y := psbEdge;
a[2].x := x + psbEdge; a[2].y := 0;
Polygon(a);
a[0].x := x - psbEdge; a[0].y := Height-1;
a[1].x := x; a[1].y := Height-1 - psbEdge;
a[2].x := x + psbEdge; a[2].y := Height-1;
Polygon(a);
MoveTo(x, 0);
LineTo(x, Height);
end;
Canvas.Draw(0, 0, bmWork);
end;
constructor TPaintScrollBar.Create;
begin
inherited;
DoubleBuffered := True;
bmWork := TBitmap.Create;
bmWork.PixelFormat := pf24bit;
FIsMouseDown := False;
FMinValue := 0;
FMaxValue := 100;
FPosition := 0;
end;
destructor TPaintScrollBar.Destroy;
begin
bmWork.Free;
inherited;
end;
function TPaintScrollBar.GetSiteRect;
begin
Result := Rect(psbEdge, psbEdge, Width - psbEdge, Height - psbEdge);
end;
procedure TPaintScrollBar.SetParams;
begin
if (APosition <> Position) or (AMin <> FMinValue) or (AMax <> FMaxValue) then
begin
if AMax < AMin then AMax := AMin;
// Verify APosition
APosition := Max(AMin, Min(AMax, APosition));
// Allow OnCanScroll to make some changes
if Assigned(FOnCanScroll) then
begin
FOnCanScroll(Self, APosition);
// Verify APosition _once more_, in case it became erroneus
APosition := Max(AMin, Min(AMax, APosition));
end;
FMaxValue := AMax;
FMinValue := AMin;
if FPosition <> APosition then
begin
FPosition := APosition;
if Assigned(FOnChange) then FOnChange(Self);
end;
Repaint;
end;
end;
function TPaintScrollBar.xToPosition;
var
rSite: TRect;
begin
rSite := GetSiteRect;
if rSite.Left <> rSite.Right then
Result := MinValue + Round((MaxValue - MinValue) * (x - rSite.Left) /
(rSite.Right-1 - rSite.Left)) else
Result := MinValue;
end;
procedure Register;
begin
RegisterComponents('Greenfish', [TPaintScrollBar]);
end;
{$IFDEF LCL}
initialization
{$I PaintScrollBar.lrs}
{$ENDIF}
end.