Menu

[r3189]: / trunk / Src / UMarquee.pas  Maximize  Restore  History

Download this file

213 lines (178 with data), 6.4 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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2008-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* TProgressBar descendant that displays a "marquee" that repeatedly cycles a
* progress bar display.
}
unit UMarquee;
interface
uses
// Delphi
Classes, ComCtrls;
type
/// <summary>Base class for a TProgressBar descendant component that can
/// display a "marquee" that repeatedly cycles the progress bar display.
/// </summary>
/// <remarks>
/// <para>Requires different concrete implementations depending on whether
/// the underlying operating system supports the progress bar marquee
/// natively.</para>
/// <para>Callers must construct instances using the CreateInstance class
/// method. The Create constructor must not be called.</para>
/// </remarks>
TMarquee = class abstract(TProgressBar)
strict private
/// <summary>Checks if the underlying operating system supports the
/// progress bar natively.</summary>
class function IsBuiltInMarqueeAvailable: Boolean;
strict protected
const
/// <summary>Update interval for marquee in ms.</summary>
UpdateInterval = 20;
public
/// <summary>Initialises a new component instance for sub-classes.
/// </summary>
/// <param name="AOwner">TComponent [in] Reference to owning component.
/// </param>
/// <remarks>This constructor must not be called directly, only by
/// sub-classes. Users of TMarquee must call CreateInstance instead.
/// </remarks>
constructor Create(AOwner: TComponent); override;
/// <summary>Constructs and returns a concrete instance of the component
/// that implements the marquee in a manner suitable for the underlying
/// operating system.</summary>
/// <param name="AOwner">TComponent [in] Reference to owning component.
/// </param>
/// <remarks>On OSs that support the marquee progress bar natively the
/// created instance uses the native support. On older OSs without native
/// support the concrete instance emulates the marquee.</remarks>
class function CreateInstance(AOwner: TComponent): TMarquee;
/// <summary>Start the marquee running.</summary>
procedure Start; virtual; abstract;
/// <summary>Stop the marquee.</summary>
procedure Stop; virtual; abstract;
end;
implementation
uses
// Delphi
SysUtils, Messages, Controls, ExtCtrls,
// Project
USystemInfo, UThemesEx;
type
/// <summary>Progress bar component that can emulate a marquee.</summary>
/// <remarks>For use on operating systems that do not have native support for
/// marquees within the progress bar.</remarks>
TEmulatedMarquee = class sealed(TMarquee)
strict private
/// <summary>Timer object used to update marquee display.</summary>
fTimer: TTimer;
/// <summary>Handles timer events by updating the progress bar position.
/// </summary>
procedure TickHandler(Sender: TObject);
public
/// <summary>Destroys object instance. Ensures the timer has been stopped.
/// </summary>
destructor Destroy; override;
/// <summary>Starts the marquee emulation running.</summary>
procedure Start; override;
/// <summary>Stops the marquee emulation.</summary>
procedure Stop; override;
end;
/// <summary>Progress bar component that displays a marquee natively.
/// </summary>
/// <remarks>For use on operating systems that have native support for
/// marquees within a progress bar.</remarks>
TNativeMarquee = class sealed(TMarquee)
strict private
const
/// <summary>Progress bar marquee window style.</summary>
/// <remarks>From CommCtrl.h.</remarks>
PBS_MARQUEE = $08; // progress bar marquee style
/// <summary>Progress bar marquee activation / deactivation message.
/// </summary>
/// <remarks>From CommCtrl.h.</remarks>
PBM_SETMARQUEE = WM_USER + 10;
public
/// <summary>Modifies the progress bar's window style to enable use of the
/// marquee.</summary>
procedure CreateParams(var Params: TCreateParams); override;
/// <summary>Starts the marquee running.</summary>
procedure Start; override;
/// <summary>Stops the marquee running.</summary>
procedure Stop; override;
end;
{ TMarquee }
constructor TMarquee.Create(AOwner: TComponent);
begin
if ClassType = TMarquee then
raise ENoConstructException.CreateFmt(
'%s.Create can only be called by instantiating subclasses. '
+ 'To create the required instance call CreateInstance instead.',
[ClassName]
);
inherited Create(AOwner);
Smooth := True;
end;
class function TMarquee.CreateInstance(AOwner: TComponent): TMarquee;
begin
if IsBuiltInMarqueeAvailable then
Result := TNativeMarquee.Create(AOwner)
else
Result := TEmulatedMarquee.Create(AOwner);
end;
class function TMarquee.IsBuiltInMarqueeAvailable: Boolean;
begin
// We only use built in marquee on Vista or later when themes are enabled
Result := TOSInfo.IsVistaOrLater and ThemeServicesEx.ThemesEnabled;
end;
{ TEmulatedMarquee }
destructor TEmulatedMarquee.Destroy;
begin
Stop; // ensure timer has been stopped
inherited;
end;
procedure TEmulatedMarquee.Start;
begin
// timer used to move emulated marquee along
fTimer := TTimer.Create(nil);
fTimer.Interval := UpdateInterval;
fTimer.OnTimer := TickHandler;
fTimer.Enabled := True;
// initialise progress bar to suitable values for marquee
Position := 0;
Max := 48;
end;
procedure TEmulatedMarquee.Stop;
begin
if Assigned(fTimer) then
begin
fTimer.Enabled := False;
FreeAndNil(fTimer);
end;
end;
procedure TEmulatedMarquee.TickHandler(Sender: TObject);
begin
Position := (Position + 1) mod (Max + 1);
end;
{ TNativeMarquee }
procedure TNativeMarquee.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.Style := Params.Style or PBS_MARQUEE;
end;
procedure TNativeMarquee.Start;
begin
Perform(PBM_SETMARQUEE, 1, UpdateInterval); // activates marquee
end;
procedure TNativeMarquee.Stop;
begin
Perform(PBM_SETMARQUEE, 0, 0); // deactivates marquee
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.