如何使用 Visual Basic 打开/唤醒屏幕

How to turn on/ wake the screen with Visual Basic

我正在为这个打开显示器的代码而苦恼。我在 Windows 10 控制面板中设置了显示屏在 10 分钟后关闭,但是我有一个语音识别应用程序是用 VB 编写的,当我通过语音调用该应用程序时需要唤醒屏幕.那么有没有通过Visual Basic唤醒监视器的代码呢?我搜索了很长时间,但只找到了一些 Java 代码。谢谢,祝你 day/night 愉快! :)

K,所以,在 the article on codeproject, some of the answers given to this question and pinvoke.net 的指导下,我已经开始工作了。它可能更干净,但我把它留给你:)。毕竟这是你的项目;)。

Imports System.Runtime.InteropServices

Public Class Monitor
    Private Const WM_SYSCOMMAND As Integer = &H112
    Private Const SC_MONITORPOWER As Integer = &HF170
    Private Const HWND_BROADCAST As Integer = &HFFFF
    Private Const INPUT_MOUSE As Integer = 0
    Private Const MOUSEEVENTF_MOVE As Integer = 1

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function SendInput(<[In]()> ByVal nInput As UInt32,<[In](), MarshalAs(UnmanagedType.LPArray, ArraySubtype:=UnmanagedType.Struct, SizeParamindex:=0)> ByVal pInputs() As INPUT, <[In]()> ByVal cbInput As Int32) As UInt32
    End Function

    Private Structure INPUT
        Public type As Integer
        Public dx As Integer
        Public dy As Integer
        Public mouseData As Integer
        Public dwFlags As Integer
        Public time As Integer
        Public dwExtraInfo As IntPtr
    End Structure


    Public Shared Sub Disable()
         SendMessage(New IntPtr(HWND_BROADCAST), WM_SYSCOMMAND, SC_MONITORPOWER, New IntPtr(2))
    End Sub

    Public Shared Sub Enable()
        Dim input = New INPUT()

        input.type = INPUT_MOUSE
        input.dx = 1
        input.dy = 0
        input.mouseData = 0
        input.dwFlags = MOUSEEVENTF_MOVE
        input.time = 0
        input.dwExtraInfo = IntPtr.Zero

        SendInput(1, {input}, 28)
    End Sub
End Class