This site is temporarily down. Please come back. How can I close a MessageBox()
DelphiFAQ Home Search:

How can I close a MessageBox()

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

(by Ralph Friedman)
You can use a thread to achieve that:

unit MsgThread;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, Forms, StdCtrls, ExtCtrls;

type
  TMboxThread = class(TThread)
  private
    { private declarations }
  protected
    procedure Execute; override;
  public
    constructor Create;
  end;

type
  TFrmMsgThread = class(TForm)
    BtnClose: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Timer1: TTimer;
    procedure BtnCloseClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    FFirst:      boolean;
    FMboxThread: TMBoxThread;
    FWinHandle:  HWnd;
  public
    { public declarations }
  end;

var
  FrmMsgThread: TFrmMsgThread;

implementation

{$R *.DFM}

{ TMboxThread }

constructor TMboxThread.Create;
begin
  FreeOnTerminate := True;
  inherited Create(False);
end;

procedure TMboxThread.Execute;
begin
  { Place thread code here }
  MessageBox(Application.Handle, 'Text', 'Caption',
             MB_APPLMODAL + MB_SETFOREGROUND);
end;

{ TForm1 }

procedure TFrmMsgThread.BtnCloseClick(Sender: TObject);
begin
  FMBoxThread    := TMBoxThread.Create;
  FFirst         := true;
  Timer1.Enabled := true;
end;

procedure TFrmMsgThread.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := false;
  if FFirst then
  begin
    FWinHandle     := GetForegroundWindow;
    FFirst          := false;
    Timer1.Enabled := true;
  end
  else
    SendMessage(FWinHandle, WM_CLOSE, 0, 0);
end;

end.

Content-type: text/html

Comments:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: