Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change caption and attributes of ShowMessage dialog

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?

like image 280
Shirish11 Avatar asked Oct 13 '11 12:10

Shirish11


People also ask

What are the 4 JOptionPane dialog boxes?

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.

How do you change the font on JOptionPane?

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.

What is the main difference between dialog and frame?

Frame vs DialogFrame has maximize and minimize buttons but Dialog doesn't have.


2 Answers

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.

like image 54
zz1433 Avatar answered Sep 17 '22 19:09

zz1433


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.

like image 42
David Heffernan Avatar answered Sep 19 '22 19:09

David Heffernan