如何设置 WPF 窗口大小以适合屏幕?

时间:2021-03-09 15:10:55

标签: wpf screen dpi

我想缩小窗口以适应屏幕。

通常我的窗口大小为 1000x800,因此在大多数情况下都有足够的空间,但我也在检查屏幕区域以确保它不会更大。然而,我的老板给了我他的电脑,他的屏幕比例设置为 150%,窗口变得比可用空间更大。我以为我通过将屏幕大小除以当前 dpi 来解决这个问题,但事实证明这并不适用于所有情况。

这是我用来根据当前 DPI 设置大小的方法:

private void setMainWindowDimensions() {
    //set window size and position with respect to current screen
    var windowInteropHelper = new WindowInteropHelper(nativeWindow);
    var screen = System.Windows.Forms.Screen.FromHandle(windowInteropHelper.Handle);
    var dpi = screen.getScale();
    nativeWindow.Width = Math.Min(screen.WorkingArea.Width / dpi.x, 1000);
    nativeWindow.Height = Math.Min(screen.WorkingArea.Height / dpi.y, 800);
}

这是我用来获取比例因子的 getScale() 扩展方法:

//https://stackoverflow.com/questions/29438430/how-to-get-dpi-scale-for-all-screens
public static (uint x, uint y) GetDpi(this S screen, DpiType dpiType = default) {
    var pnt = new System.Drawing.Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
    var mon = MonitorFromPoint(pnt, 2/*MONITOR_DEFAULTTONEAREST*/);
    GetDpiForMonitor(mon, dpiType, out var dpiX, out var dpiY);
    return (dpiX, dpiY);
}

//https://msdn.microsoft.com/en-us/library/windows/desktop/dd145062(v=vs.85).aspx
[DllImport("User32.dll")]
private static extern IntPtr MonitorFromPoint([In]System.Drawing.Point pt, [In]uint dwFlags);

//https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510(v=vs.85).aspx
[DllImport("Shcore.dll")]
private static extern IntPtr GetDpiForMonitor([In]IntPtr hmonitor, [In]DpiType dpiType, [Out]out uint dpiX, [Out]out uint dpiY);

这是 How to get DPI scale for all screens? 的答案。

当我在工作电脑上更改主显示器的比例时,上面的代码有效,它正确返回 dpi of (1.5, 1.5)。几个月后,我的老板回来了,他的电脑说它仍然无法工作。

我发现 DPI 来自主屏幕,即使窗口在另一个屏幕上打开,但我仍然无法弄清楚如何获得正确的值。

我一直在关注这篇文章 但它根本不起作用。 NativeHelpers 项目的方法返回一些值,但结果与原始方法相同。基本上 SetPerMonitorDPIAware 失败了,我不知道为什么。我使用了稍微修改的版本,我认为我的项目配置有问题,但即使我创建新的 WPF 项目,从 PerMonitorDPIWindow 类扩展窗口,结果是一样的 - 它抛出“启用每显示器 DPI 失败。"

private void createWindow() {
    var c = logCtx("Creating main applicaton window", ONE_TIME_LOG);
    var pma = PerMonitorDPIHelper.SetPerMonitorDPIAware();
    if (pma == 0) log("Could not set per monitor DPI awareness.", WARNING_LOG);
    else log("Per monitor awareness was set.", SUCCESS_LOG);
    window = new CWindow();
    nativeWindow.AllowDrop = true;
    setMainWindowDimensions();
    c.close();
}

private void setMainWindowDimensions() {
    //set window size and position with respect to current screen
    var windowInteropHelper = new WindowInteropHelper(nativeWindow);
    var screen = System.Windows.Forms.Screen.FromHandle(windowInteropHelper.Handle);
    var dpi = screen.getScale();

    var sdpi = PerMonitorDPIHelper.GetSystemDPI();
    var wdpi = PerMonitorDPIHelper.GetDpiForWindow(windowInteropHelper.Handle);
    log($@"DPI awareness: {PerMonitorDPIHelper.getPerMonitorDPIAware()}");
    log($@"system DPI: {sdpi}, window DPI: {wdpi}, screen DPI: {dpi}");

    nativeWindow.Width = Math.Min(screen.WorkingArea.Width / dpi.x, 1000);
    nativeWindow.Height = Math.Min(screen.WorkingArea.Height / dpi.y, 800);
}

private void onLoaded(object sender, RoutedEventArgs e) {
    var c = logCtx("Main window loaded", ONE_TIME_LOG);
    setMainWindowDimensions();
    c.close();
}

enter image description here

在这里,即使窗口在未缩放的屏幕上打开,您也可以看到返回的 DPI 为 1.5。

因为我有一个绘图应用程序,所以我也使用以下方法来确定真实的比例坐标:

private (double x, double y) density {
    get {
        var m = PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget
            .TransformToDevice;
        return (96 / m.M11 / 25.4, 96 / m.M22 / 25.4);
    }
}

此解决方案来自 this question,它也不适用于各种 dpi 方案。

我还在其中一条评论中看到 shcore 仅在 Windows 8 之后可用。我们的 PC 有 Win 10,但我们也希望支持 Windows 7。

还有更奇怪的事情。我的老板电脑实际上是一台只有一个主屏幕的笔记本电脑,但它仍然没有获得适当的 DPI。我现在无法访问它,所以没有我刚刚添加的日志...

是否有任何方法适用于所有场景?看起来这样的事情应该需要 2 行代码,但它变得如此复杂,我已经浪费了 2 天的时间,而且我没有想法:(

0 个答案:

没有答案
相关问题