Win32 API 日本語リファレンス
ホームDevices.Display › SetMonitorBrightness

SetMonitorBrightness

関数
モニタの輝度を新しい値に設定する。
DLLdxva2.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

// dxva2.dll
#include <windows.h>

INT SetMonitorBrightness(
    HANDLE hMonitor,
    DWORD dwNewBrightness
);

パラメーター

名前方向説明
hMonitorHANDLEin物理モニターへのハンドル。モニターハンドルを取得するには、GetPhysicalMonitorsFromHMONITOR または GetPhysicalMonitorsFromIDirect3DDevice9 を呼び出します。
dwNewBrightnessDWORDin輝度値。モニターの最小および最大の輝度値を取得するには、GetMonitorBrightness を呼び出します。

戻り値の型: INT

公式ドキュメント

モニターの輝度値を設定します。

戻り値

関数が成功した場合、戻り値は TRUE です。関数が失敗した場合、戻り値は FALSE です。拡張エラー情報を取得するには、GetLastError を呼び出します。

解説(Remarks)

この関数がサポートされている場合、GetMonitorCapabilities 関数は MC_CAPS_BRIGHTNESS フラグを返します。

この関数が戻るまでには約 50 ミリ秒かかります。

輝度設定は連続的なモニター設定です。詳細については、高レベルモニター構成関数の使用を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// dxva2.dll
#include <windows.h>

INT SetMonitorBrightness(
    HANDLE hMonitor,
    DWORD dwNewBrightness
);
[DllImport("dxva2.dll", SetLastError = true, ExactSpelling = true)]
static extern int SetMonitorBrightness(
    IntPtr hMonitor,   // HANDLE
    uint dwNewBrightness   // DWORD
);
<DllImport("dxva2.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetMonitorBrightness(
    hMonitor As IntPtr,   ' HANDLE
    dwNewBrightness As UInteger   ' DWORD
) As Integer
End Function
' hMonitor : HANDLE
' dwNewBrightness : DWORD
Declare PtrSafe Function SetMonitorBrightness Lib "dxva2" ( _
    ByVal hMonitor As LongPtr, _
    ByVal dwNewBrightness As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetMonitorBrightness = ctypes.windll.dxva2.SetMonitorBrightness
SetMonitorBrightness.restype = ctypes.c_int
SetMonitorBrightness.argtypes = [
    wintypes.HANDLE,  # hMonitor : HANDLE
    wintypes.DWORD,  # dwNewBrightness : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('dxva2.dll')
SetMonitorBrightness = Fiddle::Function.new(
  lib['SetMonitorBrightness'],
  [
    Fiddle::TYPE_VOIDP,  # hMonitor : HANDLE
    -Fiddle::TYPE_INT,  # dwNewBrightness : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "dxva2")]
extern "system" {
    fn SetMonitorBrightness(
        hMonitor: *mut core::ffi::c_void,  // HANDLE
        dwNewBrightness: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("dxva2.dll", SetLastError = true)]
public static extern int SetMonitorBrightness(IntPtr hMonitor, uint dwNewBrightness);
"@
$api = Add-Type -MemberDefinition $sig -Name 'dxva2_SetMonitorBrightness' -Namespace Win32 -PassThru
# $api::SetMonitorBrightness(hMonitor, dwNewBrightness)
#uselib "dxva2.dll"
#func global SetMonitorBrightness "SetMonitorBrightness" sptr, sptr
; SetMonitorBrightness hMonitor, dwNewBrightness   ; 戻り値は stat
; hMonitor : HANDLE -> "sptr"
; dwNewBrightness : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "dxva2.dll"
#cfunc global SetMonitorBrightness "SetMonitorBrightness" sptr, int
; res = SetMonitorBrightness(hMonitor, dwNewBrightness)
; hMonitor : HANDLE -> "sptr"
; dwNewBrightness : DWORD -> "int"
; INT SetMonitorBrightness(HANDLE hMonitor, DWORD dwNewBrightness)
#uselib "dxva2.dll"
#cfunc global SetMonitorBrightness "SetMonitorBrightness" intptr, int
; res = SetMonitorBrightness(hMonitor, dwNewBrightness)
; hMonitor : HANDLE -> "intptr"
; dwNewBrightness : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	dxva2 = windows.NewLazySystemDLL("dxva2.dll")
	procSetMonitorBrightness = dxva2.NewProc("SetMonitorBrightness")
)

// hMonitor (HANDLE), dwNewBrightness (DWORD)
r1, _, err := procSetMonitorBrightness.Call(
	uintptr(hMonitor),
	uintptr(dwNewBrightness),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function SetMonitorBrightness(
  hMonitor: THandle;   // HANDLE
  dwNewBrightness: DWORD   // DWORD
): Integer; stdcall;
  external 'dxva2.dll' name 'SetMonitorBrightness';
result := DllCall("dxva2\SetMonitorBrightness"
    , "Ptr", hMonitor   ; HANDLE
    , "UInt", dwNewBrightness   ; DWORD
    , "Int")   ; return: INT
●SetMonitorBrightness(hMonitor, dwNewBrightness) = DLL("dxva2.dll", "int SetMonitorBrightness(void*, dword)")
# 呼び出し: SetMonitorBrightness(hMonitor, dwNewBrightness)
# hMonitor : HANDLE -> "void*"
# dwNewBrightness : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "dxva2" fn SetMonitorBrightness(
    hMonitor: ?*anyopaque, // HANDLE
    dwNewBrightness: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc SetMonitorBrightness(
    hMonitor: pointer,  # HANDLE
    dwNewBrightness: uint32  # DWORD
): int32 {.importc: "SetMonitorBrightness", stdcall, dynlib: "dxva2.dll".}
pragma(lib, "dxva2");
extern(Windows)
int SetMonitorBrightness(
    void* hMonitor,   // HANDLE
    uint dwNewBrightness   // DWORD
);
ccall((:SetMonitorBrightness, "dxva2.dll"), stdcall, Int32,
      (Ptr{Cvoid}, UInt32),
      hMonitor, dwNewBrightness)
# hMonitor : HANDLE -> Ptr{Cvoid}
# dwNewBrightness : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t SetMonitorBrightness(
    void* hMonitor,
    uint32_t dwNewBrightness);
]]
local dxva2 = ffi.load("dxva2")
-- dxva2.SetMonitorBrightness(hMonitor, dwNewBrightness)
-- hMonitor : HANDLE
-- dwNewBrightness : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('dxva2.dll');
const SetMonitorBrightness = lib.func('__stdcall', 'SetMonitorBrightness', 'int32_t', ['void *', 'uint32_t']);
// SetMonitorBrightness(hMonitor, dwNewBrightness)
// hMonitor : HANDLE -> 'void *'
// dwNewBrightness : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("dxva2.dll", {
  SetMonitorBrightness: { parameters: ["pointer", "u32"], result: "i32" },
});
// lib.symbols.SetMonitorBrightness(hMonitor, dwNewBrightness)
// hMonitor : HANDLE -> "pointer"
// dwNewBrightness : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SetMonitorBrightness(
    void* hMonitor,
    uint32_t dwNewBrightness);
