简体   繁体   中英

Screen Resolution Problem In WPF?

I'm gonna detect the resolution with the following code in WPF :

double height = System.Windows.SystemParameters.PrimaryScreenHeight;
double width = System.Windows.SystemParameters.PrimaryScreenWidth;

Current resolution of my screen is 1920*1200, but height is 960.0 and width is 1536.0 !!!

What's wrong with it ?
Thanks in advance.

Keep in mind that all WPF locations and sizes are floating point with a unit of 1/96 inch. Not pixels. This makes your window designs resolution independent. Doing the math: height = 960 / 96 = 10 inches. With your video adapter set to 120 DPI (120/96 = 125%): 10 * 120 = 1200 pixels. Same for width: 1536 / 96 * 120 = 1920 pixels.

System.Windows.Forms works in units of pixels. You are getting less than 1050 for the height because it subtracts the height of the taskbar. But for WPF you always want to work with 1/96", never pixels.

For an even more robust implementation, you should calculate the DPI factors on your system and work with those factors. A normal DPI value is 96, but some monitors may have different values. Consider that your code may be running on a monitor that has a different DPI value than 96. Consider this code:

    private static void CalculateDpiFactors()
    {
        Window MainWindow = Application.Current.MainWindow;
        PresentationSource MainWindowPresentationSource = PresentationSource.FromVisual(MainWindow);
        Matrix m = MainWindowPresentationSource.CompositionTarget.TransformToDevice;
        thisDpiWidthFactor = m.M11;
        thisDpiHeightFactor = m.M22;
    }

You can then use those ratios to get the final values:

CalculateDpiFactors();
double ScreenHeight = SystemParameters.PrimaryScreenHeight * thisDpiHeightFactor;
double ScreenWidth = SystemParameters.PrimaryScreenWidth * thisDpiWidthFactor;

The values of ScreenHeight and ScreenWidth should then match what you see in your monitor's Properties window.

尝试SystemParameters.FullPrimaryScreenWidth和FullPrimaryScreenHeight,我相信在删除屏幕上的任务栏和其他桌面带后,PrimaryScreenWidth和Height会返回可用客户端窗口的大小。

Please call this after your windows is loaded.

 public static class Ext
{
    public static Size GetNativePrimaryScreenSize(this Window window)
    {
        PresentationSource mainWindowPresentationSource = PresentationSource.FromVisual(window);
        Matrix m = mainWindowPresentationSource.CompositionTarget.TransformToDevice;
        var dpiWidthFactor = m.M11;
        var dpiHeightFactor = m.M22;
        double screenHeight = SystemParameters.PrimaryScreenHeight * dpiHeightFactor;
        double screenWidth = SystemParameters.PrimaryScreenWidth * dpiWidthFactor;

        return new Size(screenWidth, screenHeight);
    }
}

If you check "Use Windows XP style DPI scaling", then the returned value of "Screen.PrimaryScreen.Bounds" is correct. If not, the returned value is proportionally shrunk by the DPI value (which is the default).

To find the "Use Windows XP style DPI scaling" checkbox, expand the "To make text and on-screen items clearer in programs that aren't designed for high DPI" link in the following link: http://windows.microsoft.com/en-us/windows-vista/Make-the-text-on-your-screen-larger-or-smaller

You can use that:

float dpiX, dpiY;
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
     dpiX = graphics.DpiX;
     dpiY = graphics.DpiY;
}

double screenX = SystemParameters.PrimaryScreenWidth / 96 * dpiX;
double screenY = SystemParameters.PrimaryScreenHeight / 96 * dpiY;

try these..i believe this could correct the error.....

System.Windows.Form1.Screen.PrimaryScreen.Bounds.Height; System.Windows.Form1.Screen.PrimaryScreen.Bounds.Widtht;

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