2014-11-23 131 views
0

我到处寻找并找不到答案。如果表单处于活动状态或非活动状态,如何获得布尔值。如何在vb.net中获得窗体活动状态的值

伪:

'If the form is active 
'Do this 
'else If the form is not active 
'Do this 

谢谢

+0

没有,你必须使自己的。使用激活和停用事件。 – 2014-11-23 19:40:08

+0

这很有道理!从来没有想过,谢谢。 – 2014-11-23 19:41:57

回答

1

这是我用到底:

Private formActive As Boolean 

Private Sub form1_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated 
formActive = True 
End Sub 

Private Sub mainForm_Deactivate(sender As Object, e As EventArgs) Handles MyBase.Deactivate 
formActive = False 
End Sub 

然后:

If formActive = True 
'If the form is active 
else 
'If the form is not active 
End If 
0

这个怎么样?它使用Form类的ActiveForm property

If Form.ActiveForm.Name = "yourForm" Then 
     'Do Events 1 
    Else 
     'Do Events 2 
    End If 
+0

我试过这个,并得到以下错误:“对象引用未设置为对象的实例。” – 2014-11-23 19:37:27

2

使用此,它获取活动窗口的hWnd,然后把它比作形式的hWnd。

Public Declare Function GetActiveWindow Lib "user32"() As System.IntPtr 
If GetActiveWindow() = Me.Handle Then 
    lblIsActive.Text = "active" 
Else 
    lblIsActive.Text = "not active" 
End If 
相关问题