2013-04-16 105 views
2

有哪些winapi改變屏幕的亮度?winapi改變亮度

enter image description here

我一直在試圖尋找一個例子或API我可以使用Delphi的,但沒有發現任何東西。

回答

8

從Windows Vista開始,您可以使用GetMonitorBrightnessSetMonitorBrightness函數。

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'; 

另一種選擇是使用WmiMonitorBrightnessMethods WMI類的WmiSetBrightness方法。

{$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. 

注意:如果GetMonitorCapabilities函數返回MC_CAPS_BRIGHTNESS標誌,這些功能都支持。

+0

+1獲得詳細的答案! – Remko

+0

謝謝!那就是我一直在尋找的東西。 – user0