Recent

Author Topic: Showmessage MessageBox MessageDlg long string  (Read 14273 times)

k1attila1

  • Full Member
  • ***
  • Posts: 105
Showmessage MessageBox MessageDlg long string
« on: April 20, 2017, 07:08:45 pm »
Hello

Showmessage, MessageBox, MessageDlg wrap/break the line if string is long (eg 100 char)

Does anybody know any solution or it is OS problem (Win10) ?

thank you Attila

« Last Edit: April 21, 2017, 04:36:42 pm by k1attila1 »

Handoko

  • Hero Member
  • *****
  • Posts: 5152
  • My goal: build my own game engine using Lazarus
Re: Showmessage MessageBox MessageDlg long string
« Reply #1 on: April 20, 2017, 08:04:48 pm »
Tested ShowMessage's wrapping behavior on Lazarus 1.6.4 FPC 3.0.2 on my Ubuntu Mate 16.10 computer. And this is the test result:

- The message will be wrapped on char 32 if the line is too long.

- The message won't be wrapped nor truncated even it exceeds my screen width, if the string doesn't contain space (chr 32).

This is the code I used:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.     { private declarations }
  19.   public
  20.     { public declarations }
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. var
  34.   i: Integer;
  35.   S: string;
  36. begin
  37.  
  38.   S := '';
  39.   for i := 1 to 20 do
  40.     S := S + IntToStr(i)+'-123456 '; // has chr 32
  41.   ShowMessage(S);
  42.  
  43.   S := '';
  44.   for i := 1 to 20 do
  45.     S := S + IntToStr(i)+'-123456x'; // no chr 32
  46.   ShowMessage(S);
  47.  
  48. end;
  49.  
  50. end.
« Last Edit: April 20, 2017, 08:57:21 pm by Handoko »

ASerge

  • Hero Member
  • *****
  • Posts: 2241
Re: Showmessage MessageBox MessageDlg long string
« Reply #2 on: April 20, 2017, 08:36:52 pm »
Does anybody know any solution or it is OS problem (Win10) ?
What is solution?

My test. Windows 7 x64.
Dialogs.ShowMessage, Application.MessageBox, Dialogs.MessageDlg wrap after 59 chars if text contain space, or show 56 chars with '...' on single line without wrap.
Windows.MessageBox always wrap text. After 73 chars or early, if text contain space.

Handoko

  • Hero Member
  • *****
  • Posts: 5152
  • My goal: build my own game engine using Lazarus
Re: Showmessage MessageBox MessageDlg long string
« Reply #3 on: April 20, 2017, 08:53:12 pm »
So the ShowMessage's behavior is OS depended. My test never got a '...' even the string's length is 491 (for i := 1 to 50).
« Last Edit: April 20, 2017, 08:58:42 pm by Handoko »

k1attila1

  • Full Member
  • ***
  • Posts: 105
Re: Showmessage MessageBox MessageDlg long string
« Reply #4 on: April 21, 2017, 05:34:16 am »
Thank You

So it is OS problem and not Lazarus.

Thank You again Attila

balazsszekely

  • Guest
Re: Showmessage MessageBox MessageDlg long string
« Reply #5 on: April 21, 2017, 06:34:52 am »
Hi Attila,

Try the following code, it works on my win10:
Code: Pascal  [Select][+][-]
  1. function MessageDlgEx(const AMsg: string; ADlgType: TMsgDlgType;
  2.   AButtons: TMsgDlgButtons; AParent: TForm): TModalResult;
  3. var
  4.   MsgFrm: TForm;
  5. begin
  6.   MsgFrm := CreateMessageDialog(AMsg, ADlgType, AButtons);
  7.   try
  8.     MsgFrm.Position := poDefaultSizeOnly;
  9.     MsgFrm.FormStyle := fsSystemStayOnTop;
  10.     MsgFrm.Left := AParent.Left + (AParent.Width - MsgFrm.Width) div 2;
  11.     MsgFrm.Top := AParent.Top + (AParent.Height - MsgFrm.Height) div 2;
  12.     Result := MsgFrm.ShowModal;
  13.   finally
  14.     MsgFrm.Free
  15.   end;
  16. end;
  17.  
  18. procedure TForm1.Button1Click(Sender: TObject);
  19. begin
  20.   MessageDlgEx('fadfa afdfad afdad fafsd faf af af ads dddddddddddddddddddddddd dddddddddddddddddddddddd dddddddddddddddddddddddd dafdsad fasdfa daf',
  21.                 mtInformation, [mbOk], Form1);
  22. end;        
  23.  

k1attila1

  • Full Member
  • ***
  • Posts: 105
Re: Showmessage MessageBox MessageDlg long string
« Reply #6 on: April 21, 2017, 07:16:21 am »
Thank you , Sir !

It is a good solution.

Attila

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Showmessage MessageBox MessageDlg long string
« Reply #7 on: April 21, 2017, 08:47:55 am »
Another solution is to replace spaces with non-breaking spaces:
Code: Pascal  [Select][+][-]
  1. const
  2.   NonBreakingSpace: String = Char($C2) + Char($A0); // non-breaking space (utf8-encoded)
  3.  
  4. procedure MyShowMessage(const aMsg: String);
  5. begin
  6.   ShowMessage(StringReplace(aMsg, Char(32), NonBreakingSpace, [rfReplaceAll]));
  7. end;
  8.  
  9. function MyMessageDlg(const aMsg: string; DlgType: TMsgDlgType;
  10.             Buttons: TMsgDlgButtons; HelpCtx: Longint): TModalResult;
  11. begin
  12.   Result := MessageDlg(StringReplace(aMsg, Char(32), NonBreakingSpace, [rfReplaceAll]),
  13.     DlgType, Buttons, HelpCtx);
  14. end;
  15.  
  16.  

EDIT: No, GetMem's solution is better, as line breaking occurs not only on spaces, but also on other charactes which can appear in your message (dashes, tabs, ...), and these aren't handled by my code.
« Last Edit: April 21, 2017, 09:10:38 am by Zoran »

k1attila1

  • Full Member
  • ***
  • Posts: 105
Re: Showmessage MessageBox MessageDlg long string
« Reply #8 on: April 21, 2017, 11:07:14 am »
Thank you Zoran !

I study your solution too ,maybe useful for another problem.

Attila

 

TinyPortal © 2005-2018