Interposer Unit (Vcl.StyledMessagesHooks)
Using the Vcl.StyledMessagesHooks.pas unit you can replace the standard Delphi message dialogs (MessageDlg, MessageDlgPos, TaskMessageDlg, TaskDlgPos and ShowMessage) with the Styled Dialogs available in this library, without changing a single call in your code.
It works like the Vcl.StyledComponentsHooks interposer for buttons: the unit declares routines with the same names as the ones in Vcl.Dialogs, so when it is the last unit in the uses clause the compiler resolves the calls to the styled versions instead of the standard ones.
How to use it
Add a Search path to the StyledComponents\Source folder (not needed if you installed the library with the Setup)
Add
Vcl.StyledMessagesHooksas the last unit in theusesclause of every unit that shows messages:
uses
Winapi.Windows, System.SysUtils, System.Classes,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StyledMessagesHooks; //must be the last unit- Optionally, configure the look of the dialogs once, in the project file (dpr):
uses
Vcl.Forms,
Vcl.StyledButton,
Vcl.ButtonStylesAttributes,
Vcl.StyledTaskDialog,
...
begin
Application.Initialize;
//Default rendering style for the buttons of the Styled Dialogs
TStyledTaskDialog.RegisterDefaultRenderingStyle(btRounded, BOOTSTRAP_FAMILY);
//Font and button family for the Styled Dialogs
InitializeStyledTaskDialogs(True, Screen.MessageFont, False, BOOTSTRAP_FAMILY);
...
Application.Run;
end.Your existing code stays unchanged, and now shows styled dialogs:
ShowMessage('File saved successfully!');
if MessageDlg('Delete the selected file?', mtConfirmation,
[mbYes, mbNo], 0, mbNo) = mrYes then
DeleteSelectedFile;
TaskMessageDlg('Connection error', 'Unable to connect to the server.',
mtError, [mbOK], 0);Replaced routines
| Standard call (Vcl.Dialogs) | Styled routine called |
|---|---|
MessageDlg | StyledMessageDlg |
MessageDlgPos | StyledMessageDlgPos |
TaskMessageDlg | StyledTaskMessageDlg |
TaskDlgPos | StyledTaskMessageDlgPos |
ShowMessage | StyledShowMessage |
Every call shape of the standard routines is supported, from 2 to 5 arguments, including the variants with the DefaultButton parameter:
function MessageDlg(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons = [mbOK]; HelpCtx: Longint = 0;
DefaultButton: TMsgDlgBtn = NO_DEFAULT_BUTTON): Integer;
function MessageDlgPos(const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint;
X: Integer = -1; Y: Integer = -1;
DefaultButton: TMsgDlgBtn = NO_DEFAULT_BUTTON): Integer;
function TaskMessageDlg(const Title, Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint;
DefaultButton: TMsgDlgBtn = NO_DEFAULT_BUTTON): Integer;
function TaskDlgPos(const Title, Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Longint;
X: Integer = -1; Y: Integer = -1;
DefaultButton: TMsgDlgBtn = NO_DEFAULT_BUTTON): Integer;
procedure ShowMessage(const Msg: string);NO_DEFAULT_BUTTON is a marker constant meaning "no explicit default button": when DefaultButton is omitted, the styled dialog chooses the default button exactly as the standard one does.
Important Notes
- Last unit in the uses clause: the interposer works only if
Vcl.StyledMessagesHookscomes afterVcl.Dialogs; otherwise the standard routines are called. - Per unit: the replacement applies only to the units that list
Vcl.StyledMessagesHooksin theirusesclause. Add it to every unit (form, data module, ...) that shows messages. - Shadowing, not overloading: the routines intentionally hide the
Vcl.Dialogsones and are not markedoverload. Marking themoverloadwould put them in the same candidate set as the identicalVcl.Dialogssignatures, and every call would fail with E2251 Ambiguous overloaded call. The optional trailing parameters cover all the standard call shapes instead. - Qualified calls are not replaced: a call written as
Vcl.Dialogs.MessageDlg(...)still shows the standard dialog. - Other routines: only the routines listed above are replaced. For
ShowMessageFmtor theTTaskDialogcomponent use the explicit styled versions (StyledShowMessageFmt,TStyledTaskDialog). - Explicit calls: if you prefer not to use the interposer, add
Vcl.StyledTaskDialogto theusesclause and call the styled routines directly (StyledMessageDlg,StyledTaskMessageDlg,StyledShowMessage, ...).
See Also
- StyledTaskDialog - Overview of the Styled Dialogs
- TStyledTaskDialog Reference - Complete technical documentation
- Interposer Unit (Vcl.StyledComponentsHooks) - The same technique applied to buttons
