GetScaleFactorForMonitor
関数指定モニターの表示スケール係数を取得する。
シグネチャ
// api-ms-win-shcore-scaling-l1-1-1.dll
#include <windows.h>
HRESULT GetScaleFactorForMonitor(
HMONITOR hMon,
DEVICE_SCALE_FACTOR* pScale
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hMon | HMONITOR | in | モニターのハンドル。 |
| pScale | DEVICE_SCALE_FACTOR* | out | この関数が正常に終了すると、この値は指定したモニターの拡大率を示す DEVICE_SCALE_FACTOR 値のいずれかを指します。 関数呼び出しが失敗した場合でも、この値は有効な拡大率を指すため、アプリは誤ったサイズのリソースのまま処理を継続することを選択できます。 |
戻り値の型: HRESULT
公式ドキュメント
特定のモニターの拡大率を取得します。この関数は GetScaleFactorForDevice の代替です。
戻り値
この関数が成功した場合は S_OK を返します。それ以外の場合は HRESULT エラーコードを返します。
解説(Remarks)
アプリのウィンドウはモニター間で移動される可能性があるため、コードでは RegisterScaleChangeEvent を通じて登録した拡大率変更イベントに加えて、WM_WINDOWPOSCHANGED メッセージも処理する必要があります。WM_WINDOWPOSCHANGED メッセージに応答して、MonitorFromWindow を呼び出し、続いて GetScaleFactorForMonitor を呼び出して、アプリのウィンドウが表示されているモニターの拡大率を取得します。これにより、コードはアセットを再読み込みしてレイアウトを変更することで、ドット/インチ (dpi) の変化に対応できます。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// api-ms-win-shcore-scaling-l1-1-1.dll
#include <windows.h>
HRESULT GetScaleFactorForMonitor(
HMONITOR hMon,
DEVICE_SCALE_FACTOR* pScale
);[DllImport("api-ms-win-shcore-scaling-l1-1-1.dll", ExactSpelling = true)]
static extern int GetScaleFactorForMonitor(
IntPtr hMon, // HMONITOR
out int pScale // DEVICE_SCALE_FACTOR* out
);<DllImport("api-ms-win-shcore-scaling-l1-1-1.dll", ExactSpelling:=True)>
Public Shared Function GetScaleFactorForMonitor(
hMon As IntPtr, ' HMONITOR
<Out> ByRef pScale As Integer ' DEVICE_SCALE_FACTOR* out
) As Integer
End Function' hMon : HMONITOR
' pScale : DEVICE_SCALE_FACTOR* out
Declare PtrSafe Function GetScaleFactorForMonitor Lib "api-ms-win-shcore-scaling-l1-1-1" ( _
ByVal hMon As LongPtr, _
ByRef pScale As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
GetScaleFactorForMonitor = ctypes.windll.LoadLibrary("api-ms-win-shcore-scaling-l1-1-1.dll").GetScaleFactorForMonitor
GetScaleFactorForMonitor.restype = ctypes.c_int
GetScaleFactorForMonitor.argtypes = [
wintypes.HANDLE, # hMon : HMONITOR
ctypes.c_void_p, # pScale : DEVICE_SCALE_FACTOR* out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('api-ms-win-shcore-scaling-l1-1-1.dll')
GetScaleFactorForMonitor = Fiddle::Function.new(
lib['GetScaleFactorForMonitor'],
[
Fiddle::TYPE_VOIDP, # hMon : HMONITOR
Fiddle::TYPE_VOIDP, # pScale : DEVICE_SCALE_FACTOR* out
],
Fiddle::TYPE_INT)#[link(name = "api-ms-win-shcore-scaling-l1-1-1")]
extern "system" {
fn GetScaleFactorForMonitor(
hMon: *mut core::ffi::c_void, // HMONITOR
pScale: *mut i32 // DEVICE_SCALE_FACTOR* 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 GetScaleFactorForMonitor(IntPtr hMon, out int pScale);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-shcore-scaling-l1-1-1_GetScaleFactorForMonitor' -Namespace Win32 -PassThru
# $api::GetScaleFactorForMonitor(hMon, pScale)#uselib "api-ms-win-shcore-scaling-l1-1-1.dll"
#func global GetScaleFactorForMonitor "GetScaleFactorForMonitor" sptr, sptr
; GetScaleFactorForMonitor hMon, varptr(pScale) ; 戻り値は stat
; hMon : HMONITOR -> "sptr"
; pScale : DEVICE_SCALE_FACTOR* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "api-ms-win-shcore-scaling-l1-1-1.dll" #cfunc global GetScaleFactorForMonitor "GetScaleFactorForMonitor" sptr, var ; res = GetScaleFactorForMonitor(hMon, pScale) ; hMon : HMONITOR -> "sptr" ; pScale : DEVICE_SCALE_FACTOR* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "api-ms-win-shcore-scaling-l1-1-1.dll" #cfunc global GetScaleFactorForMonitor "GetScaleFactorForMonitor" sptr, sptr ; res = GetScaleFactorForMonitor(hMon, varptr(pScale)) ; hMon : HMONITOR -> "sptr" ; pScale : DEVICE_SCALE_FACTOR* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; HRESULT GetScaleFactorForMonitor(HMONITOR hMon, DEVICE_SCALE_FACTOR* pScale) #uselib "api-ms-win-shcore-scaling-l1-1-1.dll" #cfunc global GetScaleFactorForMonitor "GetScaleFactorForMonitor" intptr, var ; res = GetScaleFactorForMonitor(hMon, pScale) ; hMon : HMONITOR -> "intptr" ; pScale : DEVICE_SCALE_FACTOR* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HRESULT GetScaleFactorForMonitor(HMONITOR hMon, DEVICE_SCALE_FACTOR* pScale) #uselib "api-ms-win-shcore-scaling-l1-1-1.dll" #cfunc global GetScaleFactorForMonitor "GetScaleFactorForMonitor" intptr, intptr ; res = GetScaleFactorForMonitor(hMon, varptr(pScale)) ; hMon : HMONITOR -> "intptr" ; pScale : DEVICE_SCALE_FACTOR* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは 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")
procGetScaleFactorForMonitor = api_ms_win_shcore_scaling_l1_1_1.NewProc("GetScaleFactorForMonitor")
)
// hMon (HMONITOR), pScale (DEVICE_SCALE_FACTOR* out)
r1, _, err := procGetScaleFactorForMonitor.Call(
uintptr(hMon),
uintptr(pScale),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HRESULTfunction GetScaleFactorForMonitor(
hMon: THandle; // HMONITOR
pScale: Pointer // DEVICE_SCALE_FACTOR* out
): Integer; stdcall;
external 'api-ms-win-shcore-scaling-l1-1-1.dll' name 'GetScaleFactorForMonitor';result := DllCall("api-ms-win-shcore-scaling-l1-1-1\GetScaleFactorForMonitor"
, "Ptr", hMon ; HMONITOR
, "Ptr", pScale ; DEVICE_SCALE_FACTOR* out
, "Int") ; return: HRESULT●GetScaleFactorForMonitor(hMon, pScale) = DLL("api-ms-win-shcore-scaling-l1-1-1.dll", "int GetScaleFactorForMonitor(void*, void*)")
# 呼び出し: GetScaleFactorForMonitor(hMon, pScale)
# hMon : HMONITOR -> "void*"
# pScale : DEVICE_SCALE_FACTOR* 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 GetScaleFactorForMonitor(
hMon: ?*anyopaque, // HMONITOR
pScale: [*c]i32 // DEVICE_SCALE_FACTOR* out
) callconv(std.os.windows.WINAPI) i32;proc GetScaleFactorForMonitor(
hMon: pointer, # HMONITOR
pScale: ptr int32 # DEVICE_SCALE_FACTOR* out
): int32 {.importc: "GetScaleFactorForMonitor", stdcall, dynlib: "api-ms-win-shcore-scaling-l1-1-1.dll".}pragma(lib, "api-ms-win-shcore-scaling-l1-1-1");
extern(Windows)
int GetScaleFactorForMonitor(
void* hMon, // HMONITOR
int* pScale // DEVICE_SCALE_FACTOR* out
);ccall((:GetScaleFactorForMonitor, "api-ms-win-shcore-scaling-l1-1-1.dll"), stdcall, Int32,
(Ptr{Cvoid}, Ptr{Int32}),
hMon, pScale)
# hMon : HMONITOR -> Ptr{Cvoid}
# pScale : DEVICE_SCALE_FACTOR* out -> Ptr{Int32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t GetScaleFactorForMonitor(
void* hMon,
int32_t* pScale);
]]
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.GetScaleFactorForMonitor(hMon, pScale)
-- hMon : HMONITOR
-- pScale : DEVICE_SCALE_FACTOR* 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 GetScaleFactorForMonitor = lib.func('__stdcall', 'GetScaleFactorForMonitor', 'int32_t', ['void *', 'int32_t *']);
// GetScaleFactorForMonitor(hMon, pScale)
// hMon : HMONITOR -> 'void *'
// pScale : DEVICE_SCALE_FACTOR* out -> 'int32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("api-ms-win-shcore-scaling-l1-1-1.dll", {
GetScaleFactorForMonitor: { parameters: ["pointer", "pointer"], result: "i32" },
});
// lib.symbols.GetScaleFactorForMonitor(hMon, pScale)
// hMon : HMONITOR -> "pointer"
// pScale : DEVICE_SCALE_FACTOR* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t GetScaleFactorForMonitor(
void* hMon,
int32_t* pScale);
C, "api-ms-win-shcore-scaling-l1-1-1.dll");
// $ffi->GetScaleFactorForMonitor(hMon, pScale);
// hMon : HMONITOR
// pScale : DEVICE_SCALE_FACTOR* 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 GetScaleFactorForMonitor(
Pointer hMon, // HMONITOR
IntByReference pScale // DEVICE_SCALE_FACTOR* out
);
}@[Link("api-ms-win-shcore-scaling-l1-1-1")]
lib Libapi-ms-win-shcore-scaling-l1-1-1
fun GetScaleFactorForMonitor = GetScaleFactorForMonitor(
hMon : Void*, # HMONITOR
pScale : Int32* # DEVICE_SCALE_FACTOR* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef GetScaleFactorForMonitorNative = Int32 Function(Pointer<Void>, Pointer<Int32>);
typedef GetScaleFactorForMonitorDart = int Function(Pointer<Void>, Pointer<Int32>);
final GetScaleFactorForMonitor = DynamicLibrary.open('api-ms-win-shcore-scaling-l1-1-1.dll')
.lookupFunction<GetScaleFactorForMonitorNative, GetScaleFactorForMonitorDart>('GetScaleFactorForMonitor');
// hMon : HMONITOR -> Pointer<Void>
// pScale : DEVICE_SCALE_FACTOR* out -> Pointer<Int32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function GetScaleFactorForMonitor(
hMon: THandle; // HMONITOR
pScale: Pointer // DEVICE_SCALE_FACTOR* out
): Integer; stdcall;
external 'api-ms-win-shcore-scaling-l1-1-1.dll' name 'GetScaleFactorForMonitor';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "GetScaleFactorForMonitor"
c_GetScaleFactorForMonitor :: Ptr () -> Ptr Int32 -> IO Int32
-- hMon : HMONITOR -> Ptr ()
-- pScale : DEVICE_SCALE_FACTOR* out -> Ptr Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let getscalefactorformonitor =
foreign "GetScaleFactorForMonitor"
((ptr void) @-> (ptr int32_t) @-> returning int32_t)
(* hMon : HMONITOR -> (ptr void) *)
(* pScale : DEVICE_SCALE_FACTOR* out -> (ptr int32_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 ("GetScaleFactorForMonitor" get-scale-factor-for-monitor :convention :stdcall) :int32
(h-mon :pointer) ; HMONITOR
(p-scale :pointer)) ; DEVICE_SCALE_FACTOR* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $GetScaleFactorForMonitor = Win32::API::More->new('api-ms-win-shcore-scaling-l1-1-1',
'int GetScaleFactorForMonitor(HANDLE hMon, LPVOID pScale)');
# my $ret = $GetScaleFactorForMonitor->Call($hMon, $pScale);
# hMon : HMONITOR -> HANDLE
# pScale : DEVICE_SCALE_FACTOR* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
公式の関連項目
- f RegisterScaleChangeEvent — スケール変更時にシグナルされるイベントを登録する。
- f UnregisterScaleChangeEvent — スケール変更イベントの登録を解除する。