スリープ抑止VB.NET版

VB.NETに変換してどう変わるか試してみました。

0xは&hに置き換わるんですね。

変換機能を使っただけですが、問題なく起動しました。

 

Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Partial Class MainForm
Inherits Form
<FlagsAttribute> _
Public Enum ExecutionState As UInteger
    ' 関数が失敗した時の戻り値
    Null = 0
    ' スタンバイを抑止
    SystemRequired = 1
    ' 画面OFFを抑止
    DisplayRequired = 2
    ' 効果を永続させる。ほかオプションと併用する。
    Continuous = &H80000000UI
End Enum

<DllImport("kernel32.dll")> _
Private Shared Function SetThreadExecutionState(esFlags As ExecutionState) As ExecutionState
End Function
<DllImport("user32.dll")> _
Private Shared Function SendInput(nInputs As UInteger, pInputs As INPUT(), cbSize As Integer) As UInteger
End Function
' アンマネージ DLL 対応用 struct 記述宣言
<StructLayout(LayoutKind.Sequential)> _
Private Structure INPUT
    Public type As Integer
    ' 0 = INPUT_MOUSE(デフォルト), 1 = INPUT_KEYBOARD
    Public mi As MOUSEINPUT
End Structure
' アンマネージ DLL 対応用 struct 記述宣言
<StructLayout(LayoutKind.Sequential)> _
  Private Structure MOUSEINPUT
  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

' dwFlags
Const MOUSEEVENTF_MOVED As Integer = &H1
Const MOUSEEVENTF_LEFTDOWN As Integer = &H2' 左ボタン Down
Const MOUSEEVENTF_LEFTUP As Integer = &H4' 左ボタン Up
Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8' 右ボタン Down
Const MOUSEEVENTF_RIGHTUP As Integer = &H10' 右ボタン Up
Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20' 中ボタン Down
Const MOUSEEVENTF_MIDDLEUP As Integer = &H40' 中ボタン Up
Const MOUSEEVENTF_WHEEL As Integer = &H80
Const MOUSEEVENTF_XDOWN As Integer = &H100
Const MOUSEEVENTF_XUP As Integer = &H200
Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000
Const screen_length As Integer = &H10000' for MOUSEEVENTF_ABSOLUTE (この値は固定)

Public Sub New()
    InitializeComponent()
End Sub

Private Sub Timer1Tick(sender As Object, e As EventArgs)
    '画面暗転阻止
    SetThreadExecutionState(ExecutionState.DisplayRequired)

    ' ドラッグ操作の準備 (struct 配列の宣言)
    Dim input As INPUT() = New INPUT(0) {}
    ' イベントを格納
    ' ドラッグ操作の準備 (イベントの定義 = 相対座標へ移動)
    input(0).mi.dx = 0
    ' 相対座標で0 つまり動かさない
    input(0).mi.dy = 0
    ' 相対座標で0 つまり動かさない
    input(0).mi.dwFlags = MOUSEEVENTF_MOVED

    ' ドラッグ操作の実行 (イベントの生成)
   SendInput(1, input, Marshal.SizeOf(input(0)))
End Sub
End Class