简体   繁体   中英

WPF: Windows.Forms.Screen reporting wrong Bounds for Monitors

I have a WPF application and want to output the bounds and working areas of the monitors I have. The code is as follows:

foreach (var screen in Screen.AllScreens.ToList().OrderByDescending(s => s.Primary)) // Primary Screen comes first
{
    Console.WriteLine("Device:       " + screen.DeviceName);
    Console.WriteLine("Bounds      : " + screen.Bounds);
    Console.WriteLine("Working Area: " + screen.WorkingArea);
}

DISPLAY1 is my primary screen, and DISPLAY2 is my secondary. Both have the same resolution: 1920 x 1080.

The weird thing is that this code gives the following output:

Device: \.\DISPLAY1 Bounds: {X=0,Y=0,Width=1920,Height=1080} Working Area: {X=0,Y=0,Width=1920,Height=1046}

Device: \.\DISPLAY2 Bounds: {X=2400,Y=0,Width=2400,Height=1350} Working Area: {X=2400,Y=0,Width=2400,Height=1308}

I have also changed the use of Screen.AllScreens with the Monitor class provided at: http://wpftutorial.net/ScreenResolutions.html but this still returns the same values. Any one ever encountered this issue?

Thanks

Joseph

看起来您在两个屏幕上都设置了不同的DPI。

When I am trying to find Scale factor from windows application it works fine but when I am trying to get scale factor from WPF it returns wrong value. I have same code in both application. scr.Bounds.Width returns different value in WPF and Windows form

 internal static decimal ScalingFactor(Screen scr)
        {
            DEVMODE dm = new DEVMODE();
            dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
            EnumDisplaySettings(scr.DeviceName, -1, ref dm);

            var scalingFactor = Math.Round(Decimal.Divide(dm.dmPelsWidth, scr.Bounds.Width), 2);
            return scalingFactor;
        }

Windows Form Output

WPF application Output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM