How to show a message box for a specified time?

柔情痞子 提交于 2019-12-18 13:33:15

问题


Is there a way to show a message box for a specified time (that means, the message box will close itself when the specified time elapses) ?


回答1:


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)



回答2:


If you want a more customized implementation than the MessageBoxTimeout from @TLama's answer allows (like countdown display or custom button captions):

  • create a custom form using CreateCustomForm;
  • use SetTimer to implement the timeout/count down.

For a complete code, see MsgBox - Make unclickable OK Button and change to countdown - Inno Setup.



来源:https://stackoverflow.com/questions/20816881/how-to-show-a-message-box-for-a-specified-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!