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

查看:25
本文介绍了如何在没有 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 函数,您可以使用 .NET Core 上也提供的 P/Invoke 功能.请注意,这仅适用于 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;
}

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

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);

现在您可以在 devMode 结构体的 dmPelsWidthdmPelsHeight 字段中检查屏幕分辨率.

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天全站免登陆