C, "dxva2.dll");
// $ffi->SetMonitorBrightness(hMonitor, dwNewBrightness);
// hMonitor : HANDLE
// dwNewBrightness : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Dxva2 extends StdCallLibrary {
    Dxva2 INSTANCE = Native.load("dxva2", Dxva2.class);
    int SetMonitorBrightness(
        Pointer hMonitor,   // HANDLE
        int dwNewBrightness   // DWORD
    );
}
@[Link("dxva2")]
lib Libdxva2
  fun SetMonitorBrightness = SetMonitorBrightness(
    hMonitor : Void*,   # HANDLE
    dwNewBrightness : UInt32   # DWORD
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SetMonitorBrightnessNative = Int32 Function(Pointer<Void>, Uint32);
typedef SetMonitorBrightnessDart = int Function(Pointer<Void>, int);
final SetMonitorBrightness = DynamicLibrary.open('dxva2.dll')
    .lookupFunction<SetMonitorBrightnessNative, SetMonitorBrightnessDart>('SetMonitorBrightness');
// hMonitor : HANDLE -> Pointer<Void>
// dwNewBrightness : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetMonitorBrightness(
  hMonitor: THandle;   // HANDLE
  dwNewBrightness: DWORD   // DWORD
): Integer; stdcall;
  external 'dxva2.dll' name 'SetMonitorBrightness';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetMonitorBrightness"
  c_SetMonitorBrightness :: Ptr () -> Word32 -> IO Int32
-- hMonitor : HANDLE -> Ptr ()
-- dwNewBrightness : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setmonitorbrightness =
  foreign "SetMonitorBrightness"
    ((ptr void) @-> uint32_t @-> returning int32_t)
(* hMonitor : HANDLE -> (ptr void) *)
(* dwNewBrightness : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library dxva2 (t "dxva2.dll"))
(cffi:use-foreign-library dxva2)

(cffi:defcfun ("SetMonitorBrightness" set-monitor-brightness :convention :stdcall) :int32
  (h-monitor :pointer)   ; HANDLE
  (dw-new-brightness :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetMonitorBrightness = Win32::API::More->new('dxva2',
    'int SetMonitorBrightness(HANDLE hMonitor, DWORD dwNewBrightness)');
# my $ret = $SetMonitorBrightness->Call($hMonitor, $dwNewBrightness);
# hMonitor : HANDLE -> HANDLE
# dwNewBrightness : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。