当前位置:网站首页>C # how to obtain DPI and real resolution (can solve the problem that has been 96)
C # how to obtain DPI and real resolution (can solve the problem that has been 96)
2022-06-23 07:03:00 【Wool leek】
C# obtain DPI And real resolution ( It can be solved all the time 96 The problem of )
[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>
/// Get screen resolution, current physical size
/// </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>
/// The current system DPI_X size It's usually 96
/// </summary>
public static int DpiX
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int DpiX = GetDeviceCaps(hdc, LOGPIXELSX );
ReleaseDC(IntPtr.Zero, hdc);
return DpiX;
}
}
/// <summary>
/// The current system DPI_Y size It's usually 96
/// </summary>
public static int DpiY
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int DpiX = GetDeviceCaps(hdc,LOGPIXELSY);
ReleaseDC(IntPtr.Zero, hdc);
return DpiX;
}
}
/// <summary>
/// Get the real desktop resolution size
/// </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>
/// Gets the width scaling percentage (** When you get DPI The value of has always been 96 When , The value obtained by this method can be converted to 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>
/// Gets the height scaling percentage
/// </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
from https://blog.csdn.net/htiannuo/article/details/77086550
边栏推荐
猜你喜欢
随机推荐
[STL] summary of map usage of associated containers
746. climbing stairs with minimum cost - Dynamic Planning
[STL] summary of pair usage
idea安装 CloudToolkit 插件
C language operator priority formula
cmder
899. 有序队列
MySQL function
js 动态创建a href 循环下载文件只能下载10个或者固定数目的问题
[saison de remise des diplômes · technologie agressive er] votre choix, agenouillez - vous et partez
【STL】顺序容器之deque用法总结
MySQL MVCC多版本并发控制
WPF command directive and inotifypropertychanged
746. 使用最小花费爬楼梯-动态规划
聚焦行业,赋能客户 | 博云容器云产品族五大行业解决方案发布
Wechat applet - Global Monitoring of certain attribute changes of GlobalData, such as monitoring of network state switching
/Bin/sh no such file or directory problem
312. 戳气球
WPF Command指令和INotifyPropertyChanged
901. 股票价格跨度






![[project training] change of linear arrow](/img/a4/7554522c13684d7590d247f28a6965.png)

