* * *

Author Topic: How to close MessageDialog  (Read 491 times)

Kevinn33

  • New member
  • *
  • Posts: 26
How to close MessageDialog
« on: May 09, 2018, 08:41:02 am »
Hello, i want to make an app that can show messagedialog and close it automatically in linux. can anyone tell me how to do it?
thanks before

GetMem

  • Hero Member
  • *****
  • Posts: 3018
Re: How to close MessageDialog
« Reply #1 on: May 09, 2018, 08:53:53 am »
1. Create a custom form
2. On FormShow start a timer, when ticks close the form.

Kevinn33

  • New member
  • *
  • Posts: 26
Re: How to close MessageDialog
« Reply #2 on: May 09, 2018, 01:28:38 pm »
okay thanks for the advice

i was wondering to use that for the last option
seems like its better to use that way rather than searching for no source

wp

  • Hero Member
  • *****
  • Posts: 4483
Re: How to close MessageDialog
« Reply #3 on: May 10, 2018, 11:47:44 am »
If you are on Windows and don't care about the return value of the MessageDlg then you can call this procedure - specify the caption of the MessageDlg in the parameter:

Code: Pascal  [Select]
  1. uses
  2.   Windows;
  3.  
  4. // Finds the window having the specified caption and destroys it.
  5. procedure KillWindow(ATitle: String);
  6. var
  7.   H: HWND;
  8. begin
  9.   H := FindWindow(nil, PChar(ATitle));
  10.   if H <> 0 then
  11.     DestroyWindow(H);
  12. end;
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

GetMem

  • Hero Member
  • *****
  • Posts: 3018
Re: How to close MessageDialog
« Reply #4 on: May 10, 2018, 02:12:55 pm »
Auto-close dialogs might be useful for some user, this is one way to implement it(see attachment):
Code: Pascal  [Select]
  1. unit uMsgDlg;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Dialogs, Controls, ExtCtrls;
  9.  
  10. type
  11.   TMsgDlgEx = class
  12.   private
  13.     FMsgFrm: TForm;
  14.     FParent: TForm;
  15.     FTimer: TTimer;
  16.     FDefModRes: TModalResult;
  17.     procedure DoOnTimer(Sender: TObject);
  18.   public
  19.     constructor Create(const AMsg: string; ADlgType: TMsgDlgType; AButtons:
  20.       TMsgDlgButtons; AParent: TForm; ADefModRes: TModalResult;
  21.       AInterval: Cardinal);
  22.     destructor Destroy; override;
  23.     function ShowDialog: TModalResult;
  24.   end;
  25.  
  26. implementation
  27.  
  28. constructor TMsgDlgEx.Create(const AMsg: string; ADlgType: TMsgDlgType;
  29.   AButtons: TMsgDlgButtons; AParent: TForm; ADefModRes: TModalResult;
  30.   AInterval: Cardinal);
  31. begin
  32.   FMsgFrm := CreateMessageDialog(AMsg, ADlgType, AButtons);
  33.   FDefModRes := ADefModRes;
  34.   FParent := AParent;
  35.   FTimer := TTimer.Create(nil);
  36.   FTimer.Enabled := False;
  37.   FTimer.Interval := AInterval;
  38.   FTimer.OnTimer := @DoOnTimer;
  39. end;
  40.  
  41. destructor TMsgDlgEx.Destroy;
  42. begin
  43.   FTimer.Enabled := False;
  44.   FTimer.Free;
  45.   FMsgFrm.Free;
  46.   inherited Destroy;
  47. end;
  48.  
  49. function TMsgDlgEx.ShowDialog: TModalResult;
  50. begin
  51.   FMsgFrm.FormStyle := fsSystemStayOnTop;
  52.   if FParent <> nil then
  53.   begin
  54.      FMsgFrm.Position := poDefaultSizeOnly;
  55.      FMsgFrm.Left := FParent.Left + (FParent.Width - FMsgFrm.Width) div 2;
  56.      FMsgFrm.Top := FParent.Top + (FParent.Height - FMsgFrm.Height) div 2;
  57.    end
  58.    else
  59.      FMsgFrm.Position := poWorkAreaCenter;
  60.    FTimer.Enabled := True;
  61.    Result := FMsgFrm.ShowModal;
  62. end;
  63.  
  64. procedure TMsgDlgEx.DoOnTimer(Sender: TObject);
  65. begin
  66.   FTimer.Enabled := False;
  67.   FMsgFrm.ModalResult := FDefModRes;
  68. end;

