vb.net 获取屏幕分辨率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4122691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:07:18  来源:igfitidea点击:

Getting Screen Resolution

vb.net

提问by Furqan Sehgal

I need to change screen resolutions using my application.

我需要使用我的应用程序更改屏幕分辨率。

How to:

如何:

  1. Read current resolution
  2. Changing it using my application and then returning to current on exit.
  1. 读取当前分辨率
  2. 使用我的应用程序更改它,然后在退出时返回当前状态。

Thanks, Furqan

谢谢,富尔坎

回答by Cody Gray

1) How do I read the current screen resolution?

1) 如何读取当前的屏幕分辨率?

Getting the current screen resolution is easy and built into the framework without having to delve into something like the GetSystemMetricsAPI. The Screenclass contains information about all of the display devices attached to the system. So to determine the resolution of the primary monitor, you could use something like:

获取当前屏幕分辨率很容易,并且内置到框架中,而无需深入研究GetSystemMetricsAPI 之类的东西。的Screen类包含关于所有附接到系统中的显示设备的信息。因此,要确定主显示器的分辨率,您可以使用以下内容:

 Dim screenWidth as Integer = Screen.PrimaryScreen.Bounds.Width
 Dim screenHeight as Integer = Screen.PrimaryScreen.Bounds.Height

To deal with multiple monitors, look into the Screen.AllScreensproperty, which returns an array of Screenobjects corresponding to each of the displays attached to the system. You could then determine (using the above code) the resolution of each of the displays attached to the computer by looping through the array of Screenobjects.

要处理多个显示器,请查看该Screen.AllScreens属性,该属性返回Screen与连接到系统的每个显示器对应的对象数组。然后,您可以通过循环遍历Screen对象数组来确定(使用上述代码)连接到计算机的每个显示器的分辨率。

2) How do I change the current screen resolution from my VB.NET application?

2) 如何从我的 VB.NET 应用程序更改当前屏幕分辨率?

Changing the screen resolution is a little more difficult. Before going any further, I would caution you that an application adjusting the user's display resolution is unexpected and potentially creates a hostile user environment. I know that I wouldn't use an application that changed my screen resolution without permission, and I'd think long and hard about using one that required me to do so at all. If you're trying to make a screen saver, there's probably a better and easier way to do what you want. That being said, it can be done from VB.NET if you're willing to P/Invoke a few functions from the Windows API.

更改屏幕分辨率有点困难。在继续之前,我要提醒您,调整用户显示分辨率的应用程序是意外的,并且可能会创建一个敌对的用户环境。我知道我不会在未经许可的情况下使用更改我的屏幕分辨率的应用程序,而且我会认真考虑使用一个完全需要我这样做的应用程序。如果您正在尝试制作屏幕保护程序,那么可能有一种更好、更简单的方法来做您想做的事。话虽如此,如果您愿意从 Windows API P/Invoke 一些函数,它可以从 VB.NET 完成。

The simplest way if you're only concerned about the case of a single monitor is using the ChangeDisplaySettingsfunction, which allows you to specify the graphics mode of the default display (the user's primary monitor only).

如果您只关心单个显示器的情况,最简单的方法是使用该ChangeDisplaySettings函数,它允许您指定默认显示的图形模式(仅限用户的主显示器)。

To handle the case of multiple monitors, you'll need to use the EnumDisplayDevicesfunction to obtain information about all of the display devices attached to the computer, and the ChangeDisplaySettingsExfunction to change a particular screen.

要处理多台显示器的情况,您需要使用该EnumDisplayDevices函数获取有关连接到计算机的所有显示设备的信息,以及ChangeDisplaySettingsEx更改特定屏幕的函数。

Check out pinvoke.netfor how to declare the signatures of the Windows API calls in VB.NET. A Google search turned up this thread, although I haven't verified the sample code that they provide.

查看pinvoke.net,了解如何在 VB.NET 中声明 Windows API 调用的签名。谷歌搜索出现了这个线程,虽然我还没有验证他们提供的示例代码。

To modify the screen resolution only while your app is running, and then restore it when the user quits your program, you'll want to save the current screen settings before you modify them and then restore them (using the same method calls) when your application exits.

要仅在您的应用程序运行时修改屏幕分辨率,然后在用户退出程序时恢复它,您需要在修改它们之前保存当前的屏幕设置,然后在您修改它们时恢复它们(使用相同的方法调用)应用程序退出。

回答by Alex Balabanov

How to get current screen resolution

如何获取当前屏幕分辨率

Public Function ScreenResolution() As String
        Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
        Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height
        Return intX & " × " & intY
End Function

How to change current screen resolution — solution

如何更改当前屏幕分辨率 —解决方案