Recent

Author Topic: [SOLVED-again] Centering Text in ShowMessage  (Read 8716 times)

WickedDum

  • Full Member
  • ***
  • Posts: 211
[SOLVED-again] Centering Text in ShowMessage
« on: November 07, 2016, 06:15:13 am »
Hello!

How does one center the text in a ShowMessage() construct?

The only way I can see is to add/remove spaces and compile (again) until it is pleasing to me. 

Is there another way?

Thanks!!
« Last Edit: November 08, 2016, 07:50:36 pm by WickedDum »
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: Centering Text in ShowMessage
« Reply #1 on: November 07, 2016, 06:41:12 am »
I am using that function for this issue:
Note: But I am not sure for cross compiling, maybe need a getwindowrect for linux if you need.

Code: Pascal  [Select][+][-]
  1. uses Windows,Dialogs;
  2. .
  3. .
  4.  
  5. function MessageDlgCenter(const Msg: string; DlgType: TMsgDlgType;
  6.   Buttons: TMsgDlgButtons): Integer;
  7. var R: TRect;
  8. begin
  9.   if not Assigned(Screen.ActiveForm) then
  10.   begin
  11.     Result := MessageDlg(Msg, DlgType, Buttons, 0);
  12.   end else
  13.   begin
  14.     with CreateMessageDialog(Msg, DlgType, Buttons) do
  15.     try
  16.       GetWindowRect(Screen.ActiveForm.Handle, R);
  17.       Left := R.Left + ((R.Right - R.Left) div 2) - (Width div 2);
  18.       Top := R.Top + ((R.Bottom - R.Top) div 2) - (Height div 2);
  19.       Result := ShowModal;
  20.     finally
  21.       Free;
  22.     end;
  23.   end;
  24. end;
  25.  
  26. //usage:
  27.   MessageDlgCenter('Example of messagedlgcenter it is a long message ....',mtInformation,[mbOK]);
  28.  
  29.  

« Last Edit: November 07, 2016, 06:43:58 am by tr_escape »

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: Centering Text in ShowMessage
« Reply #2 on: November 07, 2016, 07:50:40 am »
If you know the char width for the font ,so you can calculate the number of chars per line, you can use this function:
Code: Pascal  [Select][+][-]
  1. // S: string to center, Outerlength: Maximum number of chars per line
  2. function CenteredText(const S:String;Outerlength:integer):String;
  3. var
  4.   t:integer;
  5. begin
  6.   t := (Outerlength - Length(s)) div 2;
  7.   Result := Format('%*s%s%*s', [t, ' ', s, t, ' ']) ;
  8.   If Length(Result) > Outerlength then // sanity check
  9.   begin
  10.       Result := S;
  11.       SetLength(Result,Outerlength); //truncates
  12.   end;
  13. end;
  14.  
Of course this is only precise for a fixed font
Of course in textmode this already works.

Poor man's approximation for outerlength is 2 + ClientWidth div Font.Size
« Last Edit: November 07, 2016, 08:19:23 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Centering Text in ShowMessage
« Reply #3 on: November 07, 2016, 12:55:44 pm »
How does one center the text in a ShowMessage() construct?
...
Is there another way?

Write your own dialog.
It's probably the only reliable cross-platform solution.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: Centering Text in ShowMessage
« Reply #4 on: November 07, 2016, 02:05:16 pm »
I am using that function for this issue:
Note: But I am not sure for cross compiling, maybe need a getwindowrect for linux if you need.
Code: Pascal  [Select][+][-]
  1. uses Windows,Dialogs;
  2.  
Use LCLintf instead of Windows?
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14393
  • Sensorship about opinions does not belong here.
Re: Centering Text in ShowMessage
« Reply #5 on: November 07, 2016, 02:13:39 pm »
I am using that function for this issue:

