-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimaryScreen.cs
144 lines (135 loc) · 4.44 KB
/
PrimaryScreen.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace PrimaryScreen
{
public class PrimaryScreen
{
#region Win32 API
[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);
#endregion
#region DeviceCaps常量
const int HORZRES = 8;
const int VERTRES = 10;
const int LOGPIXELSX = 88;
const int LOGPIXELSY = 90;
const int DESKTOPVERTRES = 117;
const int DESKTOPHORZRES = 118;
#endregion
#region 属性
/// <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>
/// 获取宽度缩放百分比
/// </summary>
public static float ScaleX
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
float t = (float)GetDeviceCaps(hdc, DESKTOPHORZRES);
float d = (float)GetDeviceCaps(hdc, HORZRES);
float ScaleX = t / d;
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
}
}
//static void Main(string[] args)
//{
// // 获取屏幕分辨率当前物理大小
// var workingArea = PrimaryScreen.PrimaryScreen.WorkingArea;
// Console.WriteLine($"WorkingArea: Width={workingArea.Width}, Height={workingArea.Height}");
// // 当前系统DPI_X 大小 一般为96
// var dpiX = PrimaryScreen.PrimaryScreen.DpiX;
// Console.WriteLine($"DpiX: {dpiX}");
// // 当前系统DPI_Y 大小 一般为96
// var dpiY = PrimaryScreen.PrimaryScreen.DpiY;
// Console.WriteLine($"DpiY: {dpiY}");
// // 获取真实设置的桌面分辨率大小
// var desktop = PrimaryScreen.PrimaryScreen.DESKTOP;
// Console.WriteLine($"Desktop: Width={desktop.Width}, Height={desktop.Height}");
// // 获取宽度缩放百分比
// var scaleX = PrimaryScreen.PrimaryScreen.ScaleX;
// Console.WriteLine($"ScaleX: {scaleX}");
// // 获取高度缩放百分比
// var scaleY = PrimaryScreen.PrimaryScreen.ScaleY;
// Console.WriteLine($"ScaleY: {scaleY}");
//}