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?
The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.
There is an easy way to change the default font in JOptionPane . Pass a string modified in html format, which means you can either use <font> tag or even CSS. Using <font> tag.
Frame vs DialogFrame has maximize and minimize buttons but Dialog doesn't have.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With