Reposition A MessageDlg To Your (Delphi Application) Main Form

Reposition A MessageDlg To Your (Delphi Application) Main Form- DelphiGear.cn

In this tutorial, I’ll show you how to quickly redesign the MessageDlg box to have it positioned where ever you like, simply by passing it a location. A custom MessageDlgPos function you can use in your Delphi applications to decide if you want to center the dialog on your main form, owner form, screen center and so on simply by passing a particular type variable.

Start a New VCL Forms Application, after it’s loaded, press F12 to view the source code. Now, under implementation, create a new function – we’ll call it MessageDlg – that way you don’t have to try to remember what it’s called.


function MessageDlg(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; Pos: TPosition ): Integer;
begin
// code here
end;

What we’ve essentially done is recreate the original function name and variables used by Delphi (as available in the Dialogs unit). We’ve also given it the name “MessageDlg” so as to

a) override the default MessageDlg function and
b) not to confuse ourselves.

Next up is to use a function called “CreateMessageDialog”, what this function is does is self-explanatory, but to summarize: it returns a message dialog, however it does not display that dialog; which is what we want! We’ll then place this CreateMessageDialog within a with..do statement.


with CreateMessageDialog(Msg, DlgType, Buttons) do
begin
//
end;

Now, inside this block, we’ll make our position change.


// repositions the form according to the position passed
Position := Pos; 
// returns the result of the modal dialog form (mrOk, mrCancel etc).
Result := ShowModal; 

That’s it, you’re done. What should your code look like? Like this:


function MessageDlg(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; Pos: TPosition): Integer;
begin
  with CreateMessageDialog(Msg, DlgType, Buttons) do
  begin
    Position := Pos;
    Result := ShowModal;
  end;
end;

Now you have a MessageDlg whose position you can change simply by specifying it when calling the function. To use this function, you’d simply incorporate it into your code block as you would any other method.


procedure TForm1.Button1Click(Sender: TObject);
begin
  // reposition form to the center of my main application form
  MessageDlg('Test Message', mtInformation, [mbOK], poMainFormCenter); 
end;

Now that you have the dialog displayed, do you know what is the result if Dialog Closed Using the [X] Button in the Title Bar?

As a Template?

As an extra tidbit for you. Highlight the modified code you’ve just written, now do the following:

  1. In your IDE, click on View,
  2. Click on Templates,
  3. Click on the “New Code Template” button under the Templates window,
  4. You will be given an XML layout:
    The caret will automatically be placed between inverted commas, just after
  5. Under <description> you can put whatever you like, such as “Code to reposition MessageDlg”
  6. Under <author>, you can put your name; or leave it blank or “Sebastien is awesome!” Your choice.
  7. Under <code>, put “Delphi” (without quotes) between the quotes after <code> language. You will have noticed by now that the code you highlighted has already been entered into the template, under this tag as well.
  8. Now hit Ctrl+S to save, call the template filename whatever you wish.

Using the MessageDlg Template

Go back to your main forms’ unit and delete the code you just wrote for your new MessageDlg.

Now press Ctrl+J, this will bring up the quick templates window – the same window you’d use to quickly write out a try..except, or a try..finally.

Type “MessageDlg” or “MsgDlg” or whatever name you put under Note: Templates can be used in any project on your computer, however, in order to use this template on another computer you’ll have to either recreate the template using the process above, or by copying the template file to Delphi’s template directory on the new computer (typically: C:\Users\USERNAME\Documents\RAD Studio\code_templates).

Btw, if you want some other text on the buttons, take a look at : Hijack consts.pas to have Country / Language Specific Messages

MessageDlgPos Exists in Dialogs.pas

Yes, you’ve noted correctly, the MessageDlgPos already is available to you in the Dialogs unit. The original function uses X and Y parameters to specify the screen coordinates where the dialog should appear. The MessageDlgPos in this article does positioning relative to the main form of the application.