Skip to content

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 ​

  1. Add a Search path to the StyledComponents\Source folder (not needed if you installed the library with the Setup)

  2. Add Vcl.StyledMessagesHooks as the last unit in the uses clause of every unit that shows messages:

Pascal
uses
  Winapi.Windows, System.SysUtils, System.Classes,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StyledMessagesHooks; //must be the last unit
  1. Optionally, configure the look of the dialogs once, in the project file (dpr):
Pascal
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:

Pascal
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
MessageDlgStyledMessageDlg
MessageDlgPosStyledMessageDlgPos
TaskMessageDlgStyledTaskMessageDlg
TaskDlgPosStyledTaskMessageDlgPos
ShowMessageStyledShowMessage

Every call shape of the standard routines is supported, from 2 to 5 arguments, including the variants with the DefaultButton parameter:

Pascal
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.StyledMessagesHooks comes after Vcl.Dialogs; otherwise the standard routines are called.
  • Per unit: the replacement applies only to the units that list Vcl.StyledMessagesHooks in their uses clause. Add it to every unit (form, data module, ...) that shows messages.
  • Shadowing, not overloading: the routines intentionally hide the Vcl.Dialogs ones and are not marked overload. Marking them overload would put them in the same candidate set as the identical Vcl.Dialogs signatures, 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 ShowMessageFmt or the TTaskDialog component use the explicit styled versions (StyledShowMessageFmt, TStyledTaskDialog).
  • Explicit calls: if you prefer not to use the interposer, add Vcl.StyledTaskDialog to the uses clause and call the styled routines directly (StyledMessageDlg, StyledTaskMessageDlg, StyledShowMessage, ...).

See Also ​

Released under Apache License, Version 2.0.