当前位置:网站首页>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
边栏推荐
- 315. 计算右侧小于当前元素的个数
- XML DTD record
- .h5文件忘记数据库名字,使用h5py打印
- Business logic design of physical warehouse and virtual warehouse in middle inventory
- JS dynamically creates a href circular download file. Only 10 or a fixed number of files can be downloaded
- The illustration shows three handshakes and four waves. Xiaobai can understand them
- WPF command directive and inotifypropertychanged
- mysql 函数
- 994. rotten oranges - non recursive method
- 【STL】关联容器之map用法总结
猜你喜欢
随机推荐
MySQL MVCC多版本并发控制
core. What is JS ---kalrry
MySQL index
309. 最佳买卖股票时机含冷冻期
[daily training] 513 Find the value in the lower left corner of the tree
[bull Chinese document] queue package used to process distributed jobs and messages in nodejs
Solve the mining virus sshd2 (redis does not set a password and clear the crontab scheduled task)
XML DTD record
[saison de remise des diplômes · technologie agressive er] votre choix, agenouillez - vous et partez
产品-Axure9(英文版),原型设计后台动态二级菜单显示内容
Swagger3 integrates oauth2 authentication token
.h5文件忘记数据库名字,使用h5py打印
mysql 基础查询
[STL] summary of deque usage of sequential containers
313. 超级丑数
System permission program cannot access SD card
EndNote20使用教程分享(未完
QT设计师无法修改窗口大小,无法通过鼠标拖动窗口改变大小的解决方案
开源OAuth2框架 实现SSO单点登录
MySQL redo log redo log








![[STL] unordered of associated container_ Map Usage Summary](/img/6a/d614f2f363fa5181c25e79ff8b0dab.png)
