我正在尝试使用 WmiSetBrightness 方法调整显示器的亮度(不是伽马斜坡值),但系统在运行时抛出 ManagementException 说“不支持”。我见过很多人使用这个概念并成功运行。那我的有什么问题吗?是不是因为这在 Windows 7 Ultimate 中不受支持?我在哪个工作?我的代码如下:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1_Click
Try
Dim mclass As New ManagementClass("WmiMonitorBrightnessMethods")
mclass.Scope = New ManagementScope("\\.\root\wmi")
Dim instances As ManagementObjectCollection = mclass.GetInstances()
For Each instance As ManagementObject In instances
Dim brightness As Byte = 50 'In percent.
Dim timeout As UInt64 = 1 'In seconds.
Dim args As Object() = New Object() {brightness, timeout}
instance.InvokeMethod("WmiSetBrightness", args) 'Only work on the first object.
Exit For
Next
Catch ex As ManagementException
MessageBox.Show(ex.Message)
End Try
End Sub
为了测试实例是否从字面上获得,我注释了 foreach 部分并尝试通过 MessageBox 使用 instance.Count.ToString() 打印实例的计数,但仍然抛出异常。
作为替代方案,我尝试使用 DeviceIOControl 和 IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS 但没有成功。代码如下:
Public Class Form1
Private Declare Auto Function CreateFile Lib "kernel32" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Int32, _
ByVal dwFlagsAndAttributes As Int32, ByVal hTemplateFile As IntPtr) As IntPtr
Const GENERIC_READ As Int32 = &H80000000%
Const SHARE_ALL As Int32 = &H7%
Const OPEN_EXISTING As Int32 = &H3%
Const IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS As Integer = &H23049C%
Private Structure BRIGHTNESS
Public ucDisplayPolicy As Byte
Public ucACBrightness As Byte
Public ucDCBrightness As Byte
End Structure
Private Declare Auto Function DeviceIoControl Lib "kernel32" _
(ByVal hDevice As IntPtr, ByVal dwIoControlCode As Int32, _
ByRef lpInBuffer As BRIGHTNESS, ByVal nInBufferSize As Int32, _
ByVal lpOutBuffer As IntPtr, ByVal nOutBufferSize As Int32, _
ByRef lpBytesReturned As Int32, ByVal lpOverlapped As IntPtr) As Boolean
Private Declare Auto Function CloseHandle Lib "kernel32" _
(ByVal hObject As IntPtr) As Boolean
''' <summary>
''' Set the brightness of the screen.
''' </summary>
''' <param name="Brightness">The Screen Brightness.</param>
Private Sub SetBrightness(ByRef Brightness As BRIGHTNESS)
' Get the display.
Dim pDisplay As IntPtr = _
CreateFile("\\.\LCD", GENERIC_READ, SHARE_ALL, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
' Set the brightness.
DeviceIoControl(pDisplay, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, _
Brightness, Runtime.InteropServices.Marshal.SizeOf(Brightness), IntPtr.Zero, 0, Nothing, IntPtr.Zero)
' Close the handle.
CloseHandle(pDisplay)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim brightness As BRIGHTNESS
' The brightness on DC Power.
brightness.ucDCBrightness = 100
' The Brightness on AC Power.
brightness.ucACBrightness = 100
' Call the function.
SetBrightness(brightness)
End Sub
End Class
由于某种原因,DeviceIoControl 总是返回 False。请协助整理这些。
顺便说一句,这不是关于使用 SetDeviceGammaRamp 修改伽马斜坡值,因为它会调整显示器的对比度;不是亮度。所以,关于这个问题的任何帖子都不会有太大帮助。
问候!