|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System;
- using System.Drawing;
- using System.Runtime.InteropServices;
-
- namespace Common.system
- {
-
-
-
- public class PrimaryScreen
- {
- #region Win32 API
- [DllImport("user32.dll")]
- private static extern IntPtr GetDC(IntPtr ptr);
- [DllImport("gdi32.dll")]
- private static extern int GetDeviceCaps(
- IntPtr hdc, // handle to DC
- int nIndex // index of capability
- );
- [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
- private static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
- #endregion
- #region DeviceCaps常量
-
-
-
- private const int HORZRES = 8;
-
-
-
-
- private const int VERTRES = 10;
-
-
-
-
- private const int LOGPIXELSX = 88;
-
-
-
-
- private const int LOGPIXELSY = 90;
-
-
-
-
- private const int DESKTOPVERTRES = 117;
-
-
-
-
- private const int DESKTOPHORZRES = 118;
- #endregion
-
- #region 属性
-
-
-
- public static Size WorkingArea
- {
- get
- {
- IntPtr hdc = GetDC(IntPtr.Zero);
- Size size = new Size
- {
- Width = GetDeviceCaps(hdc, HORZRES),
- Height = GetDeviceCaps(hdc, VERTRES)
- };
- ReleaseDC(IntPtr.Zero, hdc);
- return size;
- }
- }
-
-
-
- public static int DpiX
- {
- get
- {
- IntPtr hdc = GetDC(IntPtr.Zero);
- int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
- ReleaseDC(IntPtr.Zero, hdc);
- return DpiX;
- }
- }
-
-
-
- public static int DpiY
- {
- get
- {
- IntPtr hdc = GetDC(IntPtr.Zero);
- int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
- ReleaseDC(IntPtr.Zero, hdc);
- return DpiX;
- }
- }
-
-
-
- public static Size DESKTOP
- {
- get
- {
- IntPtr hdc = GetDC(IntPtr.Zero);
- Size size = new Size
- {
- Width = GetDeviceCaps(hdc, DESKTOPHORZRES),
- Height = GetDeviceCaps(hdc, DESKTOPVERTRES)
- };
- ReleaseDC(IntPtr.Zero, hdc);
- return size;
- }
- }
-
-
-
-
- public static float ScaleX
- {
- get
- {
- IntPtr hdc = GetDC(IntPtr.Zero);
-
- GetDeviceCaps(hdc, DESKTOPHORZRES);
-
- GetDeviceCaps(hdc, HORZRES);
- float ScaleX = GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
- ReleaseDC(IntPtr.Zero, hdc);
- return ScaleX;
- }
- }
-
-
-
- public static float ScaleY
- {
- get
- {
- IntPtr hdc = GetDC(IntPtr.Zero);
- float ScaleY = (float)GetDeviceCaps(hdc, DESKTOPVERTRES) / GetDeviceCaps(hdc, VERTRES);
- ReleaseDC(IntPtr.Zero, hdc);
- return ScaleY;
- }
- }
-
-
-
-
-
- public static SizeF ScaleScreenSize => new SizeF(DESKTOP.Width / (DpiX / 96f), DESKTOP.Height / (DpiY / 96f));
-
-
-
- public static RectangleF ScaleWorkingAreaSize
- {
- get
- {
- RectangleF rect = System.Windows.Forms.Screen.GetWorkingArea(new System.Drawing.Point((int)ScaleScreenSize.Width, (int)ScaleScreenSize.Height));
- return new RectangleF(((float)rect.X) / (DpiX / 96f), ((float)rect.Y) / (DpiX / 96f), ((float)rect.Width) / (DpiX / 96f), ((float)rect.Height) / (DpiY / 96f));
- }
- }
- #endregion
- }
- }
|