0

I'm trying to reliably detect when a Form is currently docked. My current code works, but not when scaling is different than 100%, predictably.

I'm trying to find a function that will return the working area coordinates that will match what GetExtendedFrameBounds (PInvoke: DwmGetWindowAttribute with DWMWA_EXTENDED_FRAME_BOUNDS) gives me to work with, which I'm using to detect docking.

public static bool IsWindowDocked(Form form, int numSides = 3)
{
    var workArea = Screen.GetWorkingArea(form);
    var winRect = PInvoke.GetExtendedFrameBounds(form.Handle);
    var score = (winRect.Left == workArea.Left ? 1 : 0) +
        (winRect.Top == workArea.Top ? 1 : 0) +
        (winRect.Right == workArea.Right ? 1 : 0) +
        (winRect.Bottom == workArea.Bottom ? 1 : 0);
    return (score >= numSides);
}

Current result will detect docking on 100% scaling, but not other display scaling values!

  • Is your application DPI Aware? – Jimi Feb 18 '19 at 20:45
  • no, i'm not supporting it at the moment, so it uses default system bitmap scaling and i set all **AutoScaleMode** to **None**. my problem stems from the fact that the PInvoke command i've found that can give me the real L,T,R,B coordinates of my window, without taking into account shadow/decoration (DWMWA_EXTENDED_FRAME_BOUNDS) does return the information in actual pixels, while my app gets the scaled version. – Princess Daphie Feb 19 '19 at 20:19
  • Seeting `AutoScaleMode = none` is not relevant. If you application is not DPIAware, the system will virtualized it and with it all the relative measures you can get from standard methods. Plus, most of the standard API is not DPIAware either (`GetWindowRect` for eample). The measures you get from these are virtualized, too. If you have to deal with display measurements, your app needs to be DPIAware. The same goes for DWM related calls. Make a test using an `app.manifest` where you uncomment the `` part, setting it to true (default). – Jimi Feb 19 '19 at 21:13
  • i tested this to see and you're right, i probably can detect DPI scaling when enabling this, but i don't want to enable this, as my layout is messed up. i am not prepared to handle all the technicalities of a dpi-aware application. i just want to detect windows 7 docking... – Princess Daphie Mar 03 '19 at 00:14
  • Yes, I can understand that. But note that Dpi Awareness can be enabled on a per-thread basis. Meaning that not all you application needs to be dpiAware, possibly just one thread, where you can take all the measures you want and receive correct results. Read the notes I've written here: [DPI Awareness - Unaware in one Release, System Aware in the Other](https://stackoverflow.com/a/50276714/7444103), see whether you can make it work. – Jimi Mar 03 '19 at 01:33

0 Answers0