0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windowsのバグ?GetWindowRectとdpiの変な仕様を見つけた件について.

Last updated at Posted at 2025-04-09

環境

  • Unity 2022.3.5f1
  • Windows 11 Home 24H2 (Bld. 26100.3775)

GetWindowRectとdpi(拡大率)

Win32APIには,"GetWindowRect()" というAPIがある.

Unityからも,以下のようにDllImportを行うことで,このAPIを使用することができる.

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

先日,このAPIについて,変な仕様を見つけた.
システム>ディスプレイ>拡大縮小とレイアウト から拡大率を100%から変更するときにその現象は起こる.

↓ 現在のモニター環境はこんな感じ
スクリーンショット 2025-03-24 162710.png

dpi=96 (つまり,拡大率100%)のとき,GetWindowRect()で取得できるRectの4つの値は,以下のようになる.

image.png
※左の4つの値は,GetClientRect()という別のAPIで取得した値なので,本文とは関係ない.

(Left,Top,Right,Bottom) = (1920/348/4480/1788)
まぁ,Width = 4480-1920 = 2560なので合っている.

では,dpi=192 (拡大率200%)のときはどうか.
image.png

なるほど,なるほど.
dpiを変更したとしても,GetWindowRect()で取得できるRectは変更がないのね.
計算できるWidth の値は変わらず,Width = 4480-1920 = 2560.

法則性を変えてきたWindows君

問題はここからである.
dpi=168 (拡大率175%)にしてみた.
image.png

(Left,Top,Right,Bottom) = (1920/348/3383/1171)

... ん?
dpiを変更しても,取得できる値は変わらないのでは..?

Width = 3383-1920 = 1463となっていて,モニタの幅が2560じゃなくなっている.
2560/1.75=1462.8≒1463であるから,その数字になることはおかしなことではないのだけれど,それならdpi=192のときに,計算できるWidthの値が2560/2.0=1280になるはず.

他のdpiでも検証してみた

↓ dpi=120 (拡大率125%)
image.png
Width = 3968-1920 = 2048 (※2560/1.25=2048)

↓ dpi=144 (拡大率150%)
image.png
Width = 3627-1920 = 1707 (※2560/1.5=1706.6)

↓ dpi=216 (拡大率225%)
image.png
Width = 4480-1920 = 2560

「200%未満だったら,得られるRectは拡大率に対応するけど,200%以上になるとそのまま」 という感じだろうか?

まとめ

正直,なんでこんな仕様になっているのかはわからない.
なにかいいことがあるのだろうか.

時間があるときに,追加で検証を行ってみたり,情報収集をしたりして,事情を調べてみたい.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?