-
Notifications
You must be signed in to change notification settings - Fork 1
/
TFlatProgressBarUnit.pas
407 lines (370 loc) · 10.8 KB
/
TFlatProgressBarUnit.pas
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
unit TFlatProgressBarUnit;
interface
{$I DFS.inc}
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, ExtCtrls, Comctrls, FlatUtilitys;
type
TFlatProgressBar = class(TGraphicControl)
private
FTransparent: Boolean;
FSmooth: Boolean;
FUseAdvColors: Boolean;
FAdvColorBorder: TAdvColors;
FOrientation: TProgressBarOrientation;
FElementWidth: Integer;
FElementColor: TColor;
FBorderColor: TColor;
FPosition: Integer;
FMin: Integer;
FMax: Integer;
FStep: Integer;
procedure SetMin (Value: Integer);
procedure SetMax (Value: Integer);
procedure SetPosition (Value: Integer);
procedure SetStep (Value: Integer);
procedure SetColors (Index: Integer; Value: TColor);
procedure SetAdvColors (Index: Integer; Value: TAdvColors);
procedure SetUseAdvColors (Value: Boolean);
procedure SetOrientation (Value: TProgressBarOrientation);
procedure SetSmooth (Value: Boolean);
procedure CheckBounds;
procedure CMSysColorChange (var Message: TMessage); message CM_SYSCOLORCHANGE;
procedure CMParentColorChanged (var Message: TWMNoParams); message CM_PARENTCOLORCHANGED;
procedure SetTransparent (const Value: Boolean);
protected
procedure CalcAdvColors;
procedure DrawElements;
procedure Paint; override;
{$IFDEF DFS_COMPILER_4_UP}
procedure SetBiDiMode(Value: TBiDiMode); override;
{$ENDIF}
public
constructor Create (AOwner: TComponent); override;
procedure StepIt;
procedure StepBy (Delta: Integer);
published
property Transparent: Boolean read FTransparent write SetTransparent default false;
property Align;
property Cursor;
property Color default $00E1EAEB;
property ColorElement: TColor index 0 read FElementColor write SetColors default $00996633;
property ColorBorder: TColor index 1 read FBorderColor write SetColors default $008396A0;
property AdvColorBorder: TAdvColors index 0 read FAdvColorBorder write SetAdvColors default 50;
property UseAdvColors: Boolean read FUseAdvColors write SetUseAdvColors default false;
property Orientation: TProgressBarOrientation read FOrientation write SetOrientation default pbHorizontal;
property Enabled;
property ParentColor;
property Visible;
property Hint;
property ShowHint;
property PopupMenu;
property ParentShowHint;
property Min: Integer read FMin write SetMin;
property Max: Integer read FMax write SetMax;
property Position: Integer read FPosition write SetPosition default 0;
property Step: Integer read FStep write SetStep default 10;
property Smooth: Boolean read FSmooth write SetSmooth default false;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
{$IFDEF DFS_COMPILER_4_UP}
property Anchors;
property BiDiMode write SetBidiMode;
property Constraints;
property DragKind;
property ParentBiDiMode;
property OnEndDock;
property OnStartDock;
{$ENDIF}
end;
implementation
constructor TFlatProgressBar.Create (AOwner: TComponent);
begin
inherited Create(AOwner);
Height := 16;
Width := 147;
FElementWidth := 8;
FElementColor := $00996633;
FBorderColor := $008396A0;
ParentColor := True;
Orientation := pbHorizontal;
FStep := 10;
FMin := 0;
FMax := 100;
FUseAdvColors := false;
FAdvColorBorder := 50;
Transparent := false;
end;
procedure TFlatProgressBar.SetOrientation (Value: TProgressBarOrientation);
begin
if FOrientation <> Value then
begin
FOrientation := Value;
if (csLoading in ComponentState) then
begin
Repaint;
Exit;
end;
SetBounds(Left, Top, Height, Width);
Invalidate;
end;
end;
procedure TFlatProgressBar.SetMin (Value: Integer);
begin
if FMin <> Value then
begin
FMin := Value;
Invalidate;
end;
end;
procedure TFlatProgressBar.SetMax (Value: Integer);
begin
if FMax <> Value then
begin
if Value < FPosition then FPosition := Value;
FMax := Value;
Invalidate;
end;
end;
procedure TFlatProgressBar.SetPosition (Value: Integer);
begin
if Value > FMax then Value := FMax;
if Value < FMin then Value := FMin;
if Value > FPosition then
begin
FPosition := Value;
DrawElements;
end;
if Value < FPosition then
begin
FPosition := Value;
Invalidate;
end;
end;
procedure TFlatProgressBar.SetStep (Value: Integer);
begin
if FStep <> Value then
begin
FStep := Value;
Invalidate;
end;
end;
procedure TFlatProgressBar.StepIt;
begin
if (FPosition + FStep) > FMax then
FPosition := FMax
else
FPosition := FPosition + FStep;
DrawElements;
end;
procedure TFlatProgressBar.StepBy (Delta: Integer);
begin
if (FPosition + Delta) > FMax then
FPosition := FMax
else
FPosition := FPosition + Delta;
DrawElements;
end;
procedure TFlatProgressBar.SetColors (Index: Integer; Value: TColor);
begin
case Index of
0: FElementColor := Value;
1: FBorderColor := Value;
end;
Invalidate;
end;
procedure TFlatProgressBar.CalcAdvColors;
begin
if FUseAdvColors then
begin
FBorderColor := CalcAdvancedColor(Color, FBorderColor, FAdvColorBorder, darken);
end;
end;
procedure TFlatProgressBar.SetAdvColors (Index: Integer; Value: TAdvColors);
begin
case Index of
0: FAdvColorBorder := Value;
end;
CalcAdvColors;
Invalidate;
end;
procedure TFlatProgressBar.SetUseAdvColors (Value: Boolean);
begin
if Value <> FUseAdvColors then
begin
FUseAdvColors := Value;
ParentColor := Value;
CalcAdvColors;
Invalidate;
end;
end;
procedure TFlatProgressBar.CMSysColorChange (var Message: TMessage);
begin
if FUseAdvColors then
begin
ParentColor := True;
CalcAdvColors;
end;
Invalidate;
end;
procedure TFlatProgressBar.CMParentColorChanged (var Message: TWMNoParams);
begin
inherited;
if FUseAdvColors then
begin
ParentColor := True;
CalcAdvColors;
end;
Invalidate;
end;
procedure TFlatProgressBar.SetSmooth(Value: Boolean);
begin
if Value <> FSmooth then
begin
FSmooth := Value;
Invalidate;
end;
end;
procedure TFlatProgressBar.SetTransparent(const Value: Boolean);
begin
FTransparent := Value;
Invalidate;
end;
{$IFDEF DFS_COMPILER_4_UP}
procedure TFlatProgressBar.SetBiDiMode(Value: TBiDiMode);
begin
inherited;
Invalidate;
end;
{$ENDIF}
procedure TFlatProgressBar.CheckBounds;
var
maxboxes: Word;
begin
if FOrientation = pbHorizontal then
begin
maxboxes := (Width - 3) div (FElementWidth + 1);
if Width < 12 then
Width := 12
else
Width := maxboxes * (FElementWidth + 1) + 3;
end
else
begin
maxboxes := (Height - 3) div (FElementWidth + 1);
if Height < 12 then
Height := 12
else
Height := maxboxes * (FElementWidth + 1) + 3;
end;
end;
procedure TFlatProgressBar.Paint;
var
PaintRect: TRect;
begin
if not Smooth then
CheckBounds;
PaintRect := ClientRect;
// Background
if not FTransparent then
begin
canvas.Brush.Color := Self.Color;
canvas.Brush.Style := bsSolid;
canvas.FillRect(PaintRect);
end;
// Border
canvas.Brush.Color := FBorderColor;
Canvas.FrameRect(PaintRect);
// Elements
DrawElements;
end;
procedure TFlatProgressBar.DrawElements;
var
NumElements, NumToPaint: LongInt;
Painted: Byte;
ElementRect: TRect;
begin
with canvas do
begin
if not Smooth then
begin
if FOrientation = pbHorizontal then
begin
NumElements := Trunc((ClientWidth - 3) div (FElementWidth + 1));
NumToPaint := Trunc((FPosition - FMin) / ((FMax - FMin) / NumElements) + 0.00000001);
if NumToPaint > NumElements then
NumToPaint := NumElements;
{$IFDEF DFS_COMPILER_4_UP}
if BidiMode = bdRightToLeft then
ElementRect := Rect(ClientRect.Right - 2 - FElementWidth, ClientRect.Top + 2, ClientRect.Right - 2, ClientRect.Bottom - 2)
else
ElementRect := Rect(ClientRect.Left + 2, ClientRect.Top + 2, ClientRect.Left + 2 + FElementWidth, ClientRect.Bottom - 2);
{$ELSE}
ElementRect := Rect(ClientRect.Left + 2, ClientRect.Top + 2, ClientRect.Left + 2 + FElementWidth, ClientRect.Bottom - 2);
{$ENDIF}
if NumToPaint > 0 then
begin
Brush.Color := FElementColor;
Brush.Style := bsSolid;
for Painted := 1 to NumToPaint do
begin
Canvas.FillRect(ElementRect);
{$IFDEF DFS_COMPILER_4_UP}
if BidiMode = bdRightToLeft then
begin
ElementRect.Left := ElementRect.Left - FElementWidth - 1;
ElementRect.Right := ElementRect.Right - FElementWidth - 1;
end
else
begin
ElementRect.Left := ElementRect.Left + FElementWidth + 1;
ElementRect.Right := ElementRect.Right + FElementWidth + 1;
end;
{$ELSE}
ElementRect.Left := ElementRect.Left + FElementWidth + 1;
ElementRect.Right := ElementRect.Right + FElementWidth + 1;
{$ENDIF}
end;
end;
end
else
begin
NumElements := Trunc((ClientHeight - 3) div (FElementWidth + 1));
NumToPaint := Trunc((FPosition - FMin) / ((FMax - FMin) / NumElements) + 0.00000001);
if NumToPaint > NumElements then
NumToPaint := NumElements;
ElementRect := Rect(ClientRect.Left + 2, ClientRect.Bottom - FElementWidth - 2, ClientRect.Right - 2, ClientRect.Bottom - 2);
if NumToPaint > 0 then
begin
Brush.Color := FElementColor;
Brush.Style := bsSolid;
for Painted := 1 to NumToPaint do
begin
Canvas.FillRect(ElementRect);
ElementRect.Top := ElementRect.Top - (FElementWidth + 1);
ElementRect.Bottom := ElementRect.Bottom - (FElementWidth + 1);
end;
end;
end;
end
else
begin
if (FOrientation = pbHorizontal) and (FPosition > 0) then
begin
Brush.Color := FElementColor;
Canvas.FillRect(Rect(2, 2, ClientRect.Left + 2 + ((FPosition * (ClientWidth - 4)) div (FMax - FMin)), ClientRect.Bottom - 2));
end
else
begin
Brush.Color := FElementColor;
Canvas.FillRect(Rect(2, ClientRect.Bottom - 2 - ((FPosition * (ClientHeight - 4)) div (FMax - FMin)), ClientRect.Right - 2, ClientRect.Bottom - 2));
end;
end;
end;
end;
end.