Now I am using it too ;)
I made it cross platform. Works on Raspberry Pi!
 Took just a little tinkering ! TNX!
Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils,LCLIntf,forms, Dialogs;
  9.  
  10. function MessageDlgCenter(const Msg: string; DlgType: TMsgDlgType;
  11.   Buttons: TMsgDlgButtons): Integer;
  12.  
  13. implementation
  14.  
  15.  
  16. function MessageDlgCenter(const Msg: string; DlgType: TMsgDlgType;
  17.   Buttons: TMsgDlgButtons): Integer;
  18. var R: TRect;
  19. begin
  20.   if not Assigned(Screen.ActiveForm) then
  21.   begin
  22.     Result := MessageDlg(Msg, DlgType, Buttons, 0);
  23.   end else
  24.   begin
  25.     with CreateMessageDialog(Msg, DlgType, Buttons) do
  26.     try
  27.       GetWindowRect(Screen.ActiveForm.Handle, R);
  28.       Left := R.Left + ((R.Right - R.Left) div 2) - (Width div 2);
  29.       Top := R.Top + ((R.Bottom - R.Top) div 2) - (Height div 2);
  30.       Result := ShowModal;
  31.     finally
  32.       Free;
  33.     end;
  34.   end;
  35. end;
  36.  
  37. end.
                     
« Last Edit: November 07, 2016, 02:16:33 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Centering Text in ShowMessage
« Reply #6 on: November 07, 2016, 08:21:04 pm »
Thanks, folks!

@Bart - I wrote my own before:
Code: Pascal  [Select][+][-]
  1. Procedure CenterPrompt(row,len_input : byte;
  2.                                                                                                                                  msg : ST80;
  3.                                 colr : char);
  4.  
  5. {  Goes to the row, determines the length of the msg and input string,
  6.    then centers the msg on the line
  7.  
  8.    Sample call:  CenterPrompt(10,0,'This message is centered.','0');
  9.  
  10.    variables set:
  11.      GLOBAL  bcol : starting Y coordinate of the msg
  12.              ccol : ending Y coordinate of the msg
  13.     use of bcol and ccol lets you do gotoXY calls to proper place in
  14.     string to highlight leading letters, etc.}
  15.  
  16. {**************     CENTER PROMPT     ********************}
  17.  
  18. Procedure CenterPrompt(row,len_input : byte;
  19.                                                                                                                                  msg : ST80;
  20.                                 colr : char);
  21. var oldcolr:word;
  22.  
  23. begin
  24.   Setcolor(colr,oldcolr);                            {  Center msg  }
  25.   gotoXY(trunc(lo(windmax)-lo(windmin) - length(msg)-len_input) div 2,row);
  26.   bcol := whereX;          {  Mark beginning cursor location  }
  27.                            {    for read by calling routine   }
  28.   write(msg);
  29.   RestoreColor(colr,oldcolr);
  30.   ccol := whereX;          {  Mark end cursor location for    }
  31.                            {    read by calling routine       }
  32. end;
  33.  
I was trying to do that again, but I do not know the size of the ShowMessage window, if it changes size with longer/larger messages, how many max characters it holds.  Do you know where I could find that information?  Since I couldn't find it, I posted.    :D

