0

ワイドスクリーン モニターを使用しており、アプリケーションの縦横比を維持し、設定されている可能性のある "間違った" 解像度を処理したいと考えています。

たとえば、モニタは解像度 1920x1080 で真円を表示できます。しかし、解像度1400x1050に設定すると、円は楕円のように見えます。

私たちのクライアントはこれに非常にうるさく、アプリを決定してサイズを変更し、処理できる解像度とモニターで完全な円を表示するように求めています..

ここに画像の説明を入力

それで、アプリケーションをスケーリングし、アスペクト比を維持して、実際のデバイスで適切な比率で表示することは何とか可能ですか?

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 に答える