How to show a message box for a specified time?

Windows API has a function for showing a message box for a specified time, but for some reason is that function undocumented, which means it is not officially supported and may well be subject to change.

That function is called MessageBoxTimeout, and it has even export in the user32.dll library, what makes me feel that the only thing this function lacks is the official documentation. But who knows...

The following script shows how to display a message box for 5 seconds before the wizard form is shown. If the user doesn't click the OK button, nor manually close the window, the message box is automatically closed when that 5 seconds period elapses:

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif
const
  MB_TIMEDOUT = 32000;
  MB_ICONERROR = $10;
  MB_ICONQUESTION = $20;
  MB_ICONWARNING = $30;
  MB_ICONINFORMATION = $40;

function MessageBoxTimeout(hWnd: HWND; lpText: string; lpCaption: string;
  uType: UINT; wLanguageId: Word; dwMilliseconds: DWORD): Integer;
  external 'MessageBoxTimeout{#AW}@user32.dll stdcall';

procedure InitializeWizard;
begin
  MessageBoxTimeout(WizardForm.Handle, 'This message will be automatically ' +
    'closed in 5 seconds!', 'Caption...', MB_OK or MB_ICONINFORMATION, 0, 5000);
end;

For more information about parameters and result values refer to the MessageBox function help site and some of the unofficial articles describing the MessageBoxTimeout function itself, like e.g.:

  • Maurizio Pisano: MessageBoxTimeout API (CodeProject)
  • Eddie Shipman: Undocumented MessageBoxTimeOut function (Embarcadero)