当前位置:网站首页>C# 获取DPI和真实分辨率的方式(可以解决一直是96的问题)
C# 获取DPI和真实分辨率的方式(可以解决一直是96的问题)
2022-06-23 05:31:00 【羊毛韭菜】
C# 获取DPI和真实分辨率的方式(可以解决一直是96的问题)
[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;
/// <summary>
/// 获取屏幕分辨率当前物理大小
/// </summary>
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;
}
}
/// <summary>
/// 当前系统DPI_X 大小 一般为96
/// </summary>
public static int DpiX
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int DpiX = GetDeviceCaps(hdc, LOGPIXELSX );
ReleaseDC(IntPtr.Zero, hdc);
return DpiX;
}
}
/// <summary>
/// 当前系统DPI_Y 大小 一般为96
/// </summary>
public static int DpiY
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int DpiX = GetDeviceCaps(hdc,LOGPIXELSY);
ReleaseDC(IntPtr.Zero, hdc);
return DpiX;
}
}
/// <summary>
/// 获取真实设置的桌面分辨率大小
/// </summary>
public static Size DESKTOP
{
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;
}
}
/// <summary>
/// 获取宽度缩放百分比 (**当获取的DPI的值一直是96的时候,可以通过用此方法获取的值转化为DPI,ScaleX * 96**)
/// </summary>
public static float ScaleX
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int t = GetDeviceCaps(hdc, DESKTOPHORZRES);
int d = GetDeviceCaps(hdc, HORZRES);
float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
ReleaseDC(IntPtr.Zero, hdc);
return ScaleX;
}
}
/// <summary>
/// 获取高度缩放百分比
/// </summary>
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;
}
}
#endregion
边栏推荐
- Detailed explanation of redis persistence, master-slave and sentry architecture
- phpStudy设置301重定向
- Data indicators and data analysis models that designers need to understand
- Docker practice - redis cluster deployment and micro service deployment project
- json转化为proto
- leetcode - 572. 另一棵树的子树
- Give up Visio, this drawing tool is really fragrant!
- 什么是客户体验自动化?
- Docker实战 -- 部署Redis集群与部署微服务项目
- Leetcode topic resolution valid Sudoku
猜你喜欢
随机推荐
C language stepping on the pit: document coding error, resulting in Base64 Chinese coding error
idea的去除转义的复制粘贴
What is a PDCA cycle? How to integrate PDCA cycle and OKR
Home address exchange
程序员的真实想法 | 每日趣闻
Data indicators and data analysis models that designers need to understand
下载oss文件并修改文件名
Coordinate transformation
Leetcode topic resolution single number
Get to know webassembly quickly
Haas 506 2.0 Tutoriel de développement - bibliothèque de composants avancés - modem. SMS (ne prend en charge que les versions supérieures à 2,2)
数值计算方法 Chapter7. 计算矩阵的特征值和特征向量
设计师需要懂的数据指标与数据分析模型
射频内容学习
c#数据库报错问题大家帮我看看吧
Vs+qt project transferred to QT Creator
Termux
C语言去除字符串尾部的换行(或其他字符)
haas506 2.0开发教程-sntp(仅支持2.2以上版本)
Leetcode topic resolution valid Sudoku









