Change caption and attributes of ShowMessage dialog
Solution 1
You can create your own custom dialogs by using delphi's CreateMessageDialog
function.
Example below:
var
Dlg: TForm;
begin
Dlg := CreateMessageDialog('message', mtInformation, [mbOk], mbOK);
// Treat Dlg like any other form
Dlg.Caption := 'Hello World';
try
// The message label is named 'message'
with TLabel(Dlg.FindComponent('message')) do
begin
Font.Style := [fsUnderline];
// extraordinary code goes here
end;
// The icon is named... icon
with TPicture(Dlg.FindComponent('icon')) do
begin
// more amazing code regarding the icon
end;
Dlg.ShowModal;
finally
Dlg.Free;
end;
and of course you can insert other components aswell into that form dynamically.
Solution 2
The dialog will use the contents of Application.Title
as the caption. So you could set this before calling ShowMessage
.
However, if you want to show multiple dialogs with different captions, it would be more convenient to call the Windows MessageBox
function. Certainly if you have an older version of Delphi this will result in a more native feel to your dialog.
procedure MyShowMessage(const Msg, Caption: string);
begin
MessageBox(GetParentWindowHandleForDialog, PChar(Msg), PChar(Caption), MB_OK);
end;
function GetParentWindowHandleForDialog: HWND;
begin
//we must be careful that the handle we use here doesn't get closed while the dialog is showing
if Assigned(Screen.ActiveCustomForm) then begin
Result := Screen.ActiveCustomForm.Handle;
end else if Assigned(Application.MainForm) then begin
Result := Application.MainFormHandle;
end else begin
Result := Application.Handle;
end;
end;
If you wish to control color and size then the most obvious option is to create your own dialog as a TForm
descendent.
Shirish11
Developer for Java,Delphi,C++. I enjoy working with MySQL.
Updated on July 09, 2022Comments
-
Shirish11 almost 2 years
In Delphi can you change the caption of the
ShowMessage
dialog because by default it is taking my exe name.And can I change the background color, size of the same?
-
Shirish11 over 12 yearsi cant find
MianWindowHandle
in BDS 2006 having a custom form as a dialog box seem to be an option -
David Heffernan over 12 yearsI misremembered. It's MainFormHandle in Delphi. I'm answering too many WinForms questions!!!
-
Shirish11 over 12 yearsbut it violates the
Showmodal
property ofShowmessages
-
David Heffernan over 12 yearsSorry, I can't understand that comment.
-
Shirish11 over 12 yearswhen u display a MessageDialog box using a
Showmessage
the control of the aplication goes to the Showmessage Dialog but in this case the control is still with the application when theMessageBox
is called -
David Heffernan over 12 yearsWindows
MessageBox
is modal also. A TForm descdendant can be modal by showing it withShowModal
. Both options I present can operator modally. -
Shirish11 over 12 years
-
Peter Turner over 12 years@David I had to write this for an internal app because some of the tech guys here wanted to have red buttons on important warning messages. It's essentially a hook that can draw on the 'canvas' of the dialog you pop up. You can draw in the place where the comments are.
-
David over 12 yearsI never knew
FindComponent
existed. +1!