首页 » 程序源码 » 其他源码 » 显示器亮度,对比度,色温缩放调节,笔记本亮度调节AUTOIT源码

显示器亮度,对比度,色温缩放调节,笔记本亮度调节AUTOIT源码

通过调用dxva2.dll文件,修改显示器的设置,和按显示器上的物理按钮效果一样,有效的解决显示器上按钮不好设置,按钮功能不明的问题,有些显示器不支持,如果你使用的无效果,说明你显示器不支持此调节功能!下载地址:源码

#include <WinAPI.au3>
#include <WinAPIFiles.au3>
#include <WinAPIGdi.au3>
#include <GUIConstantsEx.au3>
#include <GuiSlider.au3>
#include <Array.au3>
Opt("GUIOnEventMode", 1)
$g_szVersion = "RU1Y2-D59U9"
If WinExists($g_szVersion) Then Exit
AutoItWinSetTitle($g_szVersion)
Global Const $Dxva2 = DllOpen("Dxva2.dll")
If @error Then Exit 1
Global Const $tagSTRUCT = "HANDLE hPhysicalMonitor;WCHAR szPhysicalMonitorDescription[128];"
Global Const $tag_MC_TIMING_REPORT = "BYTE bTimingStatusByte; DWORD dwHorizontalFrequencyInHZ; DWORD dwVerticalFrequencyInHZ"
Global Const $tag_DISPLAY_BRIGHTNESS = "byte ucDisplayPolicy; byte ucACBrightness; byte ucDCBrightness"
Global Const $IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS = _WinAPI_IOCTL($FILE_DEVICE_VIDEO, 0x125, $METHOD_BUFFERED, $FILE_ANY_ACCESS)
Global Const $IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS = _WinAPI_IOCTL($FILE_DEVICE_VIDEO, 0x126, $METHOD_BUFFERED, $FILE_ANY_ACCESS)
Global Const $IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS = _WinAPI_IOCTL($FILE_DEVICE_VIDEO, 0x127, $METHOD_BUFFERED, $FILE_ANY_ACCESS)
Global $idSlider1, $idSlider2, $idButton1, $idButton2, $idLabel1, $idLabel2
Global $hMonitor, $brightness, $contrast, $switch1, $switch2, $VCPReply
Global $idLabel3, $idSlider3, $hDevice, $BRIGHTNESS_LCD_Array
$hDevice = LCD()
If Not @error Then
        LCD_Set($hDevice)
        If $hDevice Then _WinAPI_CloseHandle($hDevice)
        DllClose($Dxva2)
        Exit
