简体   繁体   English

如何自动关闭模式窗体

[英]How to automatically close a modal form

I have a VB6 project and I implement a session timer with an automatic logout. 我有一个VB6项目,并且实现了带有自动注销功能的会话计时器。

My problem is if a modal form is opened, this form is not in Forms Collection so I can't know if this form is opened in order to unload it. 我的问题是是否打开了模式窗体,但此窗体不在Forms Collection中,所以我不知道是否为打开窗体而将其卸载。 With non modal forms, no problems. 使用非模式形式,没有问题。

If anyone have an idea ? 如果有人有主意? Is it possible to show all opened forms (non modal AND modal) ? 是否可以显示所有打开的表单(非模式和模式)?

Thanks for your responses 感谢您的回复

Edit : code snippet : 编辑:代码段:

form creation : 表格创建:

Dim FormLoc As New F_Options
FormLoc.Show vbModal

form destruction : 破坏形式:

For Each mFrm In Forms
    unload mFrm
next    

For Each mFrm In F_MDIParent.MDIActiveX1.Forms
    unload mFrm
next 

Main form is a MDI Form. 主要形式是MDI形式。 Other forms can be open and dock in MDI Mainform but some forms are "normal". 其他表格可以在MDI Mainform中打开和停靠,但有些表格是“正常”的。

Edit : 编辑:

I made some tests and Forms collection depend on a project, I thought that this collection was global for the application but apparently not. 我进行了一些测试,并且Forms集合依赖于一个项目,我认为该集合对于应用程序是全局的,但显然不是。

I want to close all forms from main project. 我想关闭主项目中的所有表格。 I can have a modal form opened by another project (90 projects for all the application) 我可以通过另一个项目打开一个模式表单(所有应用程序都包含90个项目)

you can also use Windows API functions if you just want to make sure it is closed: 如果您只想确保已关闭,也可以使用Windows API函数:

Private Declare Function CloseWindow Lib "user32" Alias "CloseWindow" (ByVal hwnd As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 专用声明函数CloseWindow Lib“ user32”别名“ CloseWindow”(ByVal hwnd as Long)长

Dim wid as long wid=FindWindow(vbnullstring,"") if wid <> 0 then CloseWindow wid 如果wid <> 0,则将wid设为长wid = FindWindow(vbnullstring,“”),然后关闭Window wid

One alternative that may work; 一种可行的替代方法;

do until Screen.ActiveForm is Nothing
    unload Screen.ActiveForm
loop

modal, modeless, and default forms do show up in the forms collection 模态,无模态和默认表单的确显示在表单集合中

run the following test project to see it happening : 运行以下测试项目以查看其发生情况:

'project with 2 forms
'  form1 with :
'    1 label: name=Label1
'    1 timer: name=Timer1
'    4 command buttons: names are Command1, Command2, Command3, Command4
'  form2 with nothing on it
Option Explicit

Private Sub Command1_Click()
  Dim frm As New Form2
  frm.BackColor = vbRed
  frm.Show vbModal
End Sub

Private Sub Command2_Click()
  Dim frm As New Form2
  frm.BackColor = vbGreen
  frm.Show vbModeless, Me
End Sub

Private Sub Command3_Click()
  Dim frm As New Form2
  frm.Show
End Sub

Private Sub Form_Load()
  With Screen
    Move .Width / 2, 0, .Width / 2, .Height / 2
  End With 'Screen
  Command1.Caption = "modal"
  Command2.Caption = "modeless"
  Command3.Caption = "default"
  Command4.Caption = "unload"
  With Timer1
    .Interval = 1000
    .Enabled = True
  End With 'Timer1
End Sub

Private Sub Form_Resize()
  Dim sngLblWidth As Single, sngLblHeight As Single
  Dim sngCmdWidth As Single, sngCmdHeight As Single
  sngCmdHeight = 495
  sngLblHeight = ScaleHeight - sngCmdHeight
  sngLblWidth = ScaleWidth
  sngCmdWidth = sngLblWidth / 4
  Label1.Move 0, 0, sngLblWidth, sngLblHeight
  Command1.Move 0, sngLblHeight, sngCmdWidth, sngCmdHeight
  Command2.Move sngCmdWidth, sngLblHeight, sngCmdWidth, sngCmdHeight
  Command3.Move 2 * sngCmdWidth, sngLblHeight, sngCmdWidth, sngCmdHeight
  Command4.Move 3 * sngCmdWidth, sngLblHeight, sngCmdWidth, sngCmdHeight
End Sub

Private Sub Timer1_Timer()
  Dim frm As Form
  Dim strShow As String
  strShow = CStr(Now)
  For Each frm In Forms
    strShow = strShow & vbCrLf & frm.Name
  Next frm
  Label1.Caption = strShow
End Sub

[EDIT] [编辑]

add 3 command buttons to form2 and click them to test what happens upon unloading 向form2添加3个命令按钮,然后单击它们以测试卸载时发生的情况

test it while running from the ide and notice the difference when a modal form is shown, when the modeless form is shown (with owner), and when a default form is shown (without owner) .. pay attention to the 'run' button i the ide .. when unloading form1 with a model form shown, the application remains running 从ide上运行时进行测试,并注意显示模态表单时,显示无模式表单时(带有所有者)和显示默认表单时(不带所有者)的区别..请注意“运行”按钮我的想法..当卸载带有模型表格的form1时,应用程序仍在运行

here is the code for Form2 这是Form2的代码

Option Explicit

Private Sub Command1_Click()
  UnloadForms "Form1"
End Sub

Private Sub Command2_Click()
  UnloadForms "Form2"
End Sub

Private Sub Command3_Click()
  UnloadForms ""
End Sub

Private Sub Form_Load()
  Command1.Caption = "Unload Form1"
  Command2.Caption = "Unload Form2"
  Command3.Caption = "Unload All"
End Sub

Private Sub UnloadForms(strName As String)
  Dim frm As Form
  For Each frm In Forms
    If InStr(frm.Name, strName) > 0 Then
      Unload frm
    End If
  Next frm
End Sub

[EDIT 2] [编辑2]

below is the sub which i use to unload all my forms, this sub is in a module so i can call it everywhere 下面是我用来卸载所有表格的子程序,该子程序位于模块中,因此我可以在任何地方调用它

Public Sub UnloadAll(strExcept As String, blnKeepMDI As Boolean)
  Dim frm As Form
  'unload all other forms
  For Each frm In Forms
    Select Case frm.Name
      Case "mdiPaneel"   'unload mdi as last
      Case "frmMsg"      'dont unload msg unless specified
      Case strExcept     'dont unload this if mdi stays
        If blnKeepMDI = False Then
          Unload frm
        End If
      Case Else
        Unload frm
    End Select
  Next frm
  If blnKeepMDI = False Then
    'unload mdi and finish program
    Unload mdiPaneel
  End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM