当前位置:网站首页>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
边栏推荐
- RF content learning
- Data indicators and data analysis models that designers need to understand
- C# wpf 通过绑定实现控件动态加载
- Day_ 12 smart health project jasperreports
- Haas506 2.0 development tutorial - Advanced Component Library -modem Sim (only supports versions above 2.2)
- Give up Visio, this drawing tool is really fragrant!
- 设计师需要懂的数据指标与数据分析模型
- Focusing on the smart city, Huawei cooperates with China Science and technology Xingtu to jointly develop a new digital blue ocean
- haas506 2.0开发教程-高级组件库-modem.sms(仅支持2.2以上版本)
- Day_ 05 smart communication health project - appointment management - appointment settings
猜你喜欢

Summary of business logic security ideas

English语法_形容词比较级 - 3级变化

华为软件测试笔试真题之变态逻辑推理题

Day_02 传智健康项目-预约管理-检查项管理

Day_ 09 smart health project - mobile terminal development - Mobile quick login and permission control

QT creator builds osgearth environment (osgqt msvc2017)

Docker practice - redis cluster deployment and micro service deployment project

Day_ 06 smart health project - mobile terminal development - physical examination appointment

sklearn sklearn中的模型调参利器 gridSearchCV(网格搜索)

从 WAN 到 SD-WAN 边缘设备的网络架构
随机推荐
Haas506 2.0 development tutorial - Advanced Component Library -modem Net (only supports versions above 2.2)
c#数据库报错问题大家帮我看看吧
Day_ 09 smart health project - mobile terminal development - Mobile quick login and permission control
Day_ 11 smart communication health project - graphic report and poi Report
Haas506 2.0 development tutorial -sntp (only versions above 2.2 are supported)
Golang regular regexp package use -04- use regular replacement (replaceall(), replaceallliteral(), replaceallfunc())
Give up Visio, this drawing tool is really fragrant!
华为软件测试笔试真题之变态逻辑推理题
haas506 2.0开发教程-sntp(仅支持2.2以上版本)
Index - MySQL
Laravel log channel grouping configuration
2020 smart power plant industry insight white paper
Home address exchange
C语言去除字符串尾部的换行(或其他字符)
golang正则regexp包使用-04-使用正则替换(ReplaceAll(),ReplaceAllLiteral(),ReplaceAllFunc())
Get to know webassembly quickly
C Advanced Learning -- Reflection
Detailed explanation of redis persistence, master-slave and sentry architecture
Machine learning artifact scikit learn minimalist tutorial
sklearn sklearn中的模型调参利器 gridSearchCV(网格搜索)