【问题标题】:How to prevent Laptop/ Computer screen from locking using VBA?如何使用 VBA 防止笔记本电脑/计算机屏幕锁定?
【发布时间】:2023-01-27 10:21:22
【问题描述】:

我在 Excel Visual Basic 模块中有代码,可以防止笔记本电脑/计算机屏幕锁定。

我在模块中调用的鼠标事件非常麻烦。

mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTFaha_LEFTUP, 0, 0, 0, 0

是否有较少的侵入性事件会阻止屏幕锁定?

这正在更广泛的自动化中使用,因此解决方案必须是 VBA。

完整代码:

Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As 
 Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
 Public Const MOUSEEVENTF_LEFTDOWN = &H2
 Public Const MOUSEEVENTF_LEFTUP = &H4
 Dim TimerActive As Boolean

Sub KeepWindowsActive()
TimerActive = True
'move cursor and click
SetCursorPos 200, 200 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTFaha_LEFTUP, 0, 0, 0, 0
Application.OnTime Now + TimeValue("00:03:00"), "KeepWindowsActive"

End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:
    Option Explicit
    
    Private Declare PtrSafe Function SetThreadExecutionState Lib "Kernel32.dll" (ByVal esFlags As Long) As Long
    
    Const ES_SYSTEM_REQUIRED As Long = &H1
    Const ES_DISPLAY_REQUIRED As Long = &H2
    Const ES_CONTINUOUS As Long = &H80000000
    
    ' Enable away mode and prevent the sleep idle time-out. Called periodically this is needed (every 59 sec, change to longer interval if you want)
    ' I've this sub called in Workbook_Open
    Public Sub PreventLocking()
        SetThreadExecutionState ES_CONTINUOUS Or ES_DISPLAY_REQUIRED Or ES_SYSTEM_REQUIRED
        Application.OnTime EarliestTime:=Now + TimeValue("00:00:59"), Procedure:="PreventLocking"
    End Sub
    
    ' Clear flags to disable away mode and allow the system to idle to sleep normally.
    ' I've this sub called in Workbook_BeforeClose, but it would probably be fine even w/o it
    Public Sub ClearLockingFlags()
        SetThreadExecutionState ES_CONTINUOUS
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2014-07-08
      • 2010-10-21
      • 1970-01-01
      • 2020-02-05
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多