我希望将显示器的宽高比设为两位数:宽度和高度。例如4和3,5和4,16和9。
我为该任务编写了一些代码。也许这是更简单的方法吗?例如,某些库函数= \
/// <summary>
/// Aspect ratio.
/// </summary>
public struct AspectRatio
{
int _height;
/// <summary>
/// Height.
/// </summary>
public int Height
{
get
{
return _height;
}
}
int _width;
/// <summary>
/// Width.
/// </summary>
public int Width
{
get
{
return _width;
}
}
/// <summary>
/// Ctor.
/// </summary>
/// <param name="height">Height of aspect ratio.</param>
/// <param name="width">Width of aspect ratio.</param>
public AspectRatio(int height, int width)
{
_height = height;
_width = width;
}
}
public sealed class Aux
{
/// <summary>
/// Get aspect ratio.
/// </summary>
/// <returns>Aspect ratio.</returns>
public static AspectRatio GetAspectRatio()
{
int deskHeight = Screen.PrimaryScreen.Bounds.Height;
int deskWidth = Screen.PrimaryScreen.Bounds.Width;
int gcd = GCD(deskWidth, deskHeight);
return new AspectRatio(deskHeight / gcd, deskWidth / gcd);
}
/// <summary>
/// Greatest Common Denominator (GCD). Euclidean algorithm.
/// </summary>
/// <param name="a">Width.</param>
/// <param name="b">Height.</param>
/// <returns>GCD.</returns>
static int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
}
答案 0 :(得分:2)
Screen
类获取高度/宽度。请参阅以下代码:
private void button1_Click(object sender, EventArgs e)
{
int nGCD = GetGreatestCommonDivisor(Screen.PrimaryScreen.Bounds.Height, Screen.PrimaryScreen.Bounds.Width);
string str = string.Format("{0}:{1}", Screen.PrimaryScreen.Bounds.Height / nGCD, Screen.PrimaryScreen.Bounds.Width / nGCD);
MessageBox.Show(str);
}
static int GetGreatestCommonDivisor(int a, int b)
{
return b == 0 ? a : GetGreatestCommonDivisor(b, a % b);
}
答案 1 :(得分:0)
我认为没有库函数可以做到,但代码看起来不错。非常类似于在Javascript中执行相同操作的相关帖子中的答案:Javascript Aspect Ratio