Win32 API 日本語リファレンス
ホームUI.HiDpi › GetDpiForMonitor

GetDpiForMonitor

関数
指定モニターのDPI値(X・Y)を取得する。
DLLapi-ms-win-shcore-scaling-l1-1-1.dll呼出規約winapi対応OSWindows 8.1 以降

シグネチャ

// api-ms-win-shcore-scaling-l1-1-1.dll
#include <windows.h>

HRESULT GetDpiForMonitor(
    HMONITOR hmonitor,
    MONITOR_DPI_TYPE dpiType,
    DWORD* dpiX,
    DWORD* dpiY
);

パラメーター

名前方向説明
hmonitorHMONITORinDPIを取得する対象モニターのハンドル。
dpiTypeMONITOR_DPI_TYPEin取得するDPIの種類を示すMONITOR_DPI_TYPE(有効/角度/生)。
dpiXDWORD*out水平方向のDPI値を受け取るDWORD出力ポインタ。
dpiYDWORD*out垂直方向のDPI値を受け取るDWORD出力ポインタ。

戻り値の型: HRESULT

各言語での呼び出し定義

// api-ms-win-shcore-scaling-l1-1-1.dll
#include <windows.h>

HRESULT GetDpiForMonitor(
    HMONITOR hmonitor,
    MONITOR_DPI_TYPE dpiType,
    DWORD* dpiX,
    DWORD* dpiY
);
[DllImport("api-ms-win-shcore-scaling-l1-1-1.dll", ExactSpelling = true)]
static extern int GetDpiForMonitor(
    IntPtr hmonitor,   // HMONITOR
    int dpiType,   // MONITOR_DPI_TYPE
    out uint dpiX,   // DWORD* out
    out uint dpiY   // DWORD* out
);
<DllImport("api-ms-win-shcore-scaling-l1-1-1.dll", ExactSpelling:=True)>
Public Shared Function GetDpiForMonitor(
    hmonitor As IntPtr,   ' HMONITOR
    dpiType As Integer,   ' MONITOR_DPI_TYPE
    <Out> ByRef dpiX As UInteger,   ' DWORD* out
    <Out> ByRef dpiY As UInteger   ' DWORD* out
) As Integer
End Function
' hmonitor : HMONITOR
' dpiType : MONITOR_DPI_TYPE
' dpiX : DWORD* out
' dpiY : DWORD* out
Declare PtrSafe Function GetDpiForMonitor Lib "api-ms-win-shcore-scaling-l1-1-1" ( _
    ByVal hmonitor As LongPtr, _
    ByVal dpiType As Long, _
    ByRef dpiX As Long, _
    ByRef dpiY As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetDpiForMonitor = ctypes.windll.LoadLibrary("api-ms-win-shcore-scaling-l1-1-1.dll").GetDpiForMonitor
GetDpiForMonitor.restype = ctypes.c_int
GetDpiForMonitor.argtypes = [
    wintypes.HANDLE,  # hmonitor : HMONITOR
    ctypes.c_int,  # dpiType : MONITOR_DPI_TYPE
    ctypes.POINTER(wintypes.DWORD),  # dpiX : DWORD* out
    ctypes.POINTER(wintypes.DWORD),  # dpiY : DWORD* out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('api-ms-win-shcore-scaling-l1-1-1.dll')
GetDpiForMonitor = Fiddle::Function.new(
  lib['GetDpiForMonitor'],
  [
    Fiddle::TYPE_VOIDP,  # hmonitor : HMONITOR
    Fiddle::TYPE_INT,  # dpiType : MONITOR_DPI_TYPE
    Fiddle::TYPE_VOIDP,  # dpiX : DWORD* out
    Fiddle::TYPE_VOIDP,  # dpiY : DWORD* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "api-ms-win-shcore-scaling-l1-1-1")]
extern "system" {
    fn GetDpiForMonitor(
        hmonitor: *mut core::ffi::c_void,  // HMONITOR
        dpiType: i32,  // MONITOR_DPI_TYPE
        dpiX: *mut u32,  // DWORD* out
        dpiY: *mut u32  // DWORD* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("api-ms-win-shcore-scaling-l1-1-1.dll")]
public static extern int GetDpiForMonitor(IntPtr hmonitor, int dpiType, out uint dpiX, out uint dpiY);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-shcore-scaling-l1-1-1_GetDpiForMonitor' -Namespace Win32 -PassThru
# $api::GetDpiForMonitor(hmonitor, dpiType, dpiX, dpiY)
#uselib "api-ms-win-shcore-scaling-l1-1-1.dll"
#func global GetDpiForMonitor "GetDpiForMonitor" sptr, sptr, sptr, sptr
; GetDpiForMonitor hmonitor, dpiType, varptr(dpiX), varptr(dpiY)   ; 戻り値は stat
; hmonitor : HMONITOR -> "sptr"
; dpiType : MONITOR_DPI_TYPE -> "sptr"
; dpiX : DWORD* out -> "sptr"
; dpiY : DWORD* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "api-ms-win-shcore-scaling-l1-1-1.dll"
#cfunc global GetDpiForMonitor "GetDpiForMonitor" sptr, int, var, var
; res = GetDpiForMonitor(hmonitor, dpiType, dpiX, dpiY)
; hmonitor : HMONITOR -> "sptr"
; dpiType : MONITOR_DPI_TYPE -> "int"
; dpiX : DWORD* out -> "var"
; dpiY : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT GetDpiForMonitor(HMONITOR hmonitor, MONITOR_DPI_TYPE dpiType, DWORD* dpiX, DWORD* dpiY)
#uselib "api-ms-win-shcore-scaling-l1-1-1.dll"
#cfunc global GetDpiForMonitor "GetDpiForMonitor" intptr, int, var, var
; res = GetDpiForMonitor(hmonitor, dpiType, dpiX, dpiY)
; hmonitor : HMONITOR -> "intptr"
; dpiType : MONITOR_DPI_TYPE -> "int"
; dpiX : DWORD* out -> "var"
; dpiY : DWORD* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	api_ms_win_shcore_scaling_l1_1_1 = windows.NewLazySystemDLL("api-ms-win-shcore-scaling-l1-1-1.dll")
	procGetDpiForMonitor = api_ms_win_shcore_scaling_l1_1_1.NewProc("GetDpiForMonitor")
)

// hmonitor (HMONITOR), dpiType (MONITOR_DPI_TYPE), dpiX (DWORD* out), dpiY (DWORD* out)
r1, _, err := procGetDpiForMonitor.Call(
	uintptr(hmonitor),
	uintptr(dpiType),
	uintptr(dpiX),
	uintptr(dpiY),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function GetDpiForMonitor(
  hmonitor: THandle;   // HMONITOR
  dpiType: Integer;   // MONITOR_DPI_TYPE
  dpiX: Pointer;   // DWORD* out
  dpiY: Pointer   // DWORD* out
): Integer; stdcall;
  external 'api-ms-win-shcore-scaling-l1-1-1.dll' name 'GetDpiForMonitor';
result := DllCall("api-ms-win-shcore-scaling-l1-1-1\GetDpiForMonitor"
    , "Ptr", hmonitor   ; HMONITOR
    , "Int", dpiType   ; MONITOR_DPI_TYPE
    , "Ptr", dpiX   ; DWORD* out
    , "Ptr", dpiY   ; DWORD* out
    , "Int")   ; return: HRESULT
●GetDpiForMonitor(hmonitor, dpiType, dpiX, dpiY) = DLL("api-ms-win-shcore-scaling-l1-1-1.dll", "int GetDpiForMonitor(void*, int, void*, void*)")
# 呼び出し: GetDpiForMonitor(hmonitor, dpiType, dpiX, dpiY)
# hmonitor : HMONITOR -> "void*"
# dpiType : MONITOR_DPI_TYPE -> "int"
# dpiX : DWORD* out -> "void*"
# dpiY : DWORD* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "api-ms-win-shcore-scaling-l1-1-1" fn GetDpiForMonitor(
    hmonitor: ?*anyopaque, // HMONITOR
    dpiType: i32, // MONITOR_DPI_TYPE
    dpiX: [*c]u32, // DWORD* out
    dpiY: [*c]u32 // DWORD* out
) callconv(std.os.windows.WINAPI) i32;
proc GetDpiForMonitor(
    hmonitor: pointer,  # HMONITOR
    dpiType: int32,  # MONITOR_DPI_TYPE
    dpiX: ptr uint32,  # DWORD* out
    dpiY: ptr uint32  # DWORD* out
): int32 {.importc: "GetDpiForMonitor", stdcall, dynlib: "api-ms-win-shcore-scaling-l1-1-1.dll".}
pragma(lib, "api-ms-win-shcore-scaling-l1-1-1");
extern(Windows)
int GetDpiForMonitor(
    void* hmonitor,   // HMONITOR
    int dpiType,   // MONITOR_DPI_TYPE
    uint* dpiX,   // DWORD* out
    uint* dpiY   // DWORD* out
);
ccall((:GetDpiForMonitor, "api-ms-win-shcore-scaling-l1-1-1.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Int32, Ptr{UInt32}, Ptr{UInt32}),
      hmonitor, dpiType, dpiX, dpiY)
# hmonitor : HMONITOR -> Ptr{Cvoid}
# dpiType : MONITOR_DPI_TYPE -> Int32
# dpiX : DWORD* out -> Ptr{UInt32}
# dpiY : DWORD* out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t GetDpiForMonitor(
    void* hmonitor,
    int32_t dpiType,
    uint32_t* dpiX,
    uint32_t* dpiY);
]]
local api-ms-win-shcore-scaling-l1-1-1 = ffi.load("api-ms-win-shcore-scaling-l1-1-1")
-- api-ms-win-shcore-scaling-l1-1-1.GetDpiForMonitor(hmonitor, dpiType, dpiX, dpiY)
-- hmonitor : HMONITOR
-- dpiType : MONITOR_DPI_TYPE
-- dpiX : DWORD* out
-- dpiY : DWORD* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('api-ms-win-shcore-scaling-l1-1-1.dll');
const GetDpiForMonitor = lib.func('__stdcall', 'GetDpiForMonitor', 'int32_t', ['void *', 'int32_t', 'uint32_t *', 'uint32_t *']);
// GetDpiForMonitor(hmonitor, dpiType, dpiX, dpiY)
// hmonitor : HMONITOR -> 'void *'
// dpiType : MONITOR_DPI_TYPE -> 'int32_t'
// dpiX : DWORD* out -> 'uint32_t *'
// dpiY : DWORD* out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("api-ms-win-shcore-scaling-l1-1-1.dll", {
  GetDpiForMonitor: { parameters: ["pointer", "i32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.GetDpiForMonitor(hmonitor, dpiType, dpiX, dpiY)
// hmonitor : HMONITOR -> "pointer"
// dpiType : MONITOR_DPI_TYPE -> "i32"
// dpiX : DWORD* out -> "pointer"
// dpiY : DWORD* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t GetDpiForMonitor(
    void* hmonitor,
    int32_t dpiType,
    uint32_t* dpiX,
    uint32_t* dpiY);
C, "api-ms-win-shcore-scaling-l1-1-1.dll");
// $ffi->GetDpiForMonitor(hmonitor, dpiType, dpiX, dpiY);
// hmonitor : HMONITOR
// dpiType : MONITOR_DPI_TYPE
// dpiX : DWORD* out
// dpiY : DWORD* out
// 構造体/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 Api-ms-win-shcore-scaling-l1-1-1 extends StdCallLibrary {
    Api-ms-win-shcore-scaling-l1-1-1 INSTANCE = Native.load("api-ms-win-shcore-scaling-l1-1-1", Api-ms-win-shcore-scaling-l1-1-1.class);
    int GetDpiForMonitor(
        Pointer hmonitor,   // HMONITOR
        int dpiType,   // MONITOR_DPI_TYPE
        IntByReference dpiX,   // DWORD* out
        IntByReference dpiY   // DWORD* out
    );
}
@[Link("api-ms-win-shcore-scaling-l1-1-1")]
lib Libapi-ms-win-shcore-scaling-l1-1-1
  fun GetDpiForMonitor = GetDpiForMonitor(
    hmonitor : Void*,   # HMONITOR
    dpiType : Int32,   # MONITOR_DPI_TYPE
    dpiX : UInt32*,   # DWORD* out
    dpiY : UInt32*   # DWORD* out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetDpiForMonitorNative = Int32 Function(Pointer<Void>, Int32, Pointer<Uint32>, Pointer<Uint32>);
typedef GetDpiForMonitorDart = int Function(Pointer<Void>, int, Pointer<Uint32>, Pointer<Uint32>);
final GetDpiForMonitor = DynamicLibrary.open('api-ms-win-shcore-scaling-l1-1-1.dll')
    .lookupFunction<GetDpiForMonitorNative, GetDpiForMonitorDart>('GetDpiForMonitor');
// hmonitor : HMONITOR -> Pointer<Void>
// dpiType : MONITOR_DPI_TYPE -> Int32
// dpiX : DWORD* out -> Pointer<Uint32>
// dpiY : DWORD* out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetDpiForMonitor(
  hmonitor: THandle;   // HMONITOR
  dpiType: Integer;   // MONITOR_DPI_TYPE
  dpiX: Pointer;   // DWORD* out
  dpiY: Pointer   // DWORD* out
): Integer; stdcall;
  external 'api-ms-win-shcore-scaling-l1-1-1.dll' name 'GetDpiForMonitor';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetDpiForMonitor"
  c_GetDpiForMonitor :: Ptr () -> Int32 -> Ptr Word32 -> Ptr Word32 -> IO Int32
-- hmonitor : HMONITOR -> Ptr ()
-- dpiType : MONITOR_DPI_TYPE -> Int32
-- dpiX : DWORD* out -> Ptr Word32
-- dpiY : DWORD* out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getdpiformonitor =
  foreign "GetDpiForMonitor"
    ((ptr void) @-> int32_t @-> (ptr uint32_t) @-> (ptr uint32_t) @-> returning int32_t)
(* hmonitor : HMONITOR -> (ptr void) *)
(* dpiType : MONITOR_DPI_TYPE -> int32_t *)
(* dpiX : DWORD* out -> (ptr uint32_t) *)
(* dpiY : DWORD* out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library api-ms-win-shcore-scaling-l1-1-1 (t "api-ms-win-shcore-scaling-l1-1-1.dll"))
(cffi:use-foreign-library api-ms-win-shcore-scaling-l1-1-1)

(cffi:defcfun ("GetDpiForMonitor" get-dpi-for-monitor :convention :stdcall) :int32
  (hmonitor :pointer)   ; HMONITOR
  (dpi-type :int32)   ; MONITOR_DPI_TYPE
  (dpi-x :pointer)   ; DWORD* out
  (dpi-y :pointer))   ; DWORD* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetDpiForMonitor = Win32::API::More->new('api-ms-win-shcore-scaling-l1-1-1',
    'int GetDpiForMonitor(HANDLE hmonitor, int dpiType, LPVOID dpiX, LPVOID dpiY)');
# my $ret = $GetDpiForMonitor->Call($hmonitor, $dpiType, $dpiX, $dpiY);
# hmonitor : HMONITOR -> HANDLE
# dpiType : MONITOR_DPI_TYPE -> int
# dpiX : DWORD* out -> LPVOID
# dpiY : DWORD* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型