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

PhysicalToLogicalPointForPerMonitorDPI

関数
モニターごとDPI環境で物理座標を論理座標へ変換する。
DLLUSER32.dll呼出規約winapi対応OSWindows 8.1 以降

シグネチャ

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

BOOL PhysicalToLogicalPointForPerMonitorDPI(
    HWND hWnd,   // optional
    POINT* lpPoint
);

パラメーター

名前型方向説明
hWndHWNDinoptional変換に使用する変換情報を持つウィンドウへのハンドル。
lpPointPOINT*inout変換する物理座標 (画面座標) を指定する POINT 構造体へのポインター。関数が成功した場合、新しい論理座標がこの構造体にコピーされます。

戻り値の型: BOOL

公式ドキュメント

呼び出し元の DPI 認識 (dots per inch (dpi) awareness) に関係なく、ウィンドウ内の点を物理座標から論理座標に変換します。

戻り値

成功した場合は TRUE を、それ以外の場合は FALSE を返します。

解説(Remarks)

Windows 8 では、システム DPI 対応アプリケーションは PhysicalToLogicalPoint と LogicalToPhysicalPoint を使用して物理空間と論理空間の間で変換を行います。Windows 8.1 では、システムおよびプロセス間通信の仮想化が追加されたため、大半のアプリケーションではこれらの API は必要ありません。その結果、Windows 8.1 ではこれらの API は点を変換しなくなりました。システムはすべての点をアプリケーション自身の座標空間で返します。 この動作により大半のアプリケーションの機能は維持されますが、アプリケーションが期待どおりに動作するように変更が必要となる例外もいくつかあります。

たとえば、アプリケーションが別のプロセスのウィンドウツリー全体をたどり、そのウィンドウに関する DPI 依存の情報をシステムに問い合わせる必要がある場合があります。既定では、システムは呼び出し元の DPI 認識に基づいて情報を返します。これはほとんどのアプリケーションにとって理想的です。しかし、呼び出し元が、そのウィンドウに関連付けられたアプリケーションの DPI 認識に基づく情報を必要とする場合もあります。これは、2 つのアプリケーションが DPI 依存の情報を互いに直接やり取りする場合に必要となることがあります。この場合、アプリケーションは LogicalToPhysicalPointForPerMonitorDPI を使用して物理座標を取得し、続いて PhysicalToLogicalPointForPerMonitorDPI を使用して、指定された HWND の DPI 認識に基づいて物理座標を論理座標に変換できます。

2 つのアプリケーションを考えます。一方は PROCESS_DPI_AWARENESS の値が PROCESS_DPI_UNAWARE で、もう一方は PROCESS_PER_MONITOR_AWARE です。PROCESS_PER_MONITOR_AWARE のアプリは、拡大率が 200% (192 DPI) である 1 台のモニター上にウィンドウを作成します。両方のアプリがこのウィンドウに対して GetWindowRect を呼び出すと、それぞれ異なる値を受け取ります。PROCESS_DPI_UNAWARE のアプリは 96 DPI の座標に基づく rect を受け取り、PROCESS_PER_MONITOR_AWARE のアプリはモニターの実際の DPI に一致する座標を受け取ります。PROCESS_DPI_UNAWARE のアプリが、システムから PROCESS_PER_MONITOR_AWARE のアプリに返された rect を必要とする場合、自身の rect の各隅について LogicalToPhysicalPointForPerMonitorDPI を呼び出し、PROCESS_PER_MONITOR_AWARE のアプリのウィンドウへのハンドルを渡すことができます。これにより、もう一方のアプリの認識に基づく点が返され、それを使用して rect を作成できます。PROCESS_PER_MONITOR_AWARE はモニターの実際の DPI を使用するため、論理座標と物理座標は同一であり、これによってこの方法が機能します。

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

各言語での呼び出し定義

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

