在代码中获取计算机的推荐分辨率
Getting the recommended resolution for computer in Code
好的,我知道关于这个问题有类似的线索,但其中 none 确实适合我的情况:
如果我转到我的桌面,点击屏幕分辨率,我有一个推荐分辨率的菜单。我想从代码 (C#) 中获取它。
我的代码如下所示:
public static Size GetOptimalScreenResolution()
{
var scope = new System.Management.ManagementScope();
var query = new System.Management.ObjectQuery("SELECT * FROM CIM_VideoControllerResolution");
UInt32 maxHResolution = 0;
UInt32 maxVResolution = 0;
using (var searcher = new System.Management.ManagementObjectSearcher(scope, query))
{
var results = searcher.Get();
foreach (var item in results)
{
if ((UInt32)item["HorizontalResolution"] > maxHResolution)
maxHResolution = (UInt32)item["HorizontalResolution"];
if ((UInt32)item["VerticalResolution"] > maxVResolution)
maxVResolution = (UInt32)item["VerticalResolution"];
}
}
return new Size(maxHResolution, maxVResolution);
}
在我的桌面上,推荐的分辨率是 1680 X 1050。
这个方法returns是1680 X 1280。
这意味着推荐的分辨率不一定是水平和垂直的最大分辨率。我如何获得 1680 X 1050 的值? 谢谢!!!
解决方案 - 感谢 tolanj
public static Size GetOptimalScreenResolution()
{
var scope = new System.Management.ManagementScope();
var query = new System.Management.ObjectQuery("SELECT * FROM CIM_VideoControllerResolution");
UInt32 maxHResolution = 0;
UInt32 maxVResolution = 0;
UInt32 maxHForMaxVResolution = 0;
using (var searcher = new System.Management.ManagementObjectSearcher(scope, query))
{
var results = searcher.Get();
foreach (var item in results)
{
if ((UInt32) item["HorizontalResolution"] >= maxHResolution)
{
maxHResolution = (UInt32) item["HorizontalResolution"];
if ((UInt32)item["VerticalResolution"] > maxVResolution || maxHForMaxVResolution != maxHResolution)
{
maxVResolution = (UInt32)item["VerticalResolution"];
maxHForMaxVResolution = (UInt32)item["HorizontalResolution"];
}
}
}
}
return new Size(maxHResolution, maxVResolution);
}
好的,我知道关于这个问题有类似的线索,但其中 none 确实适合我的情况:
如果我转到我的桌面,点击屏幕分辨率,我有一个推荐分辨率的菜单。我想从代码 (C#) 中获取它。
我的代码如下所示:
public static Size GetOptimalScreenResolution()
{
var scope = new System.Management.ManagementScope();
var query = new System.Management.ObjectQuery("SELECT * FROM CIM_VideoControllerResolution");
UInt32 maxHResolution = 0;
UInt32 maxVResolution = 0;
using (var searcher = new System.Management.ManagementObjectSearcher(scope, query))
{
var results = searcher.Get();
foreach (var item in results)
{
if ((UInt32)item["HorizontalResolution"] > maxHResolution)
maxHResolution = (UInt32)item["HorizontalResolution"];
if ((UInt32)item["VerticalResolution"] > maxVResolution)
maxVResolution = (UInt32)item["VerticalResolution"];
}
}
return new Size(maxHResolution, maxVResolution);
}
在我的桌面上,推荐的分辨率是 1680 X 1050。 这个方法returns是1680 X 1280。 这意味着推荐的分辨率不一定是水平和垂直的最大分辨率。我如何获得 1680 X 1050 的值? 谢谢!!!
解决方案 - 感谢 tolanj
public static Size GetOptimalScreenResolution()
{
var scope = new System.Management.ManagementScope();
var query = new System.Management.ObjectQuery("SELECT * FROM CIM_VideoControllerResolution");
UInt32 maxHResolution = 0;
UInt32 maxVResolution = 0;
UInt32 maxHForMaxVResolution = 0;
using (var searcher = new System.Management.ManagementObjectSearcher(scope, query))
{
var results = searcher.Get();
foreach (var item in results)
{
if ((UInt32) item["HorizontalResolution"] >= maxHResolution)
{
maxHResolution = (UInt32) item["HorizontalResolution"];
if ((UInt32)item["VerticalResolution"] > maxVResolution || maxHForMaxVResolution != maxHResolution)
{
maxVResolution = (UInt32)item["VerticalResolution"];
maxHForMaxVResolution = (UInt32)item["HorizontalResolution"];
}
}
}
}
return new Size(maxHResolution, maxVResolution);
}