반응형
    internal class DisplaySettings
    {
        [DllImport("user32.dll")]
        private static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);

        [DllImport("user32.dll")]
        private static extern int ChangeDisplaySettings(ref DEVMODE devMode, int flags);

        private const int ENUM_CURRENT_SETTINGS = -1;
        private const int CDS_UPDATEREGISTRY = 0x01;
        private const int CDS_FULLSCREEN = 0x04;

        [StructLayout(LayoutKind.Sequential)]
        public struct DEVMODE
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string dmDeviceName;

            public short dmSpecVersion;
            public short dmDriverVersion;
            public short dmSize;
            public short dmDriverExtra;
            public int dmFields;
            public short dmOrientation;
            public short dmPaperSize;
            public short dmPaperLength;
            public short dmPaperWidth;
            public short dmScale;
            public short dmCopies;
            public short dmDefaultSource;
            public short dmPrintQuality;
            public short dmColor;
            public short dmDuplex;
            public short dmYResolution;
            public short dmTTOption;
            public short dmCollate;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string dmFormName;

            public short dmLogPixels;
            public int dmBitsPerPel;
            public int dmPelsWidth;
            public int dmPelsHeight;
            public int dmDisplayFlags;
            public int dmDisplayFrequency;
            public int dmICMMethod;
            public int dmICMIntent;
            public int dmMediaType;
            public int dmDitherType;
            public int dmReserved1;
            public int dmReserved2;
            public int dmPanningWidth;
            public int dmPanningHeight;
        }

        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);

        [DllImport("gdi32.dll")]
        private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

        [DllImport("user32.dll")]
        private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

        private const int LOGPIXELSX = 88;
        private const int LOGPIXELSY = 90;
        private const int HORZRES = 8;
        private const int VERTRES = 10;

        public static bool ChangeDisplayResolution(int width, int height)
        {
            DEVMODE devMode = new DEVMODE();
            devMode.dmSize = (short)Marshal.SizeOf(devMode);

            if (EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref devMode))
            {
                devMode.dmPelsWidth = width;
                devMode.dmPelsHeight = height;
                int result = ChangeDisplaySettings(ref devMode, CDS_UPDATEREGISTRY);
                return result == 0;
            }

            return false;
        }

        public static bool ChangeDisplayScaling(int scalingPercent)
        {
            DEVMODE devMode = new DEVMODE();
            devMode.dmSize = (short)Marshal.SizeOf(devMode);

            if (EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref devMode))
            {
                devMode.dmFields = devMode.dmFields | 0x20000; // DM_DISPLAYFLAGS
                devMode.dmDisplayFlags = (scalingPercent == 100) ? 0 : 0x0001; // DMDFO_DEFAULT or DMDFO_STRETCH

                int result = ChangeDisplaySettings(ref devMode, CDS_UPDATEREGISTRY);
                return result == 0;
            }

            return false;
        }

        public struct ResolutionScaleInfo
        {
            public int Width;
            public int Height;
            public float ScalingFactorX;
            public float ScalingFactorY;
        }

        public static ResolutionScaleInfo GetResolutionScaleInfo()
        {
            IntPtr hdc = GetDC(IntPtr.Zero);
            int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
            int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
            int width = GetDeviceCaps(hdc, HORZRES);
            int height = GetDeviceCaps(hdc, VERTRES);
            float scalingFactorX = dpiX / 96f;
            float scalingFactorY = dpiY / 96f;

            Console.WriteLine($"Resolution: {width} x {height}");
            Console.WriteLine($"Scaling: {scalingFactorX:F2} x {scalingFactorY:F2}");

            // Release the device context
            if (hdc != IntPtr.Zero) ReleaseDC(IntPtr.Zero, hdc);

            return new ResolutionScaleInfo()
            {
                Width = width,
                Height = height,
                ScalingFactorX = scalingFactorX,
                ScalingFactorY = scalingFactorY,
            };
        }
    }
반응형

+ Recent posts