找回密码  加入

AUTOIT CN

搜索
查看: 565|回复: 6

[系统综合] 求大神帮我转换 AHK 代码为 AU3

[复制链接]
发表于 2019-6-5 15:36:16 | 显示全部楼层 |阅读模式
下面的代码是用来调节笔记本显示屏亮度的,它有个好处是,调节亮度的时候,有OSD显示。求大神帮我转换成AU3代码,不胜感激!
用法:
brightness 3         ;在原有亮度基础上增加3档
brightness -2        ;在原有亮度基础上降低2档

  1. #singleinstance,force
  2. #NoTrayIcon

  3. value=%1%
  4. BS := new BrightnessSetter()
  5. BS.SetBrightness(value)

  6. Exitapp

  7. class BrightnessSetter {
  8.         ; qwerty12 - 27/05/17
  9.         ; https://github.com/qwerty12/AutoHotkeyScripts/tree/master/LaptopBrightnessSetter
  10.         static _WM_POWERBROADCAST := 0x218, _osdHwnd := 0, hPowrprofMod := DllCall("LoadLibrary", "Str", "powrprof.dll", "Ptr")

  11.         __New() {
  12.                 if (BrightnessSetter.IsOnAc(AC))
  13.                         this._AC := AC
  14.                 if ((this.pwrAcNotifyHandle := DllCall("RegisterPowerSettingNotification", "Ptr", A_ScriptHwnd, "Ptr", BrightnessSetter._GUID_ACDC_POWER_SOURCE(), "UInt", DEVICE_NOTIFY_WINDOW_HANDLE := 0x00000000, "Ptr"))) ; Sadly the callback passed to *PowerSettingRegister*Notification runs on a new threadl
  15.                         OnMessage(this._WM_POWERBROADCAST, ((this.pwrBroadcastFunc := ObjBindMethod(this, "_On_WM_POWERBROADCAST"))))
  16.         }

  17.         __Delete() {
  18.                 if (this.pwrAcNotifyHandle) {
  19.                         OnMessage(BrightnessSetter._WM_POWERBROADCAST, this.pwrBroadcastFunc, 0)
  20.                         ,DllCall("UnregisterPowerSettingNotification", "Ptr", this.pwrAcNotifyHandle)
  21.                         ,this.pwrAcNotifyHandle := 0
  22.                         ,this.pwrBroadcastFunc := ""
  23.                 }
  24.         }

  25.         SetBrightness(increment, jump := False, showOSD := True, autoDcOrAc := -1, ptrAnotherScheme := 0)
  26.         {
  27.                 static PowerGetActiveScheme := DllCall("GetProcAddress", "Ptr", BrightnessSetter.hPowrprofMod, "AStr", "PowerGetActiveScheme", "Ptr")
  28.                           ,PowerSetActiveScheme := DllCall("GetProcAddress", "Ptr", BrightnessSetter.hPowrprofMod, "AStr", "PowerSetActiveScheme", "Ptr")
  29.                           ,PowerWriteACValueIndex := DllCall("GetProcAddress", "Ptr", BrightnessSetter.hPowrprofMod, "AStr", "PowerWriteACValueIndex", "Ptr")
  30.                           ,PowerWriteDCValueIndex := DllCall("GetProcAddress", "Ptr", BrightnessSetter.hPowrprofMod, "AStr", "PowerWriteDCValueIndex", "Ptr")
  31.                           ,PowerApplySettingChanges := DllCall("GetProcAddress", "Ptr", BrightnessSetter.hPowrprofMod, "AStr", "PowerApplySettingChanges", "Ptr")

  32.                 if (increment == 0 && !jump) {
  33.                         if (showOSD)
  34.                                 BrightnessSetter._ShowBrightnessOSD()
  35.                         return
  36.                 }

  37.                 if (!ptrAnotherScheme ? DllCall(PowerGetActiveScheme, "Ptr", 0, "Ptr*", currSchemeGuid, "UInt") == 0 : DllCall("powrprof\PowerDuplicateScheme", "Ptr", 0, "Ptr", ptrAnotherScheme, "Ptr*", currSchemeGuid, "UInt") == 0) {
  38.                         if (autoDcOrAc == -1) {
  39.                                 if (this != BrightnessSetter) {
  40.                                         AC := this._AC
  41.                                 } else {
  42.                                         if (!BrightnessSetter.IsOnAc(AC)) {
  43.                                                 DllCall("LocalFree", "Ptr", currSchemeGuid, "Ptr")
  44.                                                 return
  45.                                         }
  46.                                 }
  47.                         } else {
  48.                                 AC := !!autoDcOrAc
  49.                         }

  50.                         currBrightness := 0
  51.                         if (jump || BrightnessSetter._GetCurrentBrightness(currSchemeGuid, AC, currBrightness)) {
  52.                                  maxBrightness := BrightnessSetter.GetMaxBrightness()
  53.                                 ,minBrightness := BrightnessSetter.GetMinBrightness()

  54.                                 if (jump || !((currBrightness == maxBrightness && increment > 0) || (currBrightness == minBrightness && increment < minBrightness))) {
  55.                                         if (currBrightness + increment > maxBrightness)
  56.                                                 increment := maxBrightness
  57.                                         else if (currBrightness + increment < minBrightness)
  58.                                                 increment := minBrightness
  59.                                         else
  60.                                                 increment += currBrightness

  61.                                         if (DllCall(AC ? PowerWriteACValueIndex : PowerWriteDCValueIndex, "Ptr", 0, "Ptr", currSchemeGuid, "Ptr", BrightnessSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt", increment, "UInt") == 0) {
  62.                                                 ; PowerApplySettingChanges is undocumented and exists only in Windows 8+. Since both the Power control panel and the brightness slider use this, we'll do the same, but fallback to PowerSetActiveScheme if on Windows 7 or something
  63.                                                 if (!PowerApplySettingChanges || DllCall(PowerApplySettingChanges, "Ptr", BrightnessSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt") != 0)
  64.                                                         DllCall(PowerSetActiveScheme, "Ptr", 0, "Ptr", currSchemeGuid, "UInt")
  65.                                         }
  66.                                 }

  67.                                 if (showOSD)
  68.                                         BrightnessSetter._ShowBrightnessOSD()
  69.                         }
  70.                         DllCall("LocalFree", "Ptr", currSchemeGuid, "Ptr")
  71.                 }
  72.         }

  73.         IsOnAc(ByRef acStatus)
  74.         {
  75.                 static SystemPowerStatus
  76.                 if (!VarSetCapacity(SystemPowerStatus))
  77.                         VarSetCapacity(SystemPowerStatus, 12)

  78.                 if (DllCall("GetSystemPowerStatus", "Ptr", &SystemPowerStatus)) {
  79.                         acStatus := NumGet(SystemPowerStatus, 0, "UChar") == 1
  80.                         return True
  81.                 }

  82.                 return False
  83.         }
  84.        
  85.         GetDefaultBrightnessIncrement()
  86.         {
  87.                 static ret := 10
  88.                 DllCall("powrprof\PowerReadValueIncrement", "Ptr", BrightnessSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt*", ret, "UInt")
  89.                 return ret
  90.         }

  91.         GetMinBrightness()
  92.         {
  93.                 static ret := -1
  94.                 if (ret == -1)
  95.                         if (DllCall("powrprof\PowerReadValueMin", "Ptr", BrightnessSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt*", ret, "UInt"))
  96.                                 ret := 0
  97.                 return ret
  98.         }

  99.         GetMaxBrightness()
  100.         {
  101.                 static ret := -1
  102.                 if (ret == -1)
  103.                         if (DllCall("powrprof\PowerReadValueMax", "Ptr", BrightnessSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt*", ret, "UInt"))
  104.                                 ret := 100
  105.                 return ret
  106.         }

  107.         _GetCurrentBrightness(schemeGuid, AC, ByRef currBrightness)
  108.         {
  109.                 static PowerReadACValueIndex := DllCall("GetProcAddress", "Ptr", BrightnessSetter.hPowrprofMod, "AStr", "PowerReadACValueIndex", "Ptr")
  110.                           ,PowerReadDCValueIndex := DllCall("GetProcAddress", "Ptr", BrightnessSetter.hPowrprofMod, "AStr", "PowerReadDCValueIndex", "Ptr")
  111.                 return DllCall(AC ? PowerReadACValueIndex : PowerReadDCValueIndex, "Ptr", 0, "Ptr", schemeGuid, "Ptr", BrightnessSetter._GUID_VIDEO_SUBGROUP(), "Ptr", BrightnessSetter._GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), "UInt*", currBrightness, "UInt") == 0
  112.         }
  113.        
  114.         _ShowBrightnessOSD()
  115.         {
  116.                 static PostMessagePtr := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandle", "Str", "user32.dll", "Ptr"), "AStr", A_IsUnicode ? "PostMessageW" : "PostMessageA", "Ptr")
  117.                           ,WM_SHELLHOOK := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK", "UInt")
  118.                 if A_OSVersion in WIN_VISTA,WIN_7
  119.                         return
  120.                 BrightnessSetter._RealiseOSDWindowIfNeeded()
  121.                 ; Thanks to YashMaster @ https://github.com/YashMaster/Tweaky/blob/master/Tweaky/BrightnessHandler.h for realising this could be done:
  122.                 if (BrightnessSetter._osdHwnd)
  123.                         DllCall(PostMessagePtr, "Ptr", BrightnessSetter._osdHwnd, "UInt", WM_SHELLHOOK, "Ptr", 0x37, "Ptr", 0)
  124.         }

  125.         _RealiseOSDWindowIfNeeded()
  126.         {
  127.                 static IsWindow := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandle", "Str", "user32.dll", "Ptr"), "AStr", "IsWindow", "Ptr")
  128.                 if (!DllCall(IsWindow, "Ptr", BrightnessSetter._osdHwnd) && !BrightnessSetter._FindAndSetOSDWindow()) {
  129.                         BrightnessSetter._osdHwnd := 0
  130.                         try if ((shellProvider := ComObjCreate("{C2F03A33-21F5-47FA-B4BB-156362A2F239}", "{00000000-0000-0000-C000-000000000046}"))) {
  131.                                 try if ((flyoutDisp := ComObjQuery(shellProvider, "{41f9d2fb-7834-4ab6-8b1b-73e74064b465}", "{41f9d2fb-7834-4ab6-8b1b-73e74064b465}"))) {
  132.                                          DllCall(NumGet(NumGet(flyoutDisp+0)+3*A_PtrSize), "Ptr", flyoutDisp, "Int", 0, "UInt", 0)
  133.                                         ,ObjRelease(flyoutDisp)
  134.                                 }
  135.                                 ObjRelease(shellProvider)
  136.                                 if (BrightnessSetter._FindAndSetOSDWindow())
  137.                                         return
  138.                         }
  139.                         ; who knows if the SID & IID above will work for future versions of Windows 10 (or Windows 8). Fall back to this if needs must
  140.                         Loop 2 {
  141.                                 SendEvent {Volume_Mute 2}
  142.                                 if (BrightnessSetter._FindAndSetOSDWindow())
  143.                                         return
  144.                                 Sleep 100
  145.                         }
  146.                 }
  147.         }
  148.        
  149.         _FindAndSetOSDWindow()
  150.         {
  151.                 static FindWindow := DllCall("GetProcAddress", "Ptr", DllCall("GetModuleHandle", "Str", "user32.dll", "Ptr"), "AStr", A_IsUnicode ? "FindWindowW" : "FindWindowA", "Ptr")
  152.                 return !!((BrightnessSetter._osdHwnd := DllCall(FindWindow, "Str", "NativeHWNDHost", "Str", "", "Ptr")))
  153.         }

  154.         _On_WM_POWERBROADCAST(wParam, lParam)
  155.         {
  156.                 ;OutputDebug % &this
  157.                 if (wParam == 0x8013 && lParam && NumGet(lParam+0, 0, "UInt") == NumGet(BrightnessSetter._GUID_ACDC_POWER_SOURCE()+0, 0, "UInt")) { ; PBT_POWERSETTINGCHANGE and a lazy comparison
  158.                         this._AC := NumGet(lParam+0, 20, "UChar") == 0
  159.                         return True
  160.                 }
  161.         }

  162.         _GUID_VIDEO_SUBGROUP()
  163.         {
  164.                 static GUID_VIDEO_SUBGROUP__
  165.                 if (!VarSetCapacity(GUID_VIDEO_SUBGROUP__)) {
  166.                          VarSetCapacity(GUID_VIDEO_SUBGROUP__, 16)
  167.                         ,NumPut(0x7516B95F, GUID_VIDEO_SUBGROUP__, 0, "UInt"), NumPut(0x4464F776, GUID_VIDEO_SUBGROUP__, 4, "UInt")
  168.                         ,NumPut(0x1606538C, GUID_VIDEO_SUBGROUP__, 8, "UInt"), NumPut(0x99CC407F, GUID_VIDEO_SUBGROUP__, 12, "UInt")
  169.                 }
  170.                 return &GUID_VIDEO_SUBGROUP__
  171.         }

  172.         _GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS()
  173.         {
  174.                 static GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__
  175.                 if (!VarSetCapacity(GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__)) {
  176.                          VarSetCapacity(GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 16)
  177.                         ,NumPut(0xADED5E82, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 0, "UInt"), NumPut(0x4619B909, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 4, "UInt")
  178.                         ,NumPut(0xD7F54999, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 8, "UInt"), NumPut(0xCB0BAC1D, GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__, 12, "UInt")
  179.                 }
  180.                 return &GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS__
  181.         }

  182.         _GUID_ACDC_POWER_SOURCE()
  183.         {
  184.                 static GUID_ACDC_POWER_SOURCE_
  185.                 if (!VarSetCapacity(GUID_ACDC_POWER_SOURCE_)) {
  186.                          VarSetCapacity(GUID_ACDC_POWER_SOURCE_, 16)
  187.                         ,NumPut(0x5D3E9A59, GUID_ACDC_POWER_SOURCE_, 0, "UInt"), NumPut(0x4B00E9D5, GUID_ACDC_POWER_SOURCE_, 4, "UInt")
  188.                         ,NumPut(0x34FFBDA6, GUID_ACDC_POWER_SOURCE_, 8, "UInt"), NumPut(0x486551FF, GUID_ACDC_POWER_SOURCE_, 12, "UInt")
  189.                 }
  190.                 return &GUID_ACDC_POWER_SOURCE_
  191.         }

  192. }

  193. BrightnessSetter_new() {
  194.         return new BrightnessSetter()
  195. }

复制代码





发表于 2019-7-22 12:12:11 | 显示全部楼层
sanmaodo 发表于 2019-7-21 20:26
明天测试一下,再发上来,谢谢老大了!

通过翻译这个代码,感觉ahk发展比au3快,支持自定义类,有指针操作符 & *,而且com接口与api的调用支持使用指针,虽然au3也支持api的指针调用,但是官方说明是试用命令。
au3近几年的更新,多数是一些修补bug的更新,udf更新,至于au3自身功能的更新几乎是停滞不前。
  1. #NoTrayIcon
  2. #RequireAdmin
  3. #include <WinAPISys.au3>
  4. #include <Array.au3>
  5. Opt('GUIOnEventMode', 1)
  6. Opt('MustDeclareVars', 1)
  7. _GUID_VIDEO_SUBGROUP()
  8. Global Const $GUID_VIDEO_SUBGROUP = '{7516B95F-F776-4464-8C53-06167F40CC99}'
  9. Global Const $GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS = '{ADED5E82-B909-4619-9949-F5D71DAC0BCB}'
  10. Global Const $GUID_ACDC_POWER_SOURCE = '{5D3E9A59-E9D5-4B00-A6BD-FF34FF516548}'
  11. Global Const $WM_POWERBROADCAST = 0x218
  12. Global $hPowrprofMod = _WinAPI_LoadLibrary('powrprof.dll') ;
  13. Global $_AC, $pwrAcNotifyHandle, $_osdHwnd, $currSchemeGuid, $currBrightness, $maxBrightness, $minBrightness

  14. Global $MainForm = GUICreate('', 400, 200)
  15. Global $iLight = GUICtrlCreateSlider(5, 50, 390, 30)
  16. GUISetState()
  17. GUISetOnEvent(-3, '_Exit')
  18. GUICtrlSetOnEvent($iLight, '_SetLight')
  19. __New()
  20. While 1
  21.         Sleep(50)
  22. WEnd
  23. Func _SetLight()
  24.         SetBrightness(GUICtrlRead($iLight))
  25. EndFunc   ;==>_SetLight
  26. Func _Exit()
  27.         Exit
  28. EndFunc   ;==>_Exit
  29. Func __New()
  30.         Local $AC
  31.         If IsOnAc($AC) Then $_AC = $AC
  32.         $pwrAcNotifyHandle = _WinAPI_RegisterPowerSettingNotification($MainForm, $GUID_ACDC_POWER_SOURCE)
  33.         If $pwrAcNotifyHandle Then GUIRegisterMsg($WM_POWERBROADCAST, '_On_WM_POWERBROADCAST')
  34. EndFunc   ;==>__New
  35. Func _On_WM_POWERBROADCAST($hwnd, $msg, $wparam, $lparam)
  36.         #forceref $hwnd, $msg, $wparam, $lparam
  37.         Local Const $PBT_POWERSETTINGCHANGE = 0x8013 ;//已收到电源设置更改事件
  38.         Local Const $POWERBROADCAST_SETTING = "ulong Data1;ushort Data2;ushort Data3;byte Data4[8];dword DataLength;byte Data"
  39.         Switch $msg
  40.                 Case $WM_POWERBROADCAST
  41.                         Switch $wparam
  42.                                 Case $PBT_POWERSETTINGCHANGE
  43.                                         Local $PBT_PSC = DllStructCreate($POWERBROADCAST_SETTING, $lparam)
  44.                                         If DllStructGetData($PBT_PSC, 1) = DllStructGetData(_GUID_ACDC_POWER_SOURCE(), 1) Then ;//系统电源已更改
  45.                                                 $_AC = (DllStructGetData($PBT_PSC, 'Data') = 0)
  46.                                         EndIf
  47.                         EndSwitch
  48.         EndSwitch
  49.         Return $_AC
  50. EndFunc   ;==>_On_WM_POWERBROADCAST
  51. Func _GUID_VIDEO_SUBGROUP()
  52.         Return _WinAPI_GUIDFromString($GUID_VIDEO_SUBGROUP)
  53. EndFunc   ;==>_GUID_VIDEO_SUBGROUP
  54. Func _GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS()
  55.         Return _WinAPI_GUIDFromString($GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS)
  56. EndFunc   ;==>_GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS
  57. Func _GUID_ACDC_POWER_SOURCE()
  58.         Return _WinAPI_GUIDFromString($GUID_ACDC_POWER_SOURCE)
  59. EndFunc   ;==>_GUID_ACDC_POWER_SOURCE
  60. Func __Delete()
  61.         If $pwrAcNotifyHandle Then _WinAPI_UnregisterPowerSettingNotification($pwrAcNotifyHandle)
  62.         $pwrAcNotifyHandle = 0
  63. EndFunc   ;==>__Delete
  64. Func SetBrightness($increment, $jump = False, $showOSD = True, $autoDcOrAc = -1, $ptrAnotherScheme = 0)
  65.         Static $PowerApplySettingChanges = _WinAPI_GetProcAddress($hPowrprofMod, 'PowerApplySettingChanges')
  66.         ConsoleWrite('$increment===' & $increment & @CRLF)
  67.         If $increment = 0 And Not $jump Then
  68.                 If $showOSD Then _ShowBrightnessOSD()
  69.                 Return
  70.         EndIf
  71.         Local $ActiveScheme = False, $aResult
  72.         If Not $ptrAnotherScheme Then
  73.                 If PowerGetActiveScheme(0, $currSchemeGuid) = 0 Then $ActiveScheme = True
  74.         Else
  75.                 If PowerDuplicateScheme(Null, $ptrAnotherScheme, $currSchemeGuid) = 0 Then $ActiveScheme = True
  76.         EndIf
  77.         Local $AC = $_AC
  78.         If $ActiveScheme Then
  79.                 If ($autoDcOrAc == -1) Then ;~
  80.                         If Not IsOnAc($AC) Then Return _WinAPI_LocalFree($currSchemeGuid)
  81.                 Else
  82.                         $AC = Not (Not $autoDcOrAc) ;此处应该是类型转化
  83.                 EndIf
  84.                 If $jump Or _GetCurrentBrightness($currSchemeGuid, $AC, $currBrightness) Then
  85.                         $maxBrightness = GetMaxBrightness()
  86.                         $minBrightness = GetMinBrightness()
  87.                         If ($jump Or Not (($currBrightness = $maxBrightness And $increment > 0) Or ($currBrightness = $minBrightness And $increment < $minBrightness))) Then
  88.                                 If (($currBrightness + $increment) > $maxBrightness) Then
  89.                                         $increment = $maxBrightness
  90.                                 ElseIf (($currBrightness + $increment) < $minBrightness) Then
  91.                                         $increment = $minBrightness
  92.                                 Else
  93.                                         $increment += $currBrightness
  94.                                 EndIf
  95.                                 If $AC Then
  96.                                         $aResult = PowerWriteACValueIndex(0, $currSchemeGuid, _GUID_VIDEO_SUBGROUP(), _GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), $increment)
  97.                                 Else
  98.                                         $aResult = PowerWriteDCValueIndex(0, $currSchemeGuid, _GUID_VIDEO_SUBGROUP(), _GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), $increment)
  99.                                 EndIf
  100.                                 If $aResult = 0 And $PowerApplySettingChanges <> 0 Then ; PowerApplySettingChanges is undocumented and exists only in Windows 8+. Since both the Power control panel and the brightness slider use this, we'll do the same, but fallback to PowerSetActiveScheme if on Windows 7 or something
  101.                                         $aResult = DllCallAddress('UInt', $PowerApplySettingChanges, "Struct*", _GUID_VIDEO_SUBGROUP(), "Struct*", _GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS())
  102.                                         If Not $PowerApplySettingChanges Or $aResult[0] <> 0 Then PowerSetActiveScheme(0, $currSchemeGuid)
  103.                                 EndIf
  104.                         EndIf
  105.                         If ($showOSD) Then _ShowBrightnessOSD()
  106.                 EndIf
  107.                 _WinAPI_LocalFree($currSchemeGuid)
  108.         EndIf
  109. EndFunc   ;==>SetBrightness
  110. Func IsOnAc(ByRef $acStatus)
  111.         Local $tSYSTEM_POWER_STATUS
  112.         If GetSystemPowerStatus($tSYSTEM_POWER_STATUS) Then
  113.                 $acStatus = $tSYSTEM_POWER_STATUS.ACLineStatus
  114.                 Return True
  115.         EndIf
  116.         Return False
  117. EndFunc   ;==>IsOnAc
  118. Func GetDefaultBrightnessIncrement()
  119.         Static $Ret = 10
  120.         PowerReadValueIncrement(Null, _GUID_VIDEO_SUBGROUP(), _GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), $Ret)
  121.         Return $Ret
  122. EndFunc   ;==>GetDefaultBrightnessIncrement
  123. Func GetMinBrightness()
  124.         Static $Ret = -1
  125.         If $Ret = -1 Then
  126.                 If (PowerReadValueMin(Null, _GUID_VIDEO_SUBGROUP(), _GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), $Ret)) Then $Ret = 0
  127.         EndIf
  128.         Return $Ret
  129. EndFunc   ;==>GetMinBrightness
  130. Func GetMaxBrightness()
  131.         Static $Ret = -1
  132.         If $Ret = -1 Then
  133.                 If PowerReadValueMax(Null, _GUID_VIDEO_SUBGROUP(), _GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), $Ret) Then $Ret = 100
  134.         EndIf
  135.         Return $Ret
  136. EndFunc   ;==>GetMaxBrightness
  137. Func _GetCurrentBrightness($SchemeGuid, $AC, ByRef $currBrightness)
  138.         Local $Ret
  139.         If $AC Then
  140.                 $Ret = PowerReadACValueIndex(0, $SchemeGuid, _GUID_VIDEO_SUBGROUP(), _GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), $currBrightness)
  141.         Else
  142.                 $Ret = PowerReadDCValueIndex(0, $SchemeGuid, _GUID_VIDEO_SUBGROUP(), _GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS(), $currBrightness)
  143.         EndIf
  144.         Return $Ret
  145. EndFunc   ;==>_GetCurrentBrightness
  146. Func _ShowBrightnessOSD()
  147. ;~         Global $HSHELL_MONITORCHANGED = 16
  148. ;~         Global $HSHELL_HIGHBIT = 0x8000
  149.         Static $WM_SHELLHOOK = _WinAPI_RegisterWindowMessage('SHELLHOOK')
  150.         If @OSVersion = 'WIN_VISTA' Or @OSVersion = 'WIN_7' Then Return
  151.         _RealiseOSDWindowIfNeeded() ; Thanks to YashMaster [url=home.php?mod=space&uid=7654264]@[/url] https://github.com/YashMaster/Tweaky/blob/master/Tweaky/BrightnessHandler.h for realising this could be done:
  152.         If $_osdHwnd Then _WinAPI_PostMessage($_osdHwnd, $WM_SHELLHOOK, 0x37, 0)
  153. EndFunc   ;==>_ShowBrightnessOSD
  154. Func _RealiseOSDWindowIfNeeded() ;//此处调用的com接口没查到是什么东西,只能猜着构造一个类型结构。
  155.         Local $shellProvider, $flyoutDisp
  156.         If (Not _WinAPI_IsWindow($_osdHwnd) And Not _FindAndSetOSDWindow()) Then
  157.                 $_osdHwnd = 0
  158.                 $shellProvider = ObjCreateInterface("{C2F03A33-21F5-47FA-B4BB-156362A2F239}", "{00000000-0000-0000-C000-000000000046}")
  159.                 If IsObj($shellProvider) Then
  160.                         Local $pObj = Ptr(0)
  161.                         $shellProvider.QueryInterface("{41f9d2fb-7834-4ab6-8b1b-73e74064b465}", $pObj)
  162.                         $flyoutDisp = ObjCreateInterface($pObj, "{41f9d2fb-7834-4ab6-8b1b-73e74064b465}", "WhatFuncName1 int();WhatFuncName2 int();WhatFuncName3 int();flyoutDisp UInt(Ptr;INT;UInt);")
  163.                         $flyoutDisp.flyoutDisp($flyoutDisp.WhatFuncName1, 0, 0)
  164.                         $flyoutDisp = Null
  165.                 EndIf
  166.                 $shellProvider = Null
  167.                 If _FindAndSetOSDWindow() Then Return
  168.         EndIf
  169.         ; who knows if the SID & IID above will work for future versions of Windows 10 (or Windows 8). Fall back to this if needs must
  170. ;~                         Loop 2 {
  171. ;~                                 SendEvent {Volume_Mute 2}
  172. ;~                                 if (BrightnessSetter._FindAndSetOSDWindow())
  173. ;~                                         return
  174. ;~                                 Sleep 100
  175. ;~                         }
  176. ;~ EndIf
  177. EndFunc   ;==>_RealiseOSDWindowIfNeeded
  178. Func _FindAndSetOSDWindow()
  179.         $_osdHwnd = _WinAPI_FindWindow('NativeHWNDHost', '')
  180.         Return Not (Not $_osdHwnd)
  181. EndFunc   ;==>_FindAndSetOSDWindow
  182. ;=========================================================================================
  183. Func PowerGetActiveScheme($UserRootPowerKey, ByRef $ActivePolicyGuid)
  184.         $ActivePolicyGuid = DllStructCreate($tagGUID) ;
  185.         Local $Ret = DllCall('powrprof.dll', 'DWORD', 'PowerGetActiveScheme', 'PTR', $UserRootPowerKey, 'Struct*', $ActivePolicyGuid)
  186.         If Not @error Then Return $Ret[0]
  187. EndFunc   ;==>PowerGetActiveScheme
  188. Func PowerSetActiveScheme($UserRootPowerKey, $ActivePolicyGuid)
  189.         Local $Ret = DllCall('powrprof.dll', 'DWORD', 'PowerSetActiveScheme', 'PTR', Null, 'struct*', $ActivePolicyGuid)
  190.         If Not @error Then Return $Ret[0]
  191. EndFunc   ;==>PowerSetActiveScheme
  192. Func PowerWriteACValueIndex($UserRootPowerKey, $SchemeGuid, $SubGroupOfPowerSettingsGuid, $PowerSettingGuid, $AcValueIndex)
  193.         Local $Ret = DllCall('powrprof.dll', 'DWORD', 'PowerWriteACValueIndex', 'PTR', $UserRootPowerKey, 'struct*', $SchemeGuid, 'struct*', $SubGroupOfPowerSettingsGuid, 'struct*', $PowerSettingGuid, 'DWORD', $AcValueIndex)
  194.         If Not @error Then Return $Ret[0]
  195. EndFunc   ;==>PowerWriteACValueIndex
  196. Func PowerWriteDCValueIndex($UserRootPowerKey, $SchemeGuid, $SubGroupOfPowerSettingsGuid, $PowerSettingGuid, $DcValueIndex)
  197.         Local $Ret = DllCall('powrprof.dll', 'DWORD', 'PowerWriteDCValueIndex', 'PTR', $UserRootPowerKey, 'struct*', $SchemeGuid, 'struct*', $SubGroupOfPowerSettingsGuid, 'struct*', $PowerSettingGuid, 'DWORD', $DcValueIndex)
  198.         If Not @error Then Return $Ret[0]
  199. EndFunc   ;==>PowerWriteDCValueIndex
  200. Func PowerDuplicateScheme($RootPowerKey, $SourceSchemeGuid, $DestinationSchemeGuid)
  201.         Local $Ret = DllCall('powrprof.dll', 'DWORD', 'PowerDuplicateScheme', 'PTR', $RootPowerKey, 'struct*', $SourceSchemeGuid, 'struct*', $DestinationSchemeGuid)
  202.         If Not @error Then Return $Ret[0]
  203. EndFunc   ;==>PowerDuplicateScheme
  204. Func PowerReadValueIncrement($RootPowerKey, $SubGroupOfPowerSettingsGuid, $PowerSettingGuid, ByRef $ValueIncrement)
  205.         Local $Ret = DllCall('powrprof.dll', 'DWORD', 'PowerReadValueIncrement', 'PTR', $RootPowerKey, 'struct*', $SubGroupOfPowerSettingsGuid, 'struct*', $PowerSettingGuid, 'DWORD*', $ValueIncrement)
  206.         $ValueIncrement = $Ret[4]
  207.         If Not @error Then Return $Ret[0]
  208. EndFunc   ;==>PowerReadValueIncrement
  209. Func PowerReadACValueIndex($RootPowerKey, $SchemeGuid, $SubGroupOfPowerSettingsGuid, $PowerSettingGuid, ByRef $AcValueIndex)
  210.         Local $Ret = DllCall('powrprof.dll', 'DWORD', 'PowerReadACValueIndex', 'PTR', $RootPowerKey, 'struct*', $SchemeGuid, 'struct*', $SubGroupOfPowerSettingsGuid, 'struct*', $PowerSettingGuid, 'DWORD*', $AcValueIndex)
  211.         $AcValueIndex = $Ret[5]
  212.         If Not @error Then Return $Ret[0]
  213. EndFunc   ;==>PowerReadACValueIndex
  214. Func PowerReadDCValueIndex($RootPowerKey, $SchemeGuid, $SubGroupOfPowerSettingsGuid, $PowerSettingGuid, ByRef $DcValueIndex)
  215.         Local $Ret = DllCall('powrprof.dll', 'DWORD', 'PowerReadDCValueIndex', 'PTR', $RootPowerKey, 'struct*', $SchemeGuid, 'struct*', $SubGroupOfPowerSettingsGuid, 'struct*', $PowerSettingGuid, 'DWORD*', $DcValueIndex)
  216.         $DcValueIndex = $Ret[5]
  217.         If Not @error Then Return $Ret[0]
  218. EndFunc   ;==>PowerReadDCValueIndex
  219. Func PowerReadValueMax($RootPowerKey, $SubGroupOfPowerSettingsGuid, $PowerSettingGuid, ByRef $ValueMaximum)
  220.         Local $Ret = DllCall('powrprof.dll', 'DWORD', 'PowerReadValueMax', 'PTR', $RootPowerKey, 'ptr', DllCallbackGetPtr($SubGroupOfPowerSettingsGuid), 'ptr', DllCallbackGetPtr($PowerSettingGuid), 'DWORD*', $ValueMaximum)
  221.         $ValueMaximum = $Ret[4]
  222.         If Not @error Then Return $Ret[0]
  223. EndFunc   ;==>PowerReadValueMax
  224. Func PowerReadValueMin($RootPowerKey, $SubGroupOfPowerSettingsGuid, $PowerSettingGuid, $ValueMinimum)
  225.         Local $Ret = DllCall('powrprof.dll', 'DWORD', 'PowerReadValueMin', 'PTR', $RootPowerKey, 'struct*', $SubGroupOfPowerSettingsGuid, 'struct*', $PowerSettingGuid, 'DWORD*', $ValueMinimum)
  226.         If Not @error Then Return $Ret[0]
  227. EndFunc   ;==>PowerReadValueMin
  228. Func GetSystemPowerStatus(ByRef $SystemPowerStatus)
  229.         Local $SYSTEM_POWER_STATUS = 'BYTE ACLineStatus;BYTE BatteryFlag;BYTE BatteryLifePercent;BYTE SystemStatusFlag;DWORD BatteryLifeTime;DWORD BatteryFullLifeTime'
  230.         $SystemPowerStatus = DllStructCreate($SYSTEM_POWER_STATUS)
  231.         Local $aRet = DllCall('kernel32.dll', 'BOOL', 'GetSystemPowerStatus', 'Struct*', $SystemPowerStatus)
  232.         If Not @error Then Return $aRet[0]
  233. EndFunc   ;==>GetSystemPowerStatus
复制代码


回复 支持 1 反对 0

使用道具 举报

发表于 2019-7-8 13:48:27 | 显示全部楼层
AHK支持类啊,看来au3发展确实太慢了。。。
这么长的代码,你赞助一下论坛,我给你翻译一下~
 楼主| 发表于 2019-7-21 01:31:45 | 显示全部楼层
本帖最后由 sanmaodo 于 2019-7-21 01:35 编辑
haijie1223 发表于 2019-7-8 13:48
AHK支持类啊,看来au3发展确实太慢了。。。
这么长的代码,你赞助一下论坛,我给你翻译一下~

谢谢!已赞助


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?加入

x
发表于 2019-7-21 09:29:21 | 显示全部楼层

谢谢!这个脚本是完整的吗,我运行后没有效果呢。
 楼主| 发表于 2019-7-21 20:26:28 | 显示全部楼层
haijie1223 发表于 2019-7-21 09:29
谢谢!这个脚本是完整的吗,我运行后没有效果呢。

明天测试一下,再发上来,谢谢老大了!
发表于 2019-7-26 00:14:46 | 显示全部楼层
不错支持一下
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|小黑屋|手机版|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2019-9-18 14:03 , Processed in 0.078119 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表