Mukesh Bharsakle
Mukesh Bharsakle

Reputation: 443

Always getting primary monitor DPI

I need to get dpi of an external monitor, but I always get dpi of the primary monitor. I also have done manifest settings. I used following code to fetch it.

Definition

public static void GetPerMonitorDPIEx(ref double dx, ref double dy, int x, 
      int y)
    {
        UInt32 dpiX = 0;
        UInt32 dpiY = 0;
        System.Drawing.Point pt = new System.Drawing.Point(x, y);
        bool m = SetProcessDpiAwarenessContext((int)PROCESS_DPI_AWARENESS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
        var mhandle = MonitorFromPoint( pt, Utils.DEFAULT_TO_NEAREST);
        uint m5 = GetDpiForMonitor(mhandle, MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out dpiX, out dpiY);
        dx = (double)dpiX / 96;
        dy = (double)dpiY / 96;
    }

Call

GetPerMonitorDPIEx(ref dpiX, ref dpiY, System.Windows.Forms.Screen.AllScreens[i].Bounds.Left + 1, System.Windows.Forms.Screen.AllScreens[i].Bounds.Top +1);

I could see the monitor handle is coming correct for both the display, after that dpi values always coming from the primary display.

Upvotes: 0

Views: 754

Answers (2)

mcolhoun
mcolhoun

Reputation: 507

While researching the same topic, I stumbled across your question: According to Microsoft Docs:

GetDpiForMonitor is not DPI aware and should not be used if the calling thread is per-monitor DPI aware. For the DPI-aware version of this API, see GetDpiForWindow.

source: learn.microsoft.com

Upvotes: 2

ThunderBird
ThunderBird

Reputation: 81

First set your application as dpi aware then try to chek the output of

uint m5 = GetDpiForMonitor(mhandle, MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out dpiX, out dpiY);

Upvotes: 1

Related Questions