Recent

Author Topic: [SOLVED] Center ShowMessage box center in the center of the main form  (Read 8158 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Hi All,
I am trying to center a show message box with ShowMessagePos in the center of the (main) form.
I can get the main forms attributes (left - width, top - height) for calculating the center of the main form.

How would I actually get the coordinates for the show message box in order for its center to correspond with the center of the main form?

Thank you

Lazarus 1.2.6 32 b, W7 - 64 b
« Last Edit: December 22, 2014, 09:54:29 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Center ShowMessage box center in the center of the main form
« Reply #1 on: December 22, 2014, 03:47:37 am »
You could use CreateMessageDialog instead:
Code: [Select]
uses
  ..., Dialogs;
...
procedure TForm1.Button1Click(Sender: TObject);
var
  f: TForm;
begin
  //ShowMessagePos() replacement
  f := CreateMessageDialog('Test', mtCustom, [mbOK]);
  f.Position := poDesigned;
  f.Left:= Left + (Width - f.Width) div 2;
  f.Top := Top + (Height - f.Height) div 2;
  f.ShowModal;
end;

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Center ShowMessage box center in the center of the main form
« Reply #2 on: December 22, 2014, 03:51:43 pm »
I will just add some possible and additional computes to engkin's code, which are relative to any child-window centering request:

Code: [Select]
...
  if f.Left<0 then f.Left := 0
  else if f.Left>(Screen.Width-f.Width) then f.Left := (Screen.Width-f.Width);
  if f.Top<0 then f.Top := 0
  else if f.Top>(Screen.Height-f.Height) then f.Top := (Screen.Height-f.Height);
...

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Center ShowMessage box center in the center of the main form
« Reply #3 on: December 22, 2014, 06:04:46 pm »
MessageDlgPos()?

Bart

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Center ShowMessage box center in the center of the main form
« Reply #4 on: December 22, 2014, 06:10:13 pm »
tudi_x wants to know Width and Height in order to center dialog.

If I would need to do so, I would create my own dialog form in my application. But maybbe tudi_x can make a feature request on BugTracker.
« Last Edit: December 22, 2014, 07:00:23 pm by typo »

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Center ShowMessage box center in the center of the main form
« Reply #5 on: December 22, 2014, 07:49:02 pm »
OK then, as suggested use CreateMessageDlg, but unike the example above set Position to poCenterMainFrom?

Or use my ExtMsgDlg unit, from a long time ago.
(See comments in the code about positioning)

Bart

tudi_x

  • Hero Member
  • *****
  • Posts: 532
[SOLVED] Center ShowMessage box center in the center of the main form
« Reply #6 on: December 22, 2014, 09:54:08 pm »
Thank you all.

The below works very nice:

Code: [Select]
var
  f : TForm;   

begin
   f := CreateMessageDialog('Test', mtCustom, [mbOK]);
   f.Position := poOwnerFormCenter;
   f.ShowModal;   
end;

Lazarus 2.0.2 64b on Debian LXDE 10

 

TinyPortal © 2005-2018