I am creating a program that moves/resizes windows from another process with SetWindowPos()
. My own program is PROCESS_PER_MONITOR_DPI_AWARE
. The other programs could be anything from PROCESS_DPI_UNAWARE
, PROCESS_SYSTEM_DPI_AWARE
or PROCESS_PER_MONITOR_DPI_AWARE
.
Because my own program is PROCESS_PER_MONITOR_DPI_AWARE
, the coordinates I pass to SetWindowPos()
are in physical coordinates. What I now want to do is resize the client area to a specific size in logical coordinates.
What I have tried to do is
- Get the DPI of the monitor where the window is placed as
screenDPI
. - Get the DPI of the target window as
windowDPI
. - Get
scaleFactor
asscreenDPI / windowDPI
. - Scale the desired client area size by
scaleFactor
- Calculated the extra size for the window frame by subtracting the current client rect size from the window rect size.
This works for the most part, but when I am using two screens with different display scaling, then
- the calculation of the window frame size is off if I move the window from one screen to the next.
- this fails for an application that uses
PROCESS_SYSTEM_DPI_AWARE
, when the window is located on the secondary screen (which uses 96dpi compared to the primary screen with 120dpi). This has nothing to do with the window frame size and I am not yet sure why exactly it fails, but the targetx
andy
coordinates are scaled up so that the window is moved offscreen. - what happens if, because of the resize, the center of the window changes the screen? Then the
screenDPI
will no longer be correct, right? How would I handle that case?
I know that there is also the function AdjustWindowRectExForDpi
, but somehow I can't get it to work properly. What is the dpi
value I am supposed to pass to it? The dpi of the target screen, the dpi of the target window or the dpi of my own program? Additionally, this function is only available from Windows 10 onwards, so how would I handle it on an older Windows client?
I would appreciate some help with this. Thanks!