title: SetWindowPos() cross-process DPI aware
body: <p>I am creating a program that moves/resizes windows from another process with <code>SetWindowPos()</code>. My own program is <code>PROCESS_PER_MONITOR_DPI_AWARE</code>. The other programs could be anything from <code>PROCESS_DPI_UNAWARE</code>, <code>PROCESS_SYSTEM_DPI_AWARE</code> or <code>PROCESS_PER_MONITOR_DPI_AWARE</code>.</p>
  
  <p>Because my own program is <code>PROCESS_PER_MONITOR_DPI_AWARE</code>, the coordinates I pass to <code>SetWindowPos()</code> are in physical coordinates. What I now want to do is <strong>resize the client area</strong> to a specific size in <strong>logical coordinates</strong>.</p>
  
  <p>What I have tried to do is</p>
  
  <ol>
  <li>Get the DPI of the monitor where the window is placed as <code>screenDPI</code>.</li>
  <li>Get the DPI of the target window as <code>windowDPI</code>.</li>
  <li>Get <code>scaleFactor</code> as <code>screenDPI / windowDPI</code>.</li>
  <li>Scale the desired client area size by <code>scaleFactor</code></li>
  <li>Calculated the extra size for the window frame by subtracting the current client rect size from the window rect size.</li>
  </ol>
  
  <p>This works for the most part, but when I am using two screens with different display scaling, then</p>
  
  <ul>
  <li>the calculation of the window frame size is off if I move the window from one screen to the next.</li>
  <li>this fails for an application that uses <code>PROCESS_SYSTEM_DPI_AWARE</code>, 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 target <code>x</code> and <code>y</code> coordinates are scaled up so that the window is moved offscreen.</li>
  <li>what happens if, because of the resize, the center of the window changes the screen? Then the <code>screenDPI</code> will no longer be correct, right? How would I handle that case?</li>
  </ul>
  
  <p>I know that there is also the function <code>AdjustWindowRectExForDpi</code>, but somehow I can't get it to work properly. What is the <code>dpi</code> 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?</p>
  
  <p>I would appreciate some help with this. Thanks!</p>