Menu

[r2591]: / trunk / Src / UCommonDlg.pas  Maximize  Restore  History

Download this file

376 lines (336 with data), 13.8 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
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
{
* 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) 2009-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* A set of classes that assist in hooking and handling common dialog box
* messages. Also provides helper methods for handling dialog box messages.
}
unit UCommonDlg;
interface
uses
// Delphi
Messages, Dialogs, CommDlg, Windows,
// Project
UBaseObjects;
type
{
TCommonDlgHelper:
Static class that provides methods to help with common dialog tasks not
handled via the dialog hook function.
}
TCommonDlgHelper = class(TNoConstructObject)
public
class function IsHelpMessage(const Msg: TMessage): Boolean;
{Checks if a message is a common dialog help messages.
@param Msg [in] Message to be checked.
@return True if help message, False if not.
}
class function ShowHelp(const Keyword: string): Boolean;
{Displays a help topic based on an a-link keyword.
@param Keyword [in] A-link keyword that specifies help topic. No topic
displayed if Keyword is empty string.
}
end;
{
TCommonDlgHookFn:
Type of common dialog box hook (callback). These procedures receive messages
and notifications intended for a dialog box and can handle them or pass them
on to the dialog box procedure.
@param Wnd [in] Handle to the dialog box for which message is intended.
@param Msg [in] Message id.
@param WParam [in] Additional information depending on value of Msg.
@param LParam [in] Additional information depending on value of Msg.
@return 0 to pass message to dialog box procedure or non-zero to cause the
dialog box procedure to ignore the message.
}
TCommonDlgHookFn = function(Wnd: HWnd; Msg: UINT; WParam: Integer;
LParam: Integer): UINT; stdcall;
{
TCommonDlgHook:
Abstract base class for classes that hooks and handles dialog hook messages
on behalf of an associated common dialog box.
}
TCommonDlgHook = class abstract(TObject)
strict private
fDlg: TCommonDialog; // Reference to related dialog box
fOldHookFn: TCommonDlgHookFn; // Value of OldHookFn property
strict protected
property OldHookFn: TCommonDlgHookFn read fOldHookFn write fOldHookFn;
{Dialog's previous (default?) hook function}
public
constructor Create(const Dlg: TCommonDialog);
{Class constructor. Sets up object and associates with a common dialog
box.
@param Dlg [in] Related dialog box. Must not be nil.
}
procedure Initialise(var DialogData); virtual; abstract;
{Applies new dialog box hook function and ensures dialog box has reference
back to this object.
@param DialogData [in/out] Contains data about dialog box. Updated with
new hook function and reference to associated hook object.
}
function CallHookFn(Wnd: HWnd; Msg: UINT; WParam, LParam: Integer): UINT;
{Calls associated dialog hook function with the required parameters. A
reference to the dialog function must be recorded in the OldHookFn
property.
@param Wnd [in] Handle to the dialog box for which message is intended.
Passed to hook function.
@param Msg [in] Message id. Passed to hook function.
@param WParam [in] Additional information depending on value of Msg.
Passed to hook function.
@param LParam [in] Additional information depending on value of Msg.
Passed to hook function.
@return Value returned from hook function.
}
procedure AlignDlg;
{Aligns associated dialog box to its owner.
}
end;
{
TFileDlgHook:
Class that hooks and handles dialog hook messages on behalf of file open /
save common dialog dailogs using a custom hook function. Aligns dialog box
over owner.
}
TFileDlgHook = class(TCommonDlgHook)
public
procedure Initialise(var DialogData); override;
{Applies new dialog box hook function and ensures file dialog box has
reference back to this object.
@param DialogData [in/out] Contains address of file dialog's current
hook function. Receives new hook function and reference to this hook
object.
}
class function RecoverInstance(P: POpenFileName): TFileDlgHook;
overload;
{Recovers an instance an object of this class from TOpenFileName data.
@param P [in] Pointer to TOpenFilename structure that contains reference
to a TFileDlgHook instance in one of its field.
}
class function RecoverInstance(P: POFNotify): TFileDlgHook;
overload;
{Recovers an instance an object of this class from TOFNotify data.
@param P [in] Pointer to TOFNotify structure that contains reference to
a TFileDlgHook instance in one of its field.
}
end;
{
TColorDlgHook:
Class that hooks and handles dialog hook messages on behalf of color common
dialog dailogs using a custom hook function. Aligns dialog box over owner.
}
TColorDlgHook = class(TCommonDlgHook)
public
procedure Initialise(var DialogData); override;
{Applies new dialog box hook function and ensures colour dialog box has
reference back to this object.
@param DialogData [in/out] Contains address of colour dialog's current
hook function. Receives new hook function and reference to this hook
object.
}
class function RecoverInstance(P: PChooseColor): TColorDlgHook;
{Recovers an instance an object of this class from TChooseColor data.
@param P [in] Pointer to TChooseColor structure that contains reference
to a TColorDlgHook instance in one of its field.
}
end;
implementation
uses
// Project
UDlgHelper, UHelpMgr;
{ TCommonDlgHelper }
class function TCommonDlgHelper.IsHelpMessage(const Msg: TMessage): Boolean;
{Checks if a message is a common dialog help messages.
@param Msg [in] Message to be checked.
@return True if help message, False if not.
}
begin
Result := Msg.Msg = RegisterWindowMessage(CommDlg.HELPMSGSTRING);
end;
class function TCommonDlgHelper.ShowHelp(const Keyword: string): Boolean;
{Displays a help topic based on an a-link keyword.
@param Keyword [in] A-link keyword that specifies help topic. No topic
displayed if Keyword is empty string.
}
begin
Result := Keyword <> '';
if Result then
HelpMgr.ShowHelp(Keyword);
end;
{ TCommonDlgHook }
function CallHookFunc(Fn: TCommonDlgHookFn; Wnd: HWnd; Msg: UINT;
WParam, LParam: Integer): UINT;
{Calls a dialog hook function with the required parameters. Must be a standard
function, not a method.
@param Fn [in] Hook function to be called. Must be non nil.
@param Wnd [in] Handle to the dialog box for which message is intended.
Passed to Fn.
@param Msg [in] Message id. Passed to Fn.
@param WParam [in] Additional information depending on value of Msg. Passed
to Fn.
@param LParam [in] Additional information depending on value of Msg. Passed
to Fn.
@return Value returned from Fn.
}
begin
Assert(Assigned(Fn), 'CallHookFunc: Fn is nil');
Result := Fn(Wnd, Msg, LParam, WParam)
end;
procedure TCommonDlgHook.AlignDlg;
{Aligns associated dialog box to its owner.
}
begin
TDlgAligner.AlignToOwner(fDlg);
end;
function TCommonDlgHook.CallHookFn(Wnd: HWnd; Msg: UINT; WParam,
LParam: Integer): UINT;
{Calls associated dialog hook function with the required parameters. A
reference to the dialog function must be recorded in the OldHookFn property.
@param Wnd [in] Handle to the dialog box for which message is intended.
Passed to hook function.
@param Msg [in] Message id. Passed to hook function.
@param WParam [in] Additional information depending on value of Msg. Passed
to hook function.
@param LParam [in] Additional information depending on value of Msg. Passed
to hook function.
@return Value returned from hook function.
}
begin
Assert(Assigned(OldHookFn), ClassName + '.CallHookFn: OldHookFn is nil');
Result := CallHookFunc(OldHookFn, Wnd, Msg, LParam, WParam);
end;
constructor TCommonDlgHook.Create(const Dlg: TCommonDialog);
{Class constructor. Sets up object and associates with a common dialog box.
@param Dlg [in] Related dialog box. Must not be nil.
}
begin
Assert(Assigned(Dlg), ClassName + '.Create: Dlg is nil');
inherited Create;
fDlg := Dlg;
end;
{ TFileDlgHook }
function NewExplorerHook(Wnd: HWnd; Msg: UINT; WParam: WPARAM;
LParam: LPARAM): UINT; stdcall;
{Replacement explorer hook function called by Windows to process dialog box
messages. Original hook handles only WM_INITDIALOG message and CDN_INITDONE
notification. This replacement passes WM_INITDIALOG messages to original
hook but handles CDN_INITDONE to align the dialog box (original hook centres
it). All other messages are passed to original hook. This function is used by
and is associated with TFileDlgHook objects.
@param Wnd [in] Window handle of dialog box.
@param Msg [in] Identifies message being passed to hook.
@param WParam [in] Message parameter. Usage depends on message.
@param LParam [in] Message parameter. Usage depends on message.
@return Message specific return value.
}
var
Hook: TFileDlgHook; // object that handles hook messages
begin
// Set default result passed back to windows
Result := 0;
if Msg = WM_INITDIALOG then
begin
// Dialog initialising: pass on to original hook function
Hook := TFileDlgHook.RecoverInstance(POpenFileName(LParam))
as TFileDlgHook;
Result := Hook.CallHookFn(Wnd, Msg, LParam, WParam);
end
else if Msg = WM_NOTIFY then
begin
// Get reference to dialog box object from data structure
Hook := TFileDlgHook.RecoverInstance(POFNotify(LParam))
as TFileDlgHook;
if POFNotify(LParam)^.hdr.code = CDN_INITDONE then
// Dialog intialization complete: align the dialog box. We don't call old
// hook function since all this does is centre the dialog box!
// Windows ignores return value (we leave as default 0)
Hook.AlignDlg
else
// Other notification: pass on to original hook function
Result := Hook.CallHookFn(Wnd, Msg, WParam, LParam);
end;
end;
procedure TFileDlgHook.Initialise(var DialogData);
{Applies new dialog box hook function and ensures file dialog box has
reference back to this object.
@param DialogData [in/out] Contains address of file dialog's current hook
function. Receives new hook function and reference to this hook object.
}
begin
OldHookFn := TOpenFilename(DialogData).lpfnHook;
TOpenFilename(DialogData).lpfnHook := NewExplorerHook;
TOpenFilename(DialogData).lCustData := Integer(Self);
end;
class function TFileDlgHook.RecoverInstance(P: POpenFileName): TFileDlgHook;
{Recovers an instance an object of this class from TOpenFileName data.
@param P [in] Pointer to TOpenFilename structure that contains reference to
a TFileDlgHook instance in one of its field.
}
begin
Result := TFileDlgHook(P^.lCustData)
end;
class function TFileDlgHook.RecoverInstance(P: POFNotify): TFileDlgHook;
{Recovers an instance an object of this class from TOFNotify data.
@param P [in] Pointer to TOFNotify structure that contains reference to a
TFileDlgHook instance in one of its field.
}
begin
Result := RecoverInstance(P^.lpOFN);
end;
{ TColorDlgHook }
function NewCCHook(Wnd: HWnd; Msg: UINT; WParam: WPARAM;
LParam: LPARAM): UINT; stdcall;
{Replacement hook function called by Windows to process dialog box messages.
Original hook handles only WM_INITDIALOG message. This replacement passes
WM_INITDIALOG messages to original hook and then aligns dialog box as
required. No other messages are handled by this hook or by original hook. This
function is used by and is associated with TFileDlgHook objects.
@param Wnd [in] Window handle of dialog box.
@param Msg [in] Identifies message being passed to hook.
@param WParam [in] Message parameter. Usage depends on message.
@param LParam [in] Message parameter. Usage depends on message.
@return Message specific return value.
}
var
Hook: TColorDlgHook; // object that handles hook messages
begin
// Set default result passed back to Windows
Result := 0;
if Msg = WM_INITDIALOG then
begin
// Dialog initialising: pass on to original hook function which centres
// dialog box (amongst other work), then re-align dialog.
Hook := TColorDlgHook.RecoverInstance(PChooseColor(LParam))
as TColorDlgHook;
Result := Hook.CallHookFn(Wnd, Msg, WParam, LParam);
Hook.AlignDlg;
end;
end;
procedure TColorDlgHook.Initialise(var DialogData);
{Applies new dialog box hook function and ensures colour dialog box has
reference back to this object.
@param DialogData [in/out] Contains address of colour dialog's current hook
function. Receives new hook function and reference to this hook object.
}
begin
OldHookFn := TChooseColor(DialogData).lpfnHook;
TChooseColor(DialogData).lpfnHook := NewCCHook;
TChooseColor(DialogData).lCustData := Integer(Self);
end;
class function TColorDlgHook.RecoverInstance(P: PChooseColor): TColorDlgHook;
{Recovers an instance an object of this class from TChooseColor data.
@param P [in] Pointer to TChooseColor structure that contains reference to
a TColorDlgHook instance in one of its field.
}
begin
Result := TColorDlgHook(P^.lCustData)
end;
initialization
Dialogs.UseLatestCommonDialogs := False;
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.