获取屏幕分辨率 [英] Getting Screen Resolution

查看:61
本文介绍了获取屏幕分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to change screen resolutions using my application.

如何:

  1. 读取当前分辨率
  2. 使用我的应用程序更改它,然后在退出时返回当前状态.

谢谢,弗坎

推荐答案

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

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

Getting the current screen resolution is easy and built into the framework without having to delve into something like the GetSystemMetrics API. The Screen class 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:

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

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

To deal with multiple monitors, look into the Screen.AllScreens property, which returns an array of Screen objects 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 Screen objects.

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

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

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

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

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

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

查看 pinvoke.net,了解如何在 VB.NET 中声明 Windows API 调用的签名.Google 搜索出现了此线程,尽管我还没有验证示例代码他们提供的.

Check out pinvoke.net for 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.

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

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.

这篇关于获取屏幕分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