BOOL PhysicalToLogicalPointForPerMonitorDPI(
    HWND hWnd,   // optional
    POINT* lpPoint
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", ExactSpelling = true)]
static extern bool PhysicalToLogicalPointForPerMonitorDPI(
    IntPtr hWnd,   // HWND optional
    IntPtr lpPoint   // POINT* in/out
);
<DllImport("USER32.dll", ExactSpelling:=True)>
Public Shared Function PhysicalToLogicalPointForPerMonitorDPI(
    hWnd As IntPtr,   ' HWND optional
    lpPoint As IntPtr   ' POINT* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hWnd : HWND optional
' lpPoint : POINT* in/out
Declare PtrSafe Function PhysicalToLogicalPointForPerMonitorDPI Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal lpPoint As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

PhysicalToLogicalPointForPerMonitorDPI = ctypes.windll.user32.PhysicalToLogicalPointForPerMonitorDPI
PhysicalToLogicalPointForPerMonitorDPI.restype = wintypes.BOOL
PhysicalToLogicalPointForPerMonitorDPI.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND optional
    ctypes.c_void_p,  # lpPoint : POINT* in/out
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
PhysicalToLogicalPointForPerMonitorDPI = Fiddle::Function.new(
  lib['PhysicalToLogicalPointForPerMonitorDPI'],
  [
    Fiddle::TYPE_VOIDP,  # hWnd : HWND optional
    Fiddle::TYPE_VOIDP,  # lpPoint : POINT* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn PhysicalToLogicalPointForPerMonitorDPI(
        hWnd: *mut core::ffi::c_void,  // HWND optional
        lpPoint: *mut POINT  // POINT* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll")]
public static extern bool PhysicalToLogicalPointForPerMonitorDPI(IntPtr hWnd, IntPtr lpPoint);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_PhysicalToLogicalPointForPerMonitorDPI' -Namespace Win32 -PassThru
# $api::PhysicalToLogicalPointForPerMonitorDPI(hWnd, lpPoint)
#uselib "USER32.dll"
#func global PhysicalToLogicalPointForPerMonitorDPI "PhysicalToLogicalPointForPerMonitorDPI" sptr, sptr
; PhysicalToLogicalPointForPerMonitorDPI hWnd, varptr(lpPoint)   ; 戻り値は stat
; hWnd : HWND optional -> "sptr"
; lpPoint : POINT* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USER32.dll"
#cfunc global PhysicalToLogicalPointForPerMonitorDPI "PhysicalToLogicalPointForPerMonitorDPI" sptr, var
; res = PhysicalToLogicalPointForPerMonitorDPI(hWnd, lpPoint)
; hWnd : HWND optional -> "sptr"
; lpPoint : POINT* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL PhysicalToLogicalPointForPerMonitorDPI(HWND hWnd, POINT* lpPoint)
#uselib "USER32.dll"
#cfunc global PhysicalToLogicalPointForPerMonitorDPI "PhysicalToLogicalPointForPerMonitorDPI" intptr, var
; res = PhysicalToLogicalPointForPerMonitorDPI(hWnd, lpPoint)
; hWnd : HWND optional -> "intptr"
; lpPoint : POINT* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procPhysicalToLogicalPointForPerMonitorDPI = user32.NewProc("PhysicalToLogicalPointForPerMonitorDPI")
)

// hWnd (HWND optional), lpPoint (POINT* in/out)
r1, _, err := procPhysicalToLogicalPointForPerMonitorDPI.Call(
	uintptr(hWnd),
	uintptr(lpPoint),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function PhysicalToLogicalPointForPerMonitorDPI(
  hWnd: THandle;   // HWND optional
  lpPoint: Pointer   // POINT* in/out
): BOOL; stdcall;
  external 'USER32.dll' name 'PhysicalToLogicalPointForPerMonitorDPI';
result := DllCall("USER32\PhysicalToLogicalPointForPerMonitorDPI"
    , "Ptr", hWnd   ; HWND optional
    , "Ptr", lpPoint   ; POINT* in/out
    , "Int")   ; return: BOOL
●PhysicalToLogicalPointForPerMonitorDPI(hWnd, lpPoint) = DLL("USER32.dll", "bool PhysicalToLogicalPointForPerMonitorDPI(void*, void*)")
# 呼び出し: PhysicalToLogicalPointForPerMonitorDPI(hWnd, lpPoint)
# hWnd : HWND optional -> "void*"
# lpPoint : POINT* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef PhysicalToLogicalPointForPerMonitorDPINative = Int32 Function(Pointer<Void>, Pointer<Void>);
typedef PhysicalToLogicalPointForPerMonitorDPIDart = int Function(Pointer<Void>, Pointer<Void>);
final PhysicalToLogicalPointForPerMonitorDPI = DynamicLibrary.open('USER32.dll')
    .lookupFunction<PhysicalToLogicalPointForPerMonitorDPINative, PhysicalToLogicalPointForPerMonitorDPIDart>('PhysicalToLogicalPointForPerMonitorDPI');
// hWnd : HWND optional -> Pointer<Void>
// lpPoint : POINT* in/out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function PhysicalToLogicalPointForPerMonitorDPI(
  hWnd: THandle;   // HWND optional
  lpPoint: Pointer   // POINT* in/out
): BOOL; stdcall;
  external 'USER32.dll' name 'PhysicalToLogicalPointForPerMonitorDPI';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "PhysicalToLogicalPointForPerMonitorDPI"
  c_PhysicalToLogicalPointForPerMonitorDPI :: Ptr () -> Ptr () -> IO CInt
-- hWnd : HWND optional -> Ptr ()
-- lpPoint : POINT* in/out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let physicaltologicalpointforpermonitordpi =
  foreign "PhysicalToLogicalPointForPerMonitorDPI"
    ((ptr void) @-> (ptr void) @-> returning int32_t)
(* hWnd : HWND optional -> (ptr void) *)
(* lpPoint : POINT* in/out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("PhysicalToLogicalPointForPerMonitorDPI" physical-to-logical-point-for-per-monitor-dpi :convention :stdcall) :int32
  (h-wnd :pointer)   ; HWND optional
  (lp-point :pointer))   ; POINT* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $PhysicalToLogicalPointForPerMonitorDPI = Win32::API::More->new('USER32',
    'BOOL PhysicalToLogicalPointForPerMonitorDPI(HANDLE hWnd, LPVOID lpPoint)');
# my $ret = $PhysicalToLogicalPointForPerMonitorDPI->Call($hWnd, $lpPoint);
# hWnd : HWND optional -> HANDLE
# lpPoint : POINT* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型