如何处理弹出、模态、可调整大小的表单 [英] How to Handle a Popup, Modal, Resizable form
问题描述
我需要为屏幕不像其他人那么大的用户设置一个可调整大小的弹出窗体 - 将窗体设置为 Popup 和 Modal 和 BorderStyle Resizable 有一个主要限制 - 启动弹出窗口的窗体中的代码现在不等待用于返回表单.
I need to have a popup form resizable for users whose screen is not as large as others - setting the form to Popup and Modal and BorderStyle Resizable has one major limitation - the code in the form that launches the popup now does not wait for the form to return.
那么如何等待一个表单不可见呢?我尝试使用 sleep 和 doevents 进行循环,但这会使弹出窗体的响应速度不快,并且会占用 CPU 周期.我已经尝试设置启动表单的 form.gotfocus 事件,但这不会触发,这意味着我必须将打开弹出表单的代码与关闭弹出表单后执行的代码分开.
So how does one wait for a form to be made invisible? I tried looping with sleep and doevents, but that makes the popup form not very responsive and chews up cpu cycles. I have tried setting the form.gotfocus event of the launching form but that does not trigger and which means I have to split the code that opened the popup form from the code that executes after the popup form is closed.
什么是最好的解决方案?
What is the best solution?
谢谢
推荐答案
我从来没有遇到过 DoEvents/Sleep 50
循环问题.CPU 负载保持最小,表单响应迅速.
I have never had any problems with DoEvents / Sleep 50
loops. CPU load stays minimal and the form responsive.
对于一台很旧的电脑,也许可以使用 Sleep 100
.
With a very old computer, perhaps use Sleep 100
.
示例代码:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub TestOpenForm()
If FormOpenWait("frmPopup") Then
MsgBox "Form was hidden.", vbInformation
Else
MsgBox "Form was closed.", vbInformation
End If
End Sub
' Open fName, wait until it is
' - closed : return False
' - hidden : return True
Public Function FormOpenWait(fName As String, Optional sOpenArgs As String = "") As Boolean
If IsFormLoaded(fName) Then DoCmd.Close acForm, fName, acSaveNo
DoCmd.OpenForm FormName:=fName, OpenArgs:=sOpenArgs
' default: signal "closed"
FormOpenWait = False
' Wait until form is closed or made invisible
Do While IsFormLoaded(fName)
If Not Forms(fName).Visible Then
' Signal "hidden"
FormOpenWait = True
Exit Do
End If
' Wait 50ms without hogging the CPU
DoEvents
Sleep 50
Loop
End Function
Public Function IsFormLoaded(fName As String) As Boolean
IsFormLoaded = (SysCmd(acSysCmdGetObjectState, acForm, fName) And acObjStateOpen) > 0
End Function
这篇关于如何处理弹出、模态、可调整大小的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!