private static int GetMonitorDpiScale(LUID adapterId, uint targetId)
        {
            try
            {
                DISPLAYCONFIG_SOURCE_DPI_SCALE_GET deviceInfo = new DISPLAYCONFIG_SOURCE_DPI_SCALE_GET();
                deviceInfo.header.size      = (uint)Marshal.SizeOf(typeof(DISPLAYCONFIG_SOURCE_DPI_SCALE_GET));
                deviceInfo.header.adapterId = adapterId;
                deviceInfo.header.id        = targetId;
                deviceInfo.header.type      = DISPLAYCONFIG_DEVICE_INFO_TYPE.DISPLAYCONFIG_DEVICE_INFO_GET_DPI_SCALE;

                int error = DisplayConfigGetDeviceInfo(ref deviceInfo);
                if (error != 0)
                {
                    Debug.WriteLine("Failed GetMonitorDpiScale: " + error);
                    return(0);
                }

                //Check minimum and maximum scale
                if (deviceInfo.curScale < deviceInfo.minScale)
                {
                    deviceInfo.curScale = deviceInfo.minScale;
                }
                else if (deviceInfo.curScale > deviceInfo.maxScale)
                {
                    deviceInfo.curScale = deviceInfo.maxScale;
                }

                //Does not return custom set dpi values
                int[] defaultDpiValues = { 100, 125, 150, 175, 200, 225, 250, 300, 350, 400, 450, 500, 550, 600 };
                int   minScaleAbs      = Math.Abs(deviceInfo.minScale);
                int   curScaleAbs      = Math.Abs(deviceInfo.curScale);
                return(defaultDpiValues[minScaleAbs - curScaleAbs]);
            }
            catch
            {
                Debug.WriteLine("Failed GetMonitorDpiScale.");
                return(0);
            }
        }
Exemplo n.º 2
0
 private static extern int DisplayConfigGetDeviceInfo(ref DISPLAYCONFIG_SOURCE_DPI_SCALE_GET requestPacket);