如何从C#winform应用程序中检索屏幕分辨率?

时间:2010-03-08 16:16:30

标签: c# winforms

如何检索我的C#Winform App正在运行的屏幕分辨率?

5 个答案:

答案 0 :(得分:70)

您是否只需要标准应用程序将使用的区域,即排除Windows任务栏和停靠窗口?如果是,请使用Screen.WorkingArea property。否则,请使用Screen.Bounds

如果有多台显示器,您需要从表单中抓取屏幕,即

Form myForm;
Screen myScreen = Screen.FromControl(myForm);
Rectangle area = myScreen.WorkingArea;

如果您想知道哪个是主显示屏,请使用Screen.Primary属性。此外,您还可以从Screen.AllScreens属性获取屏幕列表。

答案 1 :(得分:6)

The given answer is correct, as far as it goes. However, when you have set your Text size to anything more than 125%, Windows (and .NET) start to fib about the size of the screen in order to do auto-scaling for you.

Most of the time, this is not an issue - you generally want Windows and .NET to do this. However, in the case where you really, truly need to know the actual count of pixels on the screen (say, you want to paint directly to the desktop DC), you can do the following. I've only tried this on Win10. YMMV on other Windows versions.

So far, this is the only way I've found to get true screen pixel count if you don't want to globally turn off DPI awareness in your app. Note that this example gets the Primary display size - you will need to modify this to get other screens.

outletCatalogNavItem.backBarButtonItem

答案 2 :(得分:4)

使用Screen类,并询问Bounds属性。 Screen类具有Primary Screen的静态属性,以及另一个返回a list of all the screens attached to the system的静态属性。

答案 3 :(得分:0)

这是我用来获取鼠标指针所在的工作区的屏幕分辨率的方法。我可以启动程序,然后将鼠标移到另一台显示器上并获得该分辨率。

internal static void GetScreenResolution(ref double screenX, ref double screenY)
{
    Screen myScreen = Screen.FromPoint(Cursor.Position);
    System.Drawing.Rectangle area = myScreen.WorkingArea;
    screenX = area.Width;
    screenY = area.Height;   
}

也许不是最好的解决方案,但是我可以生成比例因子并使用它来缩放控件。

我在WPF程序中使用了它,我不得不添加一个参考System.Windows.Forms
我还将它放在单独的类中,这样我的主代码中不会出现冲突。

答案 4 :(得分:-1)

Screen.PrimaryScreen.WorkingArea.Size()