在Windows 7 SP1 x64中监视打开或唤醒命令

时间:2014-12-27 04:39:44

标签: vb.net

我有下面的代码,它成功地“唤醒”了睡眠显示器,但它只是暂时唤醒它,它闪烁然后后退。我已经尝试了各种计时器和循环来重新发出命令,但是看起来很难在cpu上,有没有人有任何关于如何让它打开屏幕10分钟的想法,甚至只是转动它在等待操作系统稍后再关闭它?

我也查看了整个发送键的事情,但是在Windows 7中似乎没有做任何事情,这是一个重要的注意事项,这个代码的dredit在很大程度上要归功于StackOverflow的成员!干杯

以下是代码:

Public Class MonitorHelper

Private Const WM_SYSCOMMAND As Integer = &H112
Private SC_MONITORPOWER As New IntPtr(&HF170)
Private HWND_BROADCAST As Integer = (&HFFFF&)
Private MONITOR_ON As New IntPtr(-1)
Private MONITOR_OFF As New IntPtr(2)


Public Sub TurnOnScreen(ByVal form As Form)
    NativeMethods.PostMessage(New HandleRef(Me, HWND_BROADCAST), WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON)
End Sub

<SuppressUnmanagedCodeSecurity()> _
Private Class NativeMethods

    <DllImport("user32.dll")> _
    Public Shared Function PostMessage( _
            ByVal hWnd As HandleRef, _
            ByVal message As Integer, _
            ByVal lParam As IntPtr, _
            ByVal wParam As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

End Class
End Class

public class form3
Private monitorHelper1 As New MonitorHelper
End Class

调用显示器开启:

monitorHelper1.TurnOnScreen(Me)

1 个答案:

答案 0 :(得分:0)

所以从另一个方向看这个,不是如何打开它以及如何让它不能在一个回合的路上睡觉,我有这个工作代码...感谢Hannes Du Preez。

Imports System.Runtime.InteropServices 'APIs
Imports Microsoft.Win32         'For System Events

Public Class frmMonitor

<FlagsAttribute()> _
Public Enum EXECUTION_STATE As UInteger ' Determine Monitor State
    ES_AWAYMODE_REQUIRED = &H40
    ES_CONTINUOUS = &H80000000UI
    ES_DISPLAY_REQUIRED = &H2
    ES_SYSTEM_REQUIRED = &H1
    ' Legacy flag, should not be used.
    ' ES_USER_PRESENT = 0x00000004
End Enum

'Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.

<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function SetThreadExecutionState(ByVal esFlags As EXECUTION_STATE) As    EXECUTION_STATE
End Function

'This function queries or sets system-wide parameters, and updates the user profile during the process.
<DllImport("user32", EntryPoint:="SystemParametersInfo", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function SystemParametersInfo(ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
End Function

Private Const SPI_SETSCREENSAVETIMEOUT As Int32 = 15

Public Sub KeepMonitorActive()

    SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED + EXECUTION_STATE.ES_CONTINUOUS) 'Do not Go To Sleep

End Sub

Public Sub RestoreMonitorSettings()

    SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS) 'Restore Previous Settings, ie, Go To Sleep Again

End Sub