[x] 秒后关闭 Delphi 对话框
是否可以让 Delphi 在一定时间后关闭 ShowMessage 或 MessageDlg 对话框?
我想在应用程序关闭时向用户显示一条消息,但不想让应用程序关闭超过 10 秒左右。
我可以让默认对话框在定义的时间后关闭,还是需要编写自己的表单?
Is it possible to get Delphi to close a ShowMessage or MessageDlg Dialog after a certain length of time?
I want to show a message to the user when the application is shut down, but do not want to stop the application from shutting down for more than 10 seconds or so.
Can I get the default dialog to close after a defined time, or will I need to write my own form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您想连接计时器以关闭它,您可以连接 Screen.OnActiveFormChange 事件并使用
Screen.ActiveCustomForm
You can hook up the Screen.OnActiveFormChange event and use Screen.ActiveCustomForm if it is a interested form that you want to hook up the timer to close it
enjoy
最好的方法是使用stayontop表单并使用表单的alpha混合属性管理计数器消失,在计数结束时只需关闭表单,但是
该控件将在显示表单之前传递给所需的活动控件,这样,用户将收到一条自动消失的消息,并且不会阻止使用下一个功能,这对我来说非常酷的技巧。
Best way is to use a stayontop form and manage a counter to disappear using the alfpha blend property of the form, at the end of the count just close the form, but
the control will be passed to the active control needed before showing the form, this way, user will have a message which disappears automatically and wont prevent the usage of the next feature, very cool trick for me.
您可以使用 WTSSendMessage 来执行此操作。
您可以在 中找到此内容JWA 库,或者您自己调用它。
You can do this with WTSSendMessage.
You can find this in the JWA libraries, or call it yourself.
当模式对话框或系统消息框或类似活动(或打开菜单)时,您的应用程序实际上仍在工作,只是正在运行一个辅助消息循环来处理所有消息 - 发送或发布到它的所有消息,以及必要时它也会合成(并处理)
WM_TIMER
和WM_PAINT
消息。因此,无需创建线程或跳过任何其他环节,您只需安排在 10 秒过去后运行关闭消息框的代码即可。一种简单的方法是调用
SetTimer()
没有目标HWND
,但有一个回调函数:忽略了错误处理,但这应该可以帮助您开始。
Your application is actually still working while a modal dialog or system message box or similar is active (or while a menu is open), it's just that a secondary message loop is running which processes all messages - all messages sent or posted to it, and it will synthesize (and process)
WM_TIMER
andWM_PAINT
messages when necessary as well.So there's no need to create a thread or jump through any other hoops, you simply need to schedule the code that closes the message box to be run after those 10 seconds have elapsed. A simple way to do that is to call
SetTimer()
without a targetHWND
, but a callback function:Error handling ommitted, but this should get you started.
您可以尝试使用标准消息对话框来完成此操作。使用对话框中的 CreateMessageDialog 过程创建对话框,然后添加所需的控件。
在带有 TButton 的表单中,定义 onClick 如下:
OnTimer 属性如下:
定义变量和过程:
并测试它。
当计时器倒计时结束时,窗体自动关闭。与此类似,您可以添加其他类型的组件。
问候。
You can try to do it with a standard Message dialog. Create the dialog with CreateMessageDialog procedure from Dialogs and after add the controls that you need.
In a form with a TButton define onClick with this:
An the OnTimer property like this:
Define the variables and procedure:
And test it.
The form close automatically when the timer final the CountDown. Similar this you can add other type of components.
Regards.
试试这个:
我已经使用这个有一段时间了;它是一种享受。
Try this:
I've been using this for quite some time; it works a treat.
好的。您有 2 个选择:
1 - 您可以创建自己的 MessageDialog 表单。然后,您可以使用它并添加一个 TTimer,它将在您需要时关闭表单。
2 - 您可以继续使用 showmessage 并创建一个将使用 FindWindow (查找消息对话框窗口)的线程,然后将其关闭。
我建议您使用自己的带有计时器的表格。它更干净、更容易。
OK. You have 2 choices:
1 - You can create your own MessageDialog form. Then, you can use it and add a TTimer that will close the form when you want.
2 - You can keep using showmessage and create a thread that will use FindWindow (to find the messadialog window) and then close it.
I recommend you to use you own Form with a timer on it. Its cleaner and easier.
这在 Windows 98 和更新版本中工作得很好...
我不使用“MessageBoxTimeOut”,因为旧的 Windows 98 ME 没有它...
这个新函数的工作方式就像“CHARM”..
//add this过程
/////////////////////////////如何调用///////////////////
DialogBoxAutoClose('Alert'', "此消息将在 10 秒后关闭',10);
/////////////////////////////// ///////////////////////////////////////////////////////
This works fine with windows 98 and newers...
I don't use the " MessageBoxTimeOut" because old windows 98, ME, doesn't have it...
this new function works like a "CHARM"..
//add this procedure
////////////////////////////How Call It//////////////////
DialogBoxAutoClose('Alert'', "This message will be closed in 10 seconds',10);
/////////////////////////////////////////////////////////
MessageBox 在内部调用此函数并传递 0xFFFFFFFF 作为超时参数,因此它被删除的可能性很小(感谢 Maurizio)
MessageBox calls this function internally and pass 0xFFFFFFFF as timeout parameter, so the probability of it being removed is minimal (thanks to Maurizio for that)
我考虑过使用单独的线程,但它可能会让您陷入许多不必要的代码等。Windows 对话框根本不是为此而设计的。
你应该做你自己的表格。从好的方面来说,您可以使用带有倒计时功能的自定义代码/UI,就像定时对话框一样。
I thought about using a separate thread, but it's probably going to get you into a lot of unnecessary code etc. Windows dialogs were simply not made for this thing.
You should do your own form. On the good side, you can have custom code/UI with a countdown like timed dialog boxes do.
不会。ShowMessage 和MessageDlg 都是模态窗口,这意味着您的应用程序在显示它们时基本上处于暂停状态。
您可以设计自己的带有计时器的替换对话框。在 FormShow 事件中启用计时器,并在 FormClose 事件中禁用计时器。在 OnTimer 事件中,禁用计时器,然后关闭窗体本身。
No. ShowMessage and MessageDlg are both modal windows, which means that your application is basically suspended while they're displayed.
You can design your own replacement dialog that has a timer on it. In the FormShow event, enable the timer, and in the FormClose event disable it. In the OnTimer event, disable the timer and then close the form itself.