不支持WmiSetBrightness方法(.Net 4.0,Winforms,Windows 7). [英] WmiSetBrightness method not supported (.Net 4.0, Winforms, Windows 7).

查看:106
本文介绍了不支持WmiSetBrightness方法(.Net 4.0,Winforms,Windows 7).的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WmiSetBrightness方法调整显示器的亮度(而不是伽玛渐变值),但是系统在运行时抛出ManagementException,提示不支持".我已经看到很多人使用此概念并成功运行.那我的错是什么呢?是因为Windows 7 Ultimate不支持此功能吗?我在哪工作?
我的代码如下:

I''m trying to adjust the brightness (not the gamma ramp values) of the monitor using WmiSetBrightness method but the system throws a ManagementException during runtime saying ''Not supported''. I have seen a lot of people using this concept and running successfully. So what''s wrong with mine? Is it because this is not supported in Windows 7 Ultimate; on which I''m working?
My Code follows:

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,但没有成功.代码如下:



To test if the instances are literally obtained, I commented the foreach part and tried to print the count of the instances using instances.Count.ToString() through a MessageBox but still the exception is thrown.

As an alternative, I tried to work with DeviceIOControl and IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS but of no success. The code follows:

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修改伽玛斜率值无关,因为它可以调整显示器的对比度.不是亮度.因此,有关此事的任何帖子都不会有太大帮助.



For some reason DeviceIoControl always returns False.
Please assist in sorting these out.

BTW, this is not about modifying gamma ramp values using SetDeviceGammaRamp, as it adjusts the contrast of the monitor; not the brightness. So, any post on that matter won''t be of much help.

Regards!

推荐答案

这仅在您的显示驱动程序支持此功能的情况下才有效.显然不是.

完全没有说明制造商必须在其驱动程序软件中添加WMI支持.
This will only work if youre display driver supports this. Apparently, it doesn''t.

There is nothing at all that says a manufacturer has to add WMI support to their driver software.


这篇关于不支持WmiSetBrightness方法(.Net 4.0,Winforms,Windows 7).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