有没有办法使用vba excel跟踪窗口锁定和解锁时间

时间:2015-06-29 13:21:41

标签: excel vba

我想跟踪Windows锁定和解锁的时间 有没有办法使用vba excel跟踪窗口锁定和解锁时间?

提前致谢。

2 个答案:

答案 0 :(得分:0)

最好在事后查看事件日志,而不是在发生这种情况时尝试使用Excel实时跟踪。见this answer for the event log id values.

SuperUser上还有this post

答案 1 :(得分:0)

Private Declare Function SwitchDesktop Lib "user32" (ByVal hDesktop As Long) As Long
Private Declare Function OpenDesktop Lib "user32" Alias "OpenDesktopA" (ByVal lpszDesktop As String, ByVal dwFlags As Long, ByVal fInherit As Long, ByVal dwDesiredAccess As Long) As Long
Private Declare Function CloseDesktop Lib "user32" (ByVal hDesktop As Long) As Long
Private Const DESKTOP_SWITCHDESKTOP As Long = &H100


Function Check_If_Locked() As String
    Dim p_lngHwnd As Long
    Dim p_lngRtn As Long
    Dim p_lngErr As Long
    p_lngHwnd = OpenDesktop(lpszDesktop:="Default", dwFlags:=0, fInherit:=False, dwDesiredAccess:=DESKTOP_SWITCHDESKTOP)
    If p_lngHwnd = 0 Then
        system = "Error"
    Else
        p_lngRtn = SwitchDesktop(hDesktop:=p_lngHwnd)
        p_lngErr = Err.LastDllError

        If p_lngRtn = 0 Then
            If p_lngErr = 0 Then
                system = "Locked"
            Else
                system = "Error"
            End If
        Else
            system = "Unlocked"
        End If
        p_lngHwnd = CloseDesktop(p_lngHwnd)
    End If
    Check_If_Locked = system
End Function

Private Sub Form_Timer()
Debug.Print Check_If_Locked
End Sub