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

GetDpiForWindow

関数
指定ウィンドウのDPI値を取得する。
DLLUSER32.dll呼出規約winapi対応OSWindows 10 以降

シグネチャ

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

DWORD GetDpiForWindow(
    HWND hwnd
);

パラメーター

名前方向
hwndHWNDin

戻り値の型: DWORD

各言語での呼び出し定義

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

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

GetDpiForWindow = ctypes.windll.user32.GetDpiForWindow
GetDpiForWindow.restype = wintypes.DWORD
GetDpiForWindow.argtypes = [
    wintypes.HANDLE,  # hwnd : HWND
]
require 'fiddle'
require 'fiddle/import'

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procGetDpiForWindow = user32.NewProc("GetDpiForWindow")
)

// hwnd (HWND)
r1, _, err := procGetDpiForWindow.Call(
	uintptr(hwnd),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function GetDpiForWindow(
  hwnd: THandle   // HWND
): DWORD; stdcall;
  external 'USER32.dll' name 'GetDpiForWindow';
result := DllCall("USER32\GetDpiForWindow"
    , "Ptr", hwnd   ; HWND
    , "UInt")   ; return: DWORD
●GetDpiForWindow(hwnd) = DLL("USER32.dll", "dword GetDpiForWindow(void*)")
# 呼び出し: GetDpiForWindow(hwnd)
# hwnd : HWND -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。