EndIf
$hMonitor = _WinAPI_MonitorFromWindow(_WinAPI_GetDesktopWindow(), $MONITOR_DEFAULTTOPRIMARY) ;返回主显示器句柄
$hMonitor = _GetPhysicalMonitorsFromHMONITOR($hMonitor) ;获取物理显示器句柄
$StringLength = CapabilitiesRequestAndCapabilitiesReply($hMonitor)
$Gui = GUICreate("显示器设置", 262, 220, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
$idSlider1 = GUICtrlCreateSlider(10, 30, 240, 20, BitOR($TBS_TOOLTIPS, $TBS_AUTOTICKS))
GUICtrlSetOnEvent($idSlider1, "Brightness")
GUICtrlSetLimit($idSlider1, 100)
$idSlider2 = GUICtrlCreateSlider(10, 82, 240, 20, BitOR($TBS_TOOLTIPS, $TBS_AUTOTICKS))
GUICtrlSetOnEvent($idSlider2, "Contrast")
GUICtrlSetLimit($idSlider2, 100)
$idButton1 = GUICtrlCreateButton("宽屏切换", 16, 160, 85, 25)
GUICtrlSetOnEvent($idButton1, "Button1")
$idButton2 = GUICtrlCreateButton("恢复默认", 158, 160, 85, 25)
GUICtrlSetOnEvent($idButton2, "Button2")
$idLabel1 = GUICtrlCreateLabel("  亮度调节:不可用", 10, 10, 140, 20)
$idLabel2 = GUICtrlCreateLabel("对比度调节:不可用", 10, 60, 140, 20)
$Combo1 = GUICtrlCreateCombo("", 102, 120, 100, 25, 0x0003)
GUICtrlSetOnEvent($Combo1, "Color")
GUICtrlCreateLabel("色温调节", 32, 124, 48, 20)
_GUICtrlSlider_SetTipSide($idSlider1, $TBTS_TOP)
_GUICtrlSlider_SetTipSide($idSlider2, $TBTS_TOP)
If $StringLength <> "" Then
        $VCP_Array = Manage_vcp($StringLength)
        $VCPReply = _GetVCPFeatureAndVCPFeatureReply($hMonitor, 0x86)
        If $VCPReply[0] = 0 Then GUICtrlSetState($idButton1, $GUI_DISABLE)
        $brightness = _GetMonitorBrightness($hMonitor) ;获取监视器亮度
        ConsoleWrite($brightness[1] & @CRLF)
        $contrast = _GetMonitorContrast($hMonitor) ;获取监视器对比度
        If $brightness[2] <> 0 Then $switch1 = True
        If $contrast[2] <> 0 Then $switch2 = True
        If $switch1 Then
                GUICtrlSetData($idSlider1, $brightness[1])
                GUICtrlSetData($idLabel1, "设置亮度:" & $brightness[1])
        EndIf
        If $switch2 Then
                GUICtrlSetData($idSlider2, $contrast[1])
                GUICtrlSetData($idLabel2, "设置对比度:" & $contrast[1])
        EndIf
        
        For $i = 0 To UBound($VCP_Array) - 1
                If $VCP_Array[$i][0] = "14" Then
                        If $VCP_Array[$i][1] <> "" Then
                                $Color_Array = StringSplit($VCP_Array[$i][1], " ", 2)
                                $Color_String = ""
                                For $S = 0 To UBound($Color_Array) - 1
                                        $temp = Select_Color($Color_Array[$S])
                                        If $temp <> "" Then $Color_String &= $temp & "|"
                                Next
                        EndIf
                        ExitLoop
                EndIf
        Next
        
        If $Color_String <> "" Then
                $VCPCode = _GetVCPFeatureAndVCPFeatureReply($hMonitor, 0x14)
                GUICtrlSetData($Combo1, $Color_String, Select_Color($VCPCode[1] - 1))
        Else
                GUICtrlSetData($Combo1, "不支持", "不支持")
                GUICtrlSetState($Combo1, $GUI_DISABLE)
        EndIf
        GUISetState(@SW_SHOW, $Gui)
Else
        WinSetTitle($Gui, "", "未发现DDC/CI")
        GUICtrlSetData($Combo1, "不支持", "不支持")
        GUICtrlSetState($idSlider1, $GUI_DISABLE)
        GUICtrlSetState($idSlider2, $GUI_DISABLE)
        GUICtrlSetState($idButton1, $GUI_DISABLE)
        GUICtrlSetState($Combo1, $GUI_DISABLE)
        GUISetState(@SW_SHOW, $Gui)
EndIf

While 1
        Sleep(50)
WEnd

Func LCD()
        Local $iDevice
        $iDevice = _WinAPI_CreateFileEx("\\.\LCD", $OPEN_EXISTING, $GENERIC_READ + $GENERIC_WRITE, $FILE_ANY_ACCESS, 0)
        Return (@error) ? (SetError(1, _WinAPI_GetLastErrorMessage(), False)) : ($iDevice)
EndFunc   ;==>LCD

Func LCD_Set($iDevice)
        Local $brightness = DllStructCreate("BYTE[256]")
        Local $DISPLAY_BRIGHTNESS = DllStructCreate("BYTE[3]")
        $success = _WinAPI_DeviceIoControl($iDevice, $IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS, Null, 0, DllStructGetPtr($brightness), 0xff)
        If $success = False Then Return
        $BRIGHTNESS_LCD_Array = Brightness_lcd(DllStructGetData($brightness, 1))
        $success = _WinAPI_DeviceIoControl($iDevice, $IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS, Null, 0, DllStructGetPtr($DISPLAY_BRIGHTNESS), 0x03)
        If $success = False Then Return
        $DISPLAY_BRIGHTNESS_READ = Int(BinaryMid(DllStructGetData($DISPLAY_BRIGHTNESS, 1), 3, 1))

        $Gui = GUICreate("内置LCD显示器", 262, 80, -1, -1)
        GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
        $idSlider3 = GUICtrlCreateSlider(10, 30, 240, 20, BitOR($TBS_TOOLTIPS, $TBS_AUTOTICKS))
        GUICtrlSetLimit($idSlider3, 100)
        $idLabel3 = GUICtrlCreateLabel("  亮度调节:", 10, 10, 140, 20)
        GUICtrlSetData($idSlider3, $DISPLAY_BRIGHTNESS_READ)
        GUICtrlSetData($idLabel3, "设置亮度:" & $DISPLAY_BRIGHTNESS_READ)
        GUICtrlSetOnEvent($idSlider3, "Brightness_set_lcd")
        GUISetState(@SW_SHOW, $Gui)
        WinSetTitle($Gui, "", "内置LCD显示器")
        While 1
                Sleep(50)
        WEnd
EndFunc   ;==>LCD_Set

Func Brightness_set_lcd()
        Local $lcd_BRIGHTNESS, $DISPLAY_BRIGHTNESS, $success
        $lcd_BRIGHTNESS = GUICtrlRead($idSlider3)
        GUICtrlSetData($idLabel3, "设置亮度:" & $lcd_BRIGHTNESS)
        For $i = 0 To UBound($BRIGHTNESS_LCD_Array)
                If $lcd_BRIGHTNESS <= $BRIGHTNESS_LCD_Array[$i] Then
                        $DISPLAY_BRIGHTNESS = DllStructCreate($tag_DISPLAY_BRIGHTNESS)
                        DllStructSetData($DISPLAY_BRIGHTNESS, "ucDisplayPolicy", 3)
                        DllStructSetData($DISPLAY_BRIGHTNESS, "ucACBrightness", $BRIGHTNESS_LCD_Array[$i])
                        DllStructSetData($DISPLAY_BRIGHTNESS, "ucDCBrightness", $BRIGHTNESS_LCD_Array[$i])
                        $success = _WinAPI_DeviceIoControl($hDevice, $IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, DllStructGetPtr($DISPLAY_BRIGHTNESS), DllStructGetSize($DISPLAY_BRIGHTNESS), Null, 0)
                        If $success = False Then Return
                        ExitLoop
                EndIf
        Next
EndFunc   ;==>Brightness_set_lcd


Func Select_Color($iValue)
        Local $String
        If StringIsXDigit($iValue) Then $iValue = Dec($iValue)
        Switch $iValue
                Case 1
                        $String = "sRGB"
                Case 2
                        $String = "原生显示"
                Case 3
                        $String = "4000K"
                Case 4
                        $String = "5000K"
                Case 5
                        $String = "6500K"
                Case 6
                        $String = "7500K"
                Case 7
                        $String = "8200K"
                Case 8
                        $String = "9300K"
                Case 9
                        $String = "10000K"
                Case 10
                        $String = "11500K"
                Case 11
                        $String = "User1"
                Case 12
                        $String = "User2"
                Case 13
                        $String = "User3"
                Case Else
        EndSwitch
        Return $String
EndFunc   ;==>Select_Color

Func Color()
        Local $iValue = GUICtrlRead($Combo1)
        Select
                Case $iValue = "sRGB"
                        _SetVCPFeature($hMonitor, 0x14, 0x01)
                Case $iValue = "原生显示"
                        _SetVCPFeature($hMonitor, 0x14, 0x02)
                Case $iValue = "4000K"
                        _SetVCPFeature($hMonitor, 0x14, 0x03)
                Case $iValue = "5000K"
                        _SetVCPFeature($hMonitor, 0x14, 0x04)
                Case $iValue = "6500K"
                        _SetVCPFeature($hMonitor, 0x14, 0x05)
                Case $iValue = "7500K"
                        _SetVCPFeature($hMonitor, 0x14, 0x06)
                Case $iValue = "8200K"
                        _SetVCPFeature($hMonitor, 0x14, 0x07)
                Case $iValue = "9300K"
                        _SetVCPFeature($hMonitor, 0x14, 0x08)
                Case $iValue = "10000K"
                        _SetVCPFeature($hMonitor, 0x14, 0x09)
                Case $iValue = "11500K"
                        _SetVCPFeature($hMonitor, 0x14, 0x0a)
                Case $iValue = "User1"
                        _SetVCPFeature($hMonitor, 0x14, 0x0b)
                Case $iValue = "User2"
                        _SetVCPFeature($hMonitor, 0x14, 0x0c)
                Case $iValue = "User3"
                        _SetVCPFeature($hMonitor, 0x14, 0x0d)
                Case Else
        EndSelect
EndFunc   ;==>Color

Func Button1()
        Local $VCPCode
        If $switch1 And $switch2 Then
                $VCPCode = _GetVCPFeatureAndVCPFeatureReply($hMonitor, 0x86)
                ConsoleWrite($VCPCode[1] & @CRLF)
                If $VCPCode[1] = 8 Then
                        _SetVCPFeature($hMonitor, 0x86, 0x02)
                        GUICtrlSetData($idButton1, "窄屏")
                Else
                        _SetVCPFeature($hMonitor, 0x86, 0x08)
                        GUICtrlSetData($idButton1, "宽屏")
                EndIf
        EndIf
EndFunc   ;==>Button1

Func Button2()
        GUICtrlSetData($idButton2, "Reset..")
        Sleep(250)
        GUISetState(@SW_HIDE)
        If $switch1 Or $switch2 Then
                _RestoreMonitorFactoryDefaults($hMonitor)
                Sleep(1000)
        EndIf
        _DestroyPhysicalMonitor($hMonitor) ;关闭监视器句柄
        DllClose($Dxva2)
        Exit
EndFunc   ;==>Button2

Func Brightness()
        If Not $switch1 Then Return
        GUICtrlSetData($idLabel1, "设置亮度:" & GUICtrlRead($idSlider1))
        _SetMonitorBrightness($hMonitor, GUICtrlRead($idSlider1))
EndFunc   ;==>Brightness

Func Contrast()
        If Not $switch2 Then Return
        GUICtrlSetData($idLabel2, "设置对比度:" & GUICtrlRead($idSlider2))
        _SetMonitorContrast($hMonitor, GUICtrlRead($idSlider2))
EndFunc   ;==>Contrast

Func _GetMonitorBrightness(Const $h_monitor)
        Local Const $MinimumBrightness = DllStructCreate("dword_ptr")
        Local Const $CurrentBrightness = DllStructCreate("dword_ptr")
        Local Const $MaximumBrightness = DllStructCreate("dword_ptr")
        DllCall($Dxva2, "bool", "GetMonitorBrightness", _
                        "handle", $h_monitor, _
                        "ptr", DllStructGetPtr($MinimumBrightness), _
                        "ptr", DllStructGetPtr($CurrentBrightness), _
                        "ptr", DllStructGetPtr($MaximumBrightness))
        If @error Then Return SetError(1, _WinAPI_GetLastErrorMessage(), False)
        Local Const $brightness[3] = [DllStructGetData($MinimumBrightness, 1), _
                        DllStructGetData($CurrentBrightness, 1), _
                        DllStructGetData($MaximumBrightness, 1)]
        Return $brightness
EndFunc   ;==>_GetMonitorBrightness

Func _GetMonitorContrast(Const $h_monitor)
        Local Const $MinimumContrast = DllStructCreate("dword_ptr")
        Local Const $CurrentContrast = DllStructCreate("dword_ptr")
        Local Const $MaximumContrast = DllStructCreate("dword_ptr")
        DllCall($Dxva2, "bool", "GetMonitorContrast", _
                        "handle", $h_monitor, _
                        "ptr", DllStructGetPtr($MinimumContrast), _
                        "ptr", DllStructGetPtr($CurrentContrast), _
                        "ptr", DllStructGetPtr($MaximumContrast))
        If @error Then Return SetError(1, _WinAPI_GetLastErrorMessage(), False)
        Local Const $contrast[3] = [DllStructGetData($MinimumContrast, 1), _
                        DllStructGetData($CurrentContrast, 1), _
                        DllStructGetData($MaximumContrast, 1)]
        Return $contrast
EndFunc   ;==>_GetMonitorContrast

Func _SetMonitorBrightness(Const $h_monitor, Const $NewBrightness)
        DllCall($Dxva2, "bool", "SetMonitorBrightness", "ptr", $h_monitor, "dword", $NewBrightness)
        Return (@error ? SetError(1, _WinAPI_GetLastErrorMessage(), False) : True)
EndFunc   ;==>_SetMonitorBrightness

Func _SetMonitorContrast(Const $h_monitor, Const $contrast)
        DllCall($Dxva2, "bool", "SetMonitorContrast", "ptr", $h_monitor, "dword", $contrast)
        Return (@error ? SetError(1, _WinAPI_GetLastErrorMessage(), False) : True)
EndFunc   ;==>_SetMonitorContrast

Func _GetPhysicalMonitorsFromHMONITOR(Const $pMonitor)
        Local $Number = DllStructCreate("DWORD")
        Local $i, $tagPhysical, $M = 1
        $Ret = DllCall($Dxva2, "bool", "GetNumberOfPhysicalMonitorsFromHMONITOR", _
                        "handle", $pMonitor, _
                        "ptr", DllStructGetPtr($Number))
        $NumberOfMonitors = DllStructGetData($Number, 1)
        For $i = 1 To $NumberOfMonitors
                $tagPhysical &= $tagSTRUCT
        Next
        $MonitorArray = DllStructCreate($tagSTRUCT)
        $Ret = DllCall($Dxva2, "bool", "GetPhysicalMonitorsFromHMONITOR", _
                        "handle", $pMonitor, _
                        "DWORD", $NumberOfMonitors, _
                        "ptr", DllStructGetPtr($MonitorArray))
        
        Return DllStructGetData($MonitorArray, 1) ;返回第一个显示器句柄
EndFunc   ;==>_GetPhysicalMonitorsFromHMONITOR

Func CapabilitiesRequestAndCapabilitiesReply(Const $h_monitor)
        Local Const $tagStringLength = DllStructCreate("dword_ptr")
        DllCall($Dxva2, "bool", "GetCapabilitiesStringLength", "handle", $h_monitor, "ptr", DllStructGetPtr($tagStringLength))
        Local Const $CapabilitiesStringLength = DllStructGetData($tagStringLength, 1)
        Local Const $CapabilitiesString = DllStructCreate("char[" & $CapabilitiesStringLength & ']')
        DllCall($Dxva2, "bool", "CapabilitiesRequestAndCapabilitiesReply", _
                        "handle", $h_monitor, _
                        "ptr", DllStructGetPtr($CapabilitiesString), _
                        "dword", DllStructGetSize($CapabilitiesString))
        Return (@error) ? (SetError(1, _WinAPI_GetLastErrorMessage(), False)) : (DllStructGetData($CapabilitiesString, 1))
EndFunc   ;==>CapabilitiesRequestAndCapabilitiesReply

Func _RestoreMonitorFactoryDefaults(Const $h_monitor)
        DllCall($Dxva2, "bool", "RestoreMonitorFactoryDefaults", "ptr", $h_monitor)
        Return (@error) ? (SetError(1, _WinAPI_GetLastErrorMessage(), False)) : (True)
EndFunc   ;==>_RestoreMonitorFactoryDefaults

Func _SetVCPFeature(Const $h_monitor, Const $VCPCode, Const $NewValue)
        DllCall($Dxva2, "bool", "SetVCPFeature", _
                        "handle", $h_monitor, _
                        "byte", $VCPCode, _
                        "dword", $NewValue)

        Return (@error) ? (SetError(1, _WinAPI_GetLastErrorMessage(), False)) : (True)
EndFunc   ;==>_SetVCPFeature

Func _GetVCPFeatureAndVCPFeatureReply(Const $h_monitor, Const $VCPCode)
        Local Const $CurrentValue = DllStructCreate("dword_ptr")
        Local Const $MaximumValue = DllStructCreate("dword_ptr")
        Local Const $VCPCodeType = DllStructCreate("dword_ptr")
        DllCall($Dxva2, "bool", "GetVCPFeatureAndVCPFeatureReply", _
                        "handle", $h_monitor, _
                        "byte", $VCPCode, _
                        "ptr", DllStructGetPtr($VCPCodeType), _
                        "ptr", DllStructGetPtr($CurrentValue), _
                        "ptr", DllStructGetPtr($MaximumValue))
        If @error Then Return SetError(1, _WinAPI_GetLastErrorMessage(), False)
        Local Const $VCPFeatureReply[3] = [DllStructGetData($VCPCodeType, 1), DllStructGetData($CurrentValue, 1), DllStructGetData($MaximumValue, 1)]
        Return $VCPFeatureReply
EndFunc   ;==>_GetVCPFeatureAndVCPFeatureReply

Func Manage_vcp(Const $String)
        Local $Exp, $i, $tmp, $OUT_String
        $Exp = StringRegExp($String, "(?<=vcp\()([A-Za-z0-9]{2}[\)\(]?\s?)*(?=\))", 2)
        If @error Then Return
        $Exp = StringRegExp($Exp[0], "([0-9A-Fa-f]{2})(\(.+?\))?", 4)
        If @error Then Return
        Local $Array[UBound($Exp)][2]
        For $i = 0 To UBound($Exp) - 1
                $tmp = $Exp[$i]
                $Array[$i][0] = StringStripWS(StringLeft($tmp[0], 2), 8)
                $OUT_String = StringStripWS(StringTrimLeft($tmp[0], 2), 3)
                If $OUT_String <> "" Then
                        $OUT_String = StringTrimLeft(StringTrimRight($OUT_String, 1), 1)
                        $Array[$i][1] = $OUT_String
                EndIf
        Next
        Return $Array
EndFunc   ;==>Manage_vcp

Func _DestroyPhysicalMonitor(Const $h_monitor)
        DllCall($Dxva2, "bool", "DestroyPhysicalMonitor", "ptr", $h_monitor)
        Return (@error) ? (SetError(1, _WinAPI_GetLastErrorMessage(), False)) : (True)
EndFunc   ;==>_DestroyPhysicalMonitor

Func Brightness_lcd(Const $BRIGHTNESS_Lcd)
        Local $BRIGHTNESS_String, $tmp, $i
        For $i = 1 To BinaryLen($BRIGHTNESS_Lcd)
                $tmp = Int(BinaryMid($BRIGHTNESS_Lcd, $i, 1))
                If $i = 1 Then
                        $BRIGHTNESS_String &= $tmp & ";"
                Else
                        If $tmp = 0 Then ExitLoop
                        $BRIGHTNESS_String &= $tmp & ";"
                EndIf
        Next
        Return StringSplit(StringTrimRight($BRIGHTNESS_String, 1), ";", 2)
EndFunc   ;==>Brightness_lcd

Func _exit()
        If $hDevice Then _WinAPI_CloseHandle($hDevice)
        _DestroyPhysicalMonitor($hMonitor) ;关闭监视器句柄
        DllClose($Dxva2)
        Exit
EndFunc   ;==>_exit

未经允许不得转载:百脑问 » 显示器亮度,对比度,色温缩放调节,笔记本亮度调节AUTOIT源码

×

感谢您的支持,我们会一直保持!

扫码支持
请土豪扫码随意打赏

打开微信扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

分享到:更多 ()

评论 抢沙发

评论前必须登录!

百脑问 创新坚持 杰然不同

赞助我们联系我们
数据查询次数:18 消耗时间:0.531 秒, 使用内存: 21.00MB
您好,有什么可以帮您吗!