winapi change brightness

若如初见. 提交于 2019-12-21 20:23:34

问题


What winapi's are there to change the screen's brightness?

I've been attempting to look for an example or API I can use for Delphi but have not found anything.


回答1:


Starting with Windows Vista you can use the GetMonitorBrightness and SetMonitorBrightness functions.

function GetMonitorBrightness(
  hMonitor : THandle;
  var   pdwMinimumBrightness : DWORD;
  var   pdwCurrentBrightness : DWORD;
  var   pdwMaximumBrightness : DWORD
) : BOOL; stdcall ; external 'Dxva2.dll' name 'GetMonitorBrightness';


function SetMonitorBrightness(
  hMonitor : THandle;
  dwNewBrightness : DWORD
): BOOL; stdcall ; external 'Dxva2.dll' name 'SetMonitorBrightness';

Another option is use the WmiSetBrightness method of the WmiMonitorBrightnessMethods WMI Class.

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  Variants,
  ComObj;

procedure  SetBrightness(Timeout : Integer; Brightness : Byte);
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\WMI', '', '');
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM WmiMonitorBrightnessMethods Where Active=True','WQL',$00000020);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, iValue) = 0 do
  begin
    FWbemObject.WmiSetBrightness(Timeout, Brightness);
    FWbemObject:=Unassigned;
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      SetBrightness(5, 100);
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.

Note: These functions are supported if the GetMonitorCapabilities function returns the MC_CAPS_BRIGHTNESS flag.



来源:https://stackoverflow.com/questions/16027126/winapi-change-brightness

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!