-
C# 获取系统DPI缩放比例以及分辨率大小
方法
System.Windows.Forms.Screen
类
|
// 获取当前主屏幕分辨率 |
|
int screenWidth = Screen.PrimaryScreen.Bounds.Width; |
|
int screenHeight = Screen.PrimaryScreen.Bounds.Height; |
|
|
|
// 获取指定屏幕分辨率 |
|
Screen secondaryScreen = Screen.AllScreens[1]; |
|
int secondaryScreenWidth = secondaryScreen.Bounds.Width; |
|
int secondaryScreenHeight = secondaryScreen.Bounds.Height; |
|
System.Windows.SystemParameters
类
|
// 获取当前主屏幕分辨率 |
|
double screenWidth = SystemParameters.PrimaryScreenWidth; |
|
double screenHeight = SystemParameters.PrimaryScreenHeight; |
|
|
|
// 获取所有屏幕的分辨率 |
|
double virtualScreenWidth = SystemParameters.VirtualScreenWidth; |
|
double virtualScreenHeight = SystemParameters.VirtualScreenHeight; |
虚拟屏幕是指所有物理屏幕组合成的逻辑屏幕,可以用于跨越多个物理屏幕显示应用程序。
这两个方法都可以在正常情况下获取到屏幕的分辨率 - 当桌面缩放比例不是 100% 的时候获取到的分辨率就是“真实”的分辨率了,而是按缩放比例调整以后屏幕显示的内容的宽度和高度。
Windows API
一开始写了个只获取 DPI 缩放比例的,然后自己手动乘一下,但是调用System.Windows.Interop
的时候在同事电脑上找不到这个命名空间,不知道什么原因,后来找到了一篇类似功能的文章,微调了一下:
|
using System; |
|
using System.Drawing; |
|
using System.Runtime.InteropServices; |
|
|
|
namespace ScreenDPIHelper |
|
{ |
|
public class ScreenDPIHelper |
|
{ |
|
|
|
|
|
[DllImport("user32.dll")] |
|
static extern IntPtr GetDC(IntPtr ptr); |
|
[DllImport("gdi32.dll")] |
|
static extern int GetDeviceCaps( |
|
IntPtr hdc, // handle to DC |
|
int nIndex // index of capability |
|
); |
|
[DllImport("user32.dll", EntryPoint = "ReleaseDC")] |
|
static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc); |
|
|
|
|
|
|
|
|
|
|
|
const int HORZRES = 8; |
|
const int VERTRES = 10; |
|
const int LOGPIXELSX = 88; |
|
const int LOGPIXELSY = 90; |
|
const int DESKTOPVERTRES = 117; |
|
const int DESKTOPHORZRES = 118; |
|
|
|
|
|
|
|
|
|
|
|
// 获取屏幕分辨率当前物理大小 |
|
public static Size WorkingArea |
|
{ |
|
get |
|
{ |
|
IntPtr hdc = GetDC(IntPtr.Zero); |
|
Size size = new Size(); |
|
size.Width = GetDeviceCaps(hdc, HORZRES); |
|
size.Height = GetDeviceCaps(hdc, VERTRES); |
|
ReleaseDC(IntPtr.Zero, hdc); |
|
return size; |
|
} |
|
} |
|
// 当前系统DPI_X 大小 一般为96 |
|
public static int DpiX |
|
{ |
|
get |
|
{ |
|
IntPtr hdc = GetDC(IntPtr.Zero); |
|
int DpiX = GetDeviceCaps(hdc, LOGPIXELSX); |
|
ReleaseDC(IntPtr.Zero, hdc); |
|
return DpiX; |
|
} |
|
} |
|
// 当前系统DPI_Y 大小 一般为96 |
|
public static int DpiY |
|
{ |
|
get |
|
{ |
|
IntPtr hdc = GetDC(IntPtr.Zero); |
|
int DpiX = GetDeviceCaps(hdc, LOGPIXELSY); |
|
ReleaseDC(IntPtr.Zero, hdc); |
|
return DpiX; |
|
} |
|
} |
|
// 获取真实设置的桌面分辨率大小 |
|
public static Size DesktopResolution |
|
{ |
|
get |
|
{ |
|
IntPtr hdc = GetDC(IntPtr.Zero); |
|
Size size = new Size(); |
|
size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES); |
|
size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES); |
|
ReleaseDC(IntPtr.Zero, hdc); |
|
return size; |
|
} |
|
} |
|
// 获取宽度缩放百分比 |
|
public static float ScaleX |
|
{ |
|
get |
|
{ |
|
IntPtr hdc = GetDC(IntPtr.Zero); |
|
float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES); |
|
ReleaseDC(IntPtr.Zero, hdc); |
|
return ScaleX; |
|
} |
|
} |
|
// 获取高度缩放百分比 |
|
public static float ScaleY |
|
{ |
|
get |
|
{ |
|
IntPtr hdc = GetDC(IntPtr.Zero); |
|
float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES); |
|
ReleaseDC(IntPtr.Zero, hdc); |
|
return ScaleY; |
|
} |
|
} |
|
|
|
|
|
} |
|
} |
这个类用到了user32.dll
和gdi32.dll
这两个Win32动态链接库,并调用了其中的函数。如:
-
GetDC
: 该函数返回指定窗口客户区域或屏幕的设备上下文(DC)。 -
ReleaseDC
: 该函数释放由GetDC
函数获得的指定设备上下文(DC)。 -
GetDeviceCaps
: 该函数检索指定设备的某些功能,如分辨率,颜色深度,打印机输出分辨率等。
定义的常量参数分别为:
-
HORZRES
:水平方向分辨率。 -
VERTRES
:垂直方向分辨率。 -
LOGPIXELSX
:水平方向 DPI。 -
LOGPIXELSY
:垂直方向 DPI。 -
DESKTOPVERTRES
:真实的桌面分辨率的垂直大小。 -
DESKTOPHORZRES
:真实的桌面分辨率的水平大小。
参数的值是对应参数在 Win32 API 中的索引。
可获取的参数分别是:
-
WorkingArea
:获取屏幕分辨率的物理大小,也就是去掉任务栏等占据屏幕空间后的大小。 -
DpiX
:获取当前系统水平方向的 DPI ,DPI 是一个表示每英寸点数的度量单位,通常为 96。 -
DpiY
:获取当前系统垂直方向的 DPI 。 -
DESKTOP
:获取真实的桌面分辨率大小,包括任务栏等占据空间的部分。 -
ScaleX
:获取宽度的缩放比例,即当前屏幕的实际宽度与标准宽度(DESKTOPHORZRES)的比值。 -
ScaleY
:获取高度的缩放比例,即当前屏幕的实际高度与标准高度(DESKTOPVERTRES)的比值。
出处:https://www.cnblogs.com/BoiledYakult/p/17355056.html
栏目列表
最新更新
博克-定制图例
博克-注释和图例
Bokeh–添加小部件
向博克图添加标签
将交互式滑块添加到博克图
在 Bokeh 中添加按钮
谷歌、微软、Meta?谁才是 Python 最大的金
Objective-C语法之代码块(block)的使用
URL Encode
go语言写http踩得坑
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
[SQL Server]按照设定的周别的第一天算任意
Linux下定时自动备份Docker中所有SqlServer数
六、Danfo.js 数据可视化
五、plotly.js 数据可视化
四、数据分析、清理、转化
三、Danfo.js 入门
第二部分:使用 Danfo.js 和 Dnotebook 的据分
一、现代 JavaScript 概述
javascript 第一部分:基础知识
零、前言
uni-app开发跨平台小程序开发的诸多坑【转
前端设计模式——桥接模式