Menu

[r3166]: / trunk / Src / UThemesEx.pas  Maximize  Restore  History

Download this file

348 lines (310 with data), 11.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
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
{
* 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) 2006-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements class that extends TThemeServices with new methods and adds
* support for multiple event handlers for the OnThemeChange event.
}
unit UThemesEx;
interface
uses
// Delphi
Classes, Windows, Graphics, Themes, Messages,
// Project
UMultiCastEvents, UMessageWindow;
type
{
TThemeServicesEx:
Extends TThemeServices with methods that get size of a theme element and
that provide easy access to drawing an element.
}
TThemeServicesEx = class(TThemeServices)
strict private
var
fMessageWdw: TMessageWindow; // Intercepts theme change messages
fThemeChanges: TMultiCastEvents; // Support for multi-cast events
function InternalGetElementSize(
const Details: TThemedElementDetails): TSize;
{Gets size of a theme part in a specified state.
@param Details [in] Specifies element, part and state.
@return Size of element.
}
procedure InternalDrawElement(const Details: TThemedElementDetails;
const Bmp: TBitmap; const Rect: TRect); overload;
{Draw a theme part in specified state on an area of a bitmap.
@param Details [in] Specifies element, part and state.
@param Bmp [in] Bitmap on which part is drawn.
@param Rect [in] Area of bitmap receiving part.
}
strict protected
procedure DoOnThemeChange; override;
{Triggers multiple OnThemeChange events if themes may have changed.
}
public
constructor Create; override;
{Class constructor. Sets up object.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
function GetElementSize(const Elem: TThemedButton): TSize; overload;
{Gets size of a themed button element. Not all button elements support
this method.
@param Elem [in] Element we want size of.
@return Size of element.
}
procedure DrawElement(const Elem: TThemedComboBox; const Bmp: TBitmap;
const Rect: TRect); overload;
{Draws a themed combo box element on a bitmap.
@param Elem [in] Element to be drawn.
@param Bmp [in] Bitmap on which to draw element.
@param Rect [in] Area of bitmap in which to draw element.
}
procedure DrawElement(const Elem: TThemedButton; const Bmp: TBitmap;
const Rect: TRect); overload;
{Draws a themed button element on a bitmap.
@param Elem [in] Element to be drawn.
@param Bmp [in] Bitmap on which to draw element.
@param Rect [in] Area of bitmap in which to draw element.
}
procedure DrawElement(const Elem: TThemedTab; const Bmp: TBitmap;
const Rect: TRect); overload;
{Draws a themed tab element on a bitmap.
@param Elem [in] Element to be drawn.
@param Bmp [in] Bitmap on which to draw element.
@param Rect [in] Area of bitmap in which to draw element.
}
function GetTabBodyColour: TColor;
{Returns a reasonable approximation of the colour of an XP themed tab
sheets. Tab sheets are gradient filled, so a representative colour is
returned that looks OK painted on to a tab sheet.
@return Suggested colour.
}
procedure AddChangeEventHandler(const Evt: TNotifyEvent);
{Adds an event handler to list of handlers that are triggered when theme
changes.
@param Evt [in] Event handler to be added to list.
}
procedure RemoveChangeEventHandler(const Evt: TNotifyEvent);
{Removes an event handler from list of handlers that are triggered when
theme changes. Does nothing if handler not in list.
@param Evt [in] Event handler to be removed from list.
}
procedure HiddenWindowHandler(Sender: TObject; var Msg: TMessage;
var Handled: Boolean);
{Handles messages from the hidden window. Responds only to WM_WININICHANGE
messages by notifying that a possible theme changed has occured.
@param Sender [in] Not used.
@param Msg [in/out] Message to be handled. Left unchanged.
@param Handled [in/out] Set to true if message is WM_WININICHANGE.
}
end;
function ThemeServicesEx: TThemeServicesEx;
{Casts ThemeServices object to its actual type.
@return ThemeServices object cast to TThemeServicesEx.
}
implementation
uses
// Delphi
SysUtils, UxTheme, ComObj,
// Project
UStructs;
{
Implementation note re getting size of and drawing element types
----------------------------------------------------------------
To extend TThemeServicesEx to get size of and to draw specified element types,
for example TThemedMenu, create overloaded versions of DrawElement and
GetElementSize with the Elem parameters of required type. Then implement
DrawElement using
InternalDrawElement(GetElementDetails(Elem), Bmp, Rect);
and GetElementSize using
Result := InternalGetElementSize(GetElementDetails(Elem));
}
function ThemeServicesEx: TThemeServicesEx;
{Casts ThemeServices object to its actual type.
@return ThemeServices object cast to TThemeServicesEx.
}
begin
Result := Themes.ThemeServices as TThemeServicesEx;
end;
{ TThemeServicesEx }
procedure TThemeServicesEx.AddChangeEventHandler(const Evt: TNotifyEvent);
{Adds an event handler to list of handlers that are triggered when theme
changes.
@param Evt [in] Event handler to be added to list.
}
begin
if not Assigned(fThemeChanges) then
fThemeChanges := TMultiCastEvents.Create;
fThemeChanges.AddHandler(Evt);
end;
constructor TThemeServicesEx.Create;
{Class constructor. Sets up object.
}
begin
inherited;
fMessageWdw := TMessageWindow.Create;
fMessageWdw.OnMessage := HiddenWindowHandler;
end;
destructor TThemeServicesEx.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fThemeChanges);
FreeAndNil(fMessageWdw);
inherited;
end;
procedure TThemeServicesEx.DoOnThemeChange;
{Triggers multiple OnThemeChange events if themes may have changed.
}
begin
inherited; // triggers OnThemeChange event
if Assigned(fThemeChanges) then
fThemeChanges.TriggerEvents;
end;
procedure TThemeServicesEx.DrawElement(const Elem: TThemedComboBox;
const Bmp: TBitmap; const Rect: TRect);
{Draws a themed combo box element on a bitmap.
@param Elem [in] Element to be drawn.
@param Bmp [in] Bitmap on which to draw element.
@param Rect [in] Area of bitmap in which to draw element.
}
begin
Assert(ThemesEnabled, ClassName + '.DrawElement: Themes not enabled');
InternalDrawElement(GetElementDetails(Elem), Bmp, Rect);
end;
procedure TThemeServicesEx.DrawElement(const Elem: TThemedTab;
const Bmp: TBitmap; const Rect: TRect);
{Draws a themed tab element on a bitmap.
@param Elem [in] Element to be drawn.
@param Bmp [in] Bitmap on which to draw element.
@param Rect [in] Area of bitmap in which to draw element.
}
begin
Assert(ThemesEnabled, ClassName + '.DrawElement: Themes not enabled');
InternalDrawElement(GetElementDetails(Elem), Bmp, Rect);
end;
procedure TThemeServicesEx.DrawElement(const Elem: TThemedButton;
const Bmp: TBitmap; const Rect: TRect);
{Draws a themed button element on a bitmap.
@param Elem [in] Element to be drawn.
@param Bmp [in] Bitmap on which to draw element.
@param Rect [in] Area of bitmap in which to draw element.
}
begin
Assert(ThemesEnabled, ClassName + '.DrawElement: Themes not enabled');
InternalDrawElement(GetElementDetails(Elem), Bmp, Rect);
end;
function TThemeServicesEx.GetElementSize(const Elem: TThemedButton): TSize;
{Gets size of a themed button element. Not all button elements support this
method.
@param Elem [in] Element we want size of.
@return Size of element.
}
begin
Assert(ThemesEnabled, ClassName + '.GetElementSize: Themes not enabled');
Result := InternalGetElementSize(GetElementDetails(Elem));
end;
function TThemeServicesEx.GetTabBodyColour: TColor;
{Returns a reasonable approximation of the colour of an XP themed tab sheets.
Tab sheets are gradient filled, so a representative colour is returned that
looks OK painted on to a tab sheet.
@return Suggested colour.
}
var
Bmp: TBitmap; // bitmap on which a tab sheet is drawn
begin
Bmp := TBitmap.Create;
try
Bmp.Width := 16;
Bmp.Height := 16;
// draw the tab pane
DrawElement(ttPane, Bmp, TRectEx.Create(0, 0, Bmp.Width, Bmp.Height));
// take a representative colour from drawn pane
Result := Bmp.Canvas.Pixels[2, 3];
finally
FreeAndNil(Bmp);
end;
end;
procedure TThemeServicesEx.HiddenWindowHandler(Sender: TObject;
var Msg: TMessage; var Handled: Boolean);
{Handles messages from the hidden window. Responds only to WM_WININICHANGE
messages by notifying that a possible theme changed has occured.
@param Sender [in] Not used.
@param Msg [in/out] Message to be handled. Left unchanged.
@param Handled [in/out] Set to true if message is WM_WININICHANGE.
}
begin
case Msg.Msg of
WM_WININICHANGE:
begin
ApplyThemeChange;
Handled := True;
end;
end;
end;
procedure TThemeServicesEx.InternalDrawElement(
const Details: TThemedElementDetails; const Bmp: TBitmap;
const Rect: TRect);
{Draw a theme part in specified state on an area of a bitmap.
@param Details [in] Specifies element, part and state.
@param Bmp [in] Bitmap on which part is drawn.
@param Rect [in] Area of bitmap receiving part.
}
begin
DrawElement(Bmp.Canvas.Handle, Details, Rect);
end;
function TThemeServicesEx.InternalGetElementSize(
const Details: TThemedElementDetails): TSize;
{Gets size of a theme part in a specified state.
@param Details [in] Specifies element, part and state.
@return Size of element.
}
var
DC: HDC; // device context required by GetThemePartSize
begin
// Use desktop device context
DC := GetDC(0);
try
// Get size of element that would be drawn
OleCheck(
UxTheme.GetThemePartSize(
Theme[Details.Element], // handle to window's theme data
DC, // device context to select fonts into
Details.Part, // specifies part to calculate size of
Details.State, // specifies state of part
nil, // rectangle of part drawing dest (may be nil)
TS_DRAW, // type of size to retrieve (drawing size)
Result // dimensions of specified part
)
);
finally
ReleaseDC(0, DC);
end;
end;
procedure TThemeServicesEx.RemoveChangeEventHandler(
const Evt: TNotifyEvent);
{Removes an event handler from list of handlers that are triggered when theme
changes. Does nothing if handler not in list.
@param Evt [in] Event handler to be removed from list.
}
begin
if Assigned(fThemeChanges) then
begin
fThemeChanges.RemoveHandler(Evt);
// we free list if it is empty
if fThemeChanges.Count = 0 then
FreeAndNil(fThemeChanges);
end;
end;
initialization
// Ensure Themes unit creates ThemeServices singleton as type TThemeServicesEx
// rather than TThemeServices.
ThemeServicesClass := TThemeServicesEx;
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.