tr_escape - I have errors.  :(  I even added Windows though I am already 'using' LCLIntF. 
Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, Windows, SysUtils, FileUtil, LazFileUtils, Forms,
  3.   Controls, Graphics, SHFolder, ShlObj, Dialogs, StdCtrls,
  4.   ExtCtrls, Buttons, LCLIntf, LCLType, LazUtf8, BCButton,
  5.   BCButtonFocus;
  But I'm not giving up!

@Thaddy - I'll give your code a shot in a few.  Thank you very much for your tinkering!!!!  (I think I might give up!  ;)

Thank you all for your fantastic help and suggestions!!

Added later:
YEP!  Thaddy came to my rescue!  It works (as you know it did.)  Thank you.

« Last Edit: November 07, 2016, 09:02:42 pm by WickedDum »
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Centering Text in ShowMessage
« Reply #7 on: November 07, 2016, 09:23:45 pm »
OK.  Sorry y'all.  My bad.  My question is still not answered...   %)

The MessageDLGCenter centers the box on the screen, but not the text in the little information window itself.  That's what I am trying to accomplish....

Still lookin' ...    :)
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: Centering Text in ShowMessage
« Reply #8 on: November 08, 2016, 06:57:13 am »
If you change your codes to:


Code: Pascal  [Select][+][-]
  1. function MessageDlgCenter(const Msg: string; DlgType: TMsgDlgType;
  2.   Buttons: TMsgDlgButtons): Integer;
  3. var R: TRect;
  4. begin
  5.   if not Assigned(Screen.ActiveForm) then
  6.   begin
  7.     Result := MessageDlg(Msg, DlgType, Buttons, 0);
  8.   end else
  9.   begin
  10.     with CreateMessageDialog(Msg, DlgType, Buttons) do
  11.     try
  12.       GetWindowRect(Screen.ActiveForm.Handle, R);
  13.       //Left := R.Left + ((R.Right - R.Left) div 2) - (Width div 2);
  14.       //Top := R.Top + ((R.Bottom - R.Top) div 2) - (Height div 2);
  15.       Result := ShowModal;
  16.     finally
  17.       Free;
  18.     end;
  19.   end;
  20. end;
  21.  

Your window will show center of the desktop/screen.

But if you need fixed size window you should design as dialog window or create on runtime dynamically.

Showmessage and Createmessagedialog desinged for general using to notify user.

Maybe this simple codes will help you:

Code: Pascal  [Select][+][-]
  1. uses ...
  2.   , LCLIntf
  3.   , Buttons
  4.   ;
  5.  
  6.  
  7. procedure TForm1.Button2Click(Sender: TObject);
  8. var
  9.   frmDialog : TForm;
  10.   lblMsg : TLabel;
  11.   btnClose: TBitBtn;
  12. begin
  13.   frmDialog:=TForm.Create(Self);
  14.   lblMsg := TLabel.Create(Self);
  15.   btnClose := TBitBtn.Create(Self);
  16.  
  17.  
  18.   with frmDialog do
  19.   begin
  20.  
  21.     lblMsg.AutoSize:=False;
  22.     lblMsg.Align:=alTop;
  23.     lblMsg.Alignment:=taCenter;
  24.     lblMsg.Caption:='That''s the mesage!';
  25.     lblMsg.Parent:=frmDialog;
  26.  
  27.     btnClose.Parent:=frmDialog;
  28.     btnClose.Kind:=bkOK;
  29.     btnClose.Align:=alBottom;
  30.     btnClose.Caption:='OK';
  31.  
  32.     Width:=300;
  33.     Height:=100;
  34.     BorderStyle:=bsDialog;
  35.     Position:=poOwnerFormCenter;
  36.     ShowModal;
  37.   end;
  38. end;
  39.  



WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Centering Text in ShowMessage
« Reply #9 on: November 08, 2016, 07:49:53 pm »
Thanks, tr_escape!  That's pretty close - if I do a wee bit of tweaking...  I think I like the use of ShowModal.

This is where I'm at right now:
Code: Pascal  [Select][+][-]
  1. {****************  Center Message  ****************}
  2. Procedure CenterMsg(OrigMsg:string);
  3. var
  4.   padding,
  5.   LenOrigMsg : double;
  6.  
  7. begin
  8.   padding := (80-length(OrigMsg));
  9.   LenOrigMsg := round(padding/2);
  10.   While LenOrigMsg > 0 do
  11.     begin
  12.       OrigMsg := ' ' + OrigMsg;
  13.       LenOrigMsg := LenOrigMsg - 1;
  14.     end;
  15.   ShowMessage(OrigMsg);              //  Centered message
  16. end;

Just a few things to make it perfect:

1- I just need to create an acceptable length of window algorithm based on the (users') font size...  80 isn't ideal.
2- I think I would prefer a wider window other than what ShowMessage provides.  Maybe make it dynamic according to the size of the message string...
3- A touch of color.  :)

But it works for now!!  :D

Thanks, again, guys!!  GREAT help!!
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

 

TinyPortal © 2005-2018