如何在没有Windows窗体参考的情况下获得屏幕分辨率? [英] How to get the screen resolution without Windows Forms reference?

查看:103
本文介绍了如何在没有Windows窗体参考的情况下获得屏幕分辨率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获得运行测试的台式机的分辨率。以前,我是通过以下方式获取分辨率的:

I need to get the resolution of the desktop my test runs on. Previously I was grabbing the resolution like this:

Screen screen = Screen.PrimaryScreen;
int screenWidth = screen.Bounds.Width;
int screenHeight = screen.Bounds.Height;

不幸的是,使用 System.Windows.Forms 不可能了。我的项目是.NET Core,所以最好是为此需要一个NuGet软件包。

Unfortunately, using System.Windows.Forms isn't possible anymore. My project is .NET Core, so preferably I need a NuGet package for this.

如果有人有任何建议,我将不胜感激。

If anyone's got any suggestions I'd appreciate it.

推荐答案

如果不想使用 System.Windows.Forms (或不能),则可以可以使用Windows API函数 EnumDisplaySettings

If you don't want to use System.Windows.Forms (or cannot), you can get the screen resolution by using a Windows API function EnumDisplaySettings.

要调用WinAPI函数,可以使用P / Invoke功能。也可以在.NET Core上使用。请注意,这仅适用于Windows系统,因为非Windows目标上没有WinAPI。

To call the WinAPI function, you can use the P/Invoke feature that is also available on .NET Core. Please note that this will only work on a Windows system, because there's no WinAPI on non-Windows targets.

函数声明如下:

[DllImport("user32.dll")]
static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);

您还需要WinAPI DEVMODE 结构定义:

You also need the WinAPI DEVMODE struct definition:

[StructLayout(LayoutKind.Sequential)]
struct DEVMODE
{
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
  public string dmDeviceName;
  public short dmSpecVersion;
  public short dmDriverVersion;
  public short dmSize;
  public short dmDriverExtra;
  public int dmFields;
  public int dmPositionX;
  public int dmPositionY;
  public int dmDisplayOrientation;
  public int dmDisplayFixedOutput;
  public short dmColor;
  public short dmDuplex;
  public short dmYResolution;
  public short dmTTOption;
  public short dmCollate;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
  public string dmFormName;
  public short dmLogPixels;
  public int dmBitsPerPel;
  public int dmPelsWidth;
  public int dmPelsHeight;
  public int dmDisplayFlags;
  public int dmDisplayFrequency;
  public int dmICMMethod;
  public int dmICMIntent;
  public int dmMediaType;
  public int dmDitherType;
  public int dmReserved1;
  public int dmReserved2;
  public int dmPanningWidth;
  public int dmPanningHeight;
}

实际上,您不需要此结构的大部分字段。有趣的是 dmPelsWidth dmPelsHeight

Actually, you don't need most of this structure's fields. The interesting ones are dmPelsWidth and dmPelsHeight.

调用像这样的函数:

const int ENUM_CURRENT_SETTINGS = -1;

DEVMODE devMode = default;
devMode.dmSize = (short)Marshal.SizeOf(devMode);
EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref devMode);

现在,您可以在 dmPelsWidth devMode 结构的c>和 dmPelsHeight 字段。

Now you can check the screen resolution in the dmPelsWidth and dmPelsHeight fields of the devMode struct.

由于我们将 null 指定为第一个参数,因此该函数描述了运行调用线程的计算机上的当前显示设备。

Since we specify null as first argument, the function describes the current display device on the computer on which the calling thread is running.

这篇关于如何在没有Windows窗体参考的情况下获得屏幕分辨率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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