2014-11-23 43 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 
相關問題