0

I have a widescreen monitor and I want to keep aspect ratio of my application and even handle "wrong" resolution that might be set.

For example monitor can display perfect circle with resolution 1920x1080. But circle looks like ellipse when resolution 1400x1050 is set.

Our client is really picky on this and asks to determine and resize app to display perfect circle with any resolution and monitor we can handle..

enter image description here

So is it possible somehow to scale application and keep aspect ratio to display it with proper proportions on real device?

4

2 回答 2

2

您可以覆盖OnResize事件Form以保留其纵横比:

private static readonly Size MaxResolution = GetMaxResolution();
private static double AspectRatio = (double)MaxResolution.Width / MaxResolution.Height;
private readonly PointF Dpi;

public YourForm() // constructor
{
    Dpi = GetDpi();
    AspectRatio *= Dpi.X / Dpi.Y;
}

private PointF GetDpi()
{
    PointF dpi = PointF.Empty;
    using (Graphics g = CreateGraphics())
    {
        dpi.X = g.DpiX;
        dpi.Y = g.DpiY;
    }
    return dpi;
}

private static Size GetMaxResolution()
{
    var scope = new ManagementScope();
    var q = new ObjectQuery("SELECT * FROM CIM_VideoControllerResolution");

    using (var searcher = new ManagementObjectSearcher(scope, q))
    {
        var results = searcher.Get();
        int maxHResolution = 0;
        int maxVResolution = 0;

        foreach (var item in results)
        {
            if (item.GetPropertyValue("HorizontalResolution") == null)
                continue;
            int h = int.Parse(item["HorizontalResolution"].ToString());
            int v = int.Parse(item["VerticalResolution"].ToString());
            if (h > maxHResolution || v > maxVResolution)
            {
                maxHResolution = h;
                maxVResolution = v;
            }
        }
        return new Size(maxHResolution, maxVResolution);
    }
}

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
    int minWidth = Math.Min(Width, (int)(Height * AspectRatio));
    if (WindowState == FormWindowState.Normal)
        Size = new Size(minWidth, (int)(minWidth / AspectRatio));
}

编辑
假设最大屏幕分辨率反映了“方形”要求,您可以获得此处此处所述的所有屏幕分辨率。您只需要找到它们的“最大值”。

请注意,Windows 通常没有真正的 DPI 值,我只使用 DPI 来比较水平 DPI 和垂直 DPI。

于 2014-10-11T17:58:13.227 回答
-2

获取窗口的高度并将宽度缩放为高度的一部分,或者根据需要反之。

void aspectRation(var width, var height){
    //Divide by 100 to get 1% of height, then multiply by (a number) to maintain ratio
    var newWidth = (height/100) * 80;

    //set the window size here
}

设置窗口大小将取决于您的应用程序。

于 2014-10-11T17:56:05.963 回答