Usage:
Code: Pascal  [Select]
  1. uses uMsgDlg;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   MsgDlgEx: TMsgDlgEx;
  6.   Str: String;
  7. begin
  8.   Str := 'Do you wish to save the changes(Dialog will autoclose in 5 seconds, default result is mrYes)?';
  9.   MsgDlgEx := TMsgDlgEx.Create(Str, mtInformation, [mbYes, mbNo], Self, mrYes, 5000);
  10.   try
  11.     if MsgDlgEx.ShowDialog = mrYes then
  12.       ShowMessage('Saving changes')
  13.   finally
  14.     MsgDlgEx.Free;
  15.   end;
  16. end;
  17.  
« Last Edit: May 10, 2018, 02:14:26 pm by GetMem »

Kevinn33

  • New member
  • *
  • Posts: 26
Re: How to close MessageDialog
« Reply #5 on: May 12, 2018, 07:14:10 pm »
If you are on Windows and don't care about the return value of the MessageDlg then you can call this procedure - specify the caption of the MessageDlg in the parameter:

Code: Pascal  [Select]
  1. uses
  2.   Windows;
  3.  
  4. // Finds the window having the specified caption and destroys it.
  5. procedure KillWindow(ATitle: String);
  6. var
  7.   H: HWND;
  8. begin
  9.   H := FindWindow(nil, PChar(ATitle));
  10.   if H <> 0 then
  11.     DestroyWindow(H);
  12. end;

hahaha but i want it to work on Linux :)

Auto-close dialogs might be useful for some user, this is one way to implement it(see attachment):
Code: Pascal  [Select]
  1. unit uMsgDlg;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Dialogs, Controls, ExtCtrls;
  9.  
  10. type
  11.   TMsgDlgEx = class
  12.   private
  13.     FMsgFrm: TForm;
  14.     FParent: TForm;
  15.     FTimer: TTimer;
  16.     FDefModRes: TModalResult;
  17.     procedure DoOnTimer(Sender: TObject);
  18.   public
  19.     constructor Create(const AMsg: string; ADlgType: TMsgDlgType; AButtons:
  20.       TMsgDlgButtons; AParent: TForm; ADefModRes: TModalResult;
  21.       AInterval: Cardinal);
  22.     destructor Destroy; override;
  23.     function ShowDialog: TModalResult;
  24.   end;
  25.  
  26. implementation
  27.  
  28. constructor TMsgDlgEx.Create(const AMsg: string; ADlgType: TMsgDlgType;
  29.   AButtons: TMsgDlgButtons; AParent: TForm; ADefModRes: TModalResult;
  30.   AInterval: Cardinal);
  31. begin
  32.   FMsgFrm := CreateMessageDialog(AMsg, ADlgType, AButtons);
  33.   FDefModRes := ADefModRes;
  34.   FParent := AParent;
  35.   FTimer := TTimer.Create(nil);
  36.   FTimer.Enabled := False;
  37.   FTimer.Interval := AInterval;
  38.   FTimer.OnTimer := @DoOnTimer;
  39. end;
  40.  
  41. destructor TMsgDlgEx.Destroy;
  42. begin
  43.   FTimer.Enabled := False;
  44.   FTimer.Free;
  45.   FMsgFrm.Free;
  46.   inherited Destroy;
  47. end;
  48.  
  49. function TMsgDlgEx.ShowDialog: TModalResult;
  50. begin
  51.   FMsgFrm.FormStyle := fsSystemStayOnTop;
  52.   if FParent <> nil then
  53.   begin
  54.      FMsgFrm.Position := poDefaultSizeOnly;
  55.      FMsgFrm.Left := FParent.Left + (FParent.Width - FMsgFrm.Width) div 2;
  56.      FMsgFrm.Top := FParent.Top + (FParent.Height - FMsgFrm.Height) div 2;
  57.    end
  58.    else
  59.      FMsgFrm.Position := poWorkAreaCenter;
  60.    FTimer.Enabled := True;
  61.    Result := FMsgFrm.ShowModal;
  62. end;
  63.  
  64. procedure TMsgDlgEx.DoOnTimer(Sender: TObject);
  65. begin
  66.   FTimer.Enabled := False;
  67.   FMsgFrm.ModalResult := FDefModRes;
  68. end;

Usage:
Code: Pascal  [Select]
  1. uses uMsgDlg;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   MsgDlgEx: TMsgDlgEx;
  6.   Str: String;
  7. begin
  8.   Str := 'Do you wish to save the changes(Dialog will autoclose in 5 seconds, default result is mrYes)?';
  9.   MsgDlgEx := TMsgDlgEx.Create(Str, mtInformation, [mbYes, mbNo], Self, mrYes, 5000);
  10.   try
  11.     if MsgDlgEx.ShowDialog = mrYes then
  12.       ShowMessage('Saving changes')
  13.   finally
  14.     MsgDlgEx.Free;
  15.   end;
  16. end;
  17.  

thanks for the advice!, but at the end i use the first method you give :)

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus