Dim Stop_S As BooleanPrivate Sub Command1_Click() Command1.Enabled = False Command2.Enabled = True Stop_S = False Do While 1 = 1 Wait_S (10) If Stop_S = True Then Exit Do Else SendKeys "{F16}" End If Loop End SubPrivate Sub Command2_Click() Command1.Enabled = True Command2.Enabled = False Stop_S = True End SubPrivate Sub Form_Load() Command1.Caption = "开始监控" Command1.Enabled = True Command2.Caption = "停止监控" Command2.Enabled = False Stop_S = False End SubPublic Sub Wait_S(SecondV As Single) Dim i As Single, j As Single i = Timer + SecondV Do While i > Timer If Timer < 1 And i >= 86400 Then i = i - 86400 End If DoEvents Loop End Sub
以上是每隔10秒结束一下屏保 一个小时可以写Wait_S (3600)
Option Explicit
PPrivate Const SPI_SCREENSAVERRUNNING = 114Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Declare Function SystemParametersInfoSS Lib "user32" Alias "SystemParametersInfoA" _ (ByVal uAction As Long, ByVal uParam As Long, _ ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long'' 测试屏幕保护是否在运行 Public Function IsScreenSaverRunning() As Boolean Dim lRetVal As Long Dim lRunning As Long
On Error Resume Next lRetVal = SystemParametersInfoSS(SPI_SCREENSAVERRUNNING, 0&, lRunning, 0&) lRetVal = SystemParametersInfoSS(SPI_SCREENSAVERRUNNING, lRunning, 0&, 0&) If lRunning <> 0 Then IsScreenSaverRunning = True End If End FunctionPrivate Sub Form_Load() Timer1.Enabled = True Timer1.Interval = 60000 ' 每分钟触发一次 End SubPrivate Sub Timer1_Timer() Static i As Long Dim hdesk As Long i = i + 1 If i >= 60 Then ' 如果一小时 If IsScreenSaverRunning Then '' 如果屏保在运行 Debug.Print "OK", Timer Sleep 5000 ' 加上这句Sleep,可等待屏保程序完全运行起来,否则下面的语句可能会影 ' 响其他程序的运行或不能使其退出 SendKeys "{F16}" End If i = 0 End If End Sub 以上方法并不能退出有密码的屏保程序,所以我建议你在程序运行时使用下面的方法来禁止系统运行屏幕保护 SystemParametersInfo SPI_SETSCREENSAVEACTIVE, False, 0, SPIF_SENDWININICHANGE
Command1.Enabled = False
Command2.Enabled = True
Stop_S = False
Do While 1 = 1
Wait_S (10)
If Stop_S = True Then
Exit Do
Else
SendKeys "{F16}"
End If
Loop
End SubPrivate Sub Command2_Click()
Command1.Enabled = True
Command2.Enabled = False
Stop_S = True
End SubPrivate Sub Form_Load()
Command1.Caption = "开始监控"
Command1.Enabled = True
Command2.Caption = "停止监控"
Command2.Enabled = False
Stop_S = False
End SubPublic Sub Wait_S(SecondV As Single)
Dim i As Single, j As Single
i = Timer + SecondV
Do While i > Timer
If Timer < 1 And i >= 86400 Then
i = i - 86400
End If
DoEvents
Loop
End Sub
Option Explicit
PPrivate Const SPI_SCREENSAVERRUNNING = 114Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function SystemParametersInfoSS Lib "user32" Alias "SystemParametersInfoA" _
(ByVal uAction As Long, ByVal uParam As Long, _
ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long'' 测试屏幕保护是否在运行
Public Function IsScreenSaverRunning() As Boolean
Dim lRetVal As Long
Dim lRunning As Long
On Error Resume Next
lRetVal = SystemParametersInfoSS(SPI_SCREENSAVERRUNNING, 0&, lRunning, 0&)
lRetVal = SystemParametersInfoSS(SPI_SCREENSAVERRUNNING, lRunning, 0&, 0&)
If lRunning <> 0 Then
IsScreenSaverRunning = True
End If
End FunctionPrivate Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 60000 ' 每分钟触发一次
End SubPrivate Sub Timer1_Timer()
Static i As Long
Dim hdesk As Long
i = i + 1
If i >= 60 Then
' 如果一小时
If IsScreenSaverRunning Then
'' 如果屏保在运行
Debug.Print "OK", Timer
Sleep 5000
' 加上这句Sleep,可等待屏保程序完全运行起来,否则下面的语句可能会影
' 响其他程序的运行或不能使其退出
SendKeys "{F16}"
End If
i = 0
End If
End Sub
以上方法并不能退出有密码的屏保程序,所以我建议你在程序运行时使用下面的方法来禁止系统运行屏幕保护
SystemParametersInfo SPI_SETSCREENSAVEACTIVE, False, 0, SPIF_SENDWININICHANGE
在進程中搜索一下,有就kill掉。