我正在尝试创建一个简单的应用程序,根据时间和日期打开或关闭PC的显示器。使用PostMessage命令,我可以做到这一点,但似乎如果我把它放在计时器中,它就无法工作。
Public Class Form1
Private Const HWND_BROADCAST As Long = &HFFFF&
Private Const WM_SYSCOMMAND As Long = &H112&
Private Const SC_MONITORPOWER As Long = &HF170&
Private Const POWER_ON = -1
Private Const POWER_LOW = 1
Private Const POWER_OFF = 2
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Sub monitors(ByVal powerState As Integer)
PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, powerState)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim rightNow As DateTime = DateTime.Now()
Select Case rightNow.DayOfWeek
Case 1 To 5
Label1.Text = "Week day" & vbCrLf & "Auto-power enabled" & vbCrLf & rightNow.Hour & ":" & rightNow.Minute
If rightNow.Hour >= 17 And rightNow.Minute >= 30 Then
monitors(POWER_OFF)
ElseIf rightNow.Hour >= 8 And rightNow.Minute >= 30 Then
monitors(POWER_ON)
End If
Case 0 Or 6
Label1.Text = "Week end" & vbCrLf & "Auto power-off."
monitors(POWER_OFF)
End Select
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
monitors(POWER_OFF)
End Sub
End Class
这就是我正在使用的;计时器启用设置为5000的间隔。我认为也许计时器做的一些检查是无意中立刻唤醒了计算机,所以我试图只是把邮件放在没有运气的情况下。然而,button1_click中的调用工作得很好。
这是我做错了吗?是否有某种安全措施可以禁止在这样的计时器中使用postmessage / sendmessage?