Vb6 如何自动关闭模式窗体

Vb6 如何自动关闭模式窗体,vb6,Vb6,我有一个VB6项目,我实现了一个带有自动注销的会话计时器 我的问题是,如果打开了一个模式表单,该表单不在表单集合中,因此我无法知道是否打开该表单以卸载它。对于非模态形式,没有问题 如果有人有主意的话?是否可以显示所有打开的表单(非模态和模态) 谢谢你的回复 编辑:代码段: 表单创建: Dim FormLoc As New F_Options FormLoc.Show vbModal 表格销毁: For Each mFrm In Forms unload mFrm next F

我有一个VB6项目,我实现了一个带有自动注销的会话计时器

我的问题是,如果打开了一个模式表单,该表单不在表单集合中,因此我无法知道是否打开该表单以卸载它。对于非模态形式,没有问题

如果有人有主意的话?是否可以显示所有打开的表单(非模态和模态)

谢谢你的回复

编辑:代码段:

表单创建:

Dim FormLoc As New F_Options
FormLoc.Show vbModal
表格销毁:

For Each mFrm In Forms
    unload mFrm
next    

For Each mFrm In F_MDIParent.MDIActiveX1.Forms
    unload mFrm
next 
主窗体是MDI窗体。其他表单可以在MDI Mainform中打开和停靠,但有些表单是“正常”的

编辑:

我根据一个项目进行了一些测试和表单收集,我认为这个收集对于应用程序来说是全局的,但显然不是


我想关闭主项目中的所有表单。我可以让另一个项目打开一个模式表单(所有应用程序90个项目)

一个可行的替代方案

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

一个可行的替代方案

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

模态、非模态和默认表单确实显示在表单集合中

运行以下测试项目以查看它的发生:

'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
[编辑]

在form2中添加3个命令按钮,并单击它们以测试卸载时发生的情况

在从ide运行时对其进行测试,并注意在显示模式窗体、显示非模式窗体(有所有者)和显示默认窗体(无所有者)时的区别。。请注意ide中的“运行”按钮。。当卸载显示模型表单的form1时,应用程序将保持运行

这是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
[编辑2]

下面是我用来卸载所有表单的sub,这个sub位于一个模块中,因此我可以在任何地方调用它

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

模态、非模态和默认表单确实显示在表单集合中

运行以下测试项目以查看它的发生:

'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
[编辑]

在form2中添加3个命令按钮,并单击它们以测试卸载时发生的情况

在从ide运行时对其进行测试,并注意在显示模式窗体、显示非模式窗体(有所有者)和显示默认窗体(无所有者)时的区别。。请注意ide中的“运行”按钮。。当卸载显示模型表单的form1时,应用程序将保持运行

这是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
[编辑2]

下面是我用来卸载所有表单的sub,这个sub位于一个模块中,因此我可以在任何地方调用它

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

如果您只想确保它已关闭,也可以使用Windows API函数:

私有声明函数CloseWindow Lib“user32”别名“CloseWindow”(ByVal hwnd As Long)为Long 私有声明函数findwindowlib“user32”别名“FindWindowA”(ByVal lpClassName作为字符串,ByVal lpWindowName作为字符串),长度为

暗淡如长 wid=FindWindow(vbnullstring,“”)
如果wid为0,则关闭窗口wid

如果您只想确保它已关闭,也可以使用Windows API函数:

私有声明函数CloseWindow Lib“user32”别名“CloseWindow”(ByVal hwnd As Long)为Long 私有声明函数findwindowlib“user32”别名“FindWindowA”(ByVal lpClassName作为字符串,ByVal lpWindowName作为字符串),长度为

暗淡如长 wid=FindWindow(vbnullstring,“”)
如果wid为0,则关闭窗口wid

什么打开了表单?如果它不是由您/您的代码创建的,那么唯一的选择可能是向它发布一条
WM_CLOSE
消息。如果加载了表单,则它会显示在表单集合中,而不考虑模式。你是说“窗口”吗?非VB窗体窗口确实是个骗局。该窗体是由我的代码用.show创建的。表单已正确加载,但未显示在表单集合中。@user1069516能否添加一个片段来演示您描述的行为?问题可能在于卸载的顺序。我通常在一个模块中创建一个子模块用于卸载,并传递调用表单的名称,然后循环遍历表单,然后卸载所有表单,调用表单除外,我最后单独卸载了调用表单。什么打开了表单?如果它不是由您/您的代码创建的,那么唯一的选择可能是向它发布一条
WM_CLOSE
消息。如果加载了表单,则它会显示在表单集合中,而不考虑模式。你是说“窗口”吗?非VB窗体窗口确实是个骗局。该窗体是由我的代码用.show创建的。表单已正确加载,但未显示在表单集合中。@user1069516能否添加一个片段来演示您描述的行为?问题可能在于卸载的顺序。我通常在一个模块中为卸载创建一个子模块,并传递调用表单的名称,然后循环遍历表单,然后卸载所有表单,但调用表单除外,我最后单独卸载它