Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

Timeout MessageBox

Timeout MessageBox

By Zarko Gajic, About.com

Here's how to call a Message Box with a timeout, the message box closes itself after the timeout period was reached.

The trick is to call an undocumented MessageBoxTimeout API located in user32.dll.

The function returns an integer value of either a MB_TIMEDOUT value (indicating the timeout period was reached and the Message Box auto closed), or a value representing the button the user clicked.
Notice that the return value is always 1, when a message box with only an OK button (MB_OK Flag) is used.

~~~~~~~~~~~~~~~~~~~~~~~~~
//interface declaration
const
  MB_TIMEDOUT = 32000;

function MessageBoxTimeOut(hWnd: HWND; lpText: PChar; lpCaption: PChar; uType: UINT; wLanguageId: WORD; dwMilliseconds: DWORD): Integer; stdcall; external user32 name 'MessageBoxTimeoutA';

//implementation (Button1's OnClick event handler on Form1)
procedure TForm1.Button1Click(Sender: TObject) ;
var
  iRet: Integer;
  iFlags: Integer;
begin
   iFlags := MB_OK or MB_SETFOREGROUND or MB_SYSTEMMODAL or MB_ICONINFORMATION;
   MessageBoxTimeout(Application.Handle, 'Test a timeout of 2 seconds.', 'MessageBoxTimeout Test', iFlags, 0, 2000) ;

   iFlags := MB_YESNO or MB_SETFOREGROUND or MB_SYSTEMMODAL or MB_ICONINFORMATION;
   iRet := MessageBoxTimeout(Application.Handle, 'Test a timeout of 5 seconds.', 'MessageBoxTimeout Test', iFlags, 0, 5000) ;
   case iRet of
     IDYES:
       ShowMessage('Yes') ;
     IDNO:
       ShowMessage('No') ;
     MB_TIMEDOUT:
       ShowMessage('TimedOut') ;
   end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
Tip author: "MrBaseball34"

Note: to add a progress bar to a dialog window, go see: "How to place a progress bar inside a standard dialog box"

Delphi tips navigator:
» How to call the View Source dialog in TWebBrowser
« How to create, use and free TXMLDocument dynamically (without an AV)

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming

About.com Special Features

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2004 Delphi Tips
  7. Timeout MessageBox

©2009 About.com, a part of The New York Times Company.

All rights reserved.