using System;
using System.Runtime.InteropServices;
namespace TProduct.WinFrmUI
{
//检查屏幕比例
public class PrimaryScreenSizeHelper
{
public static void CheckScale()
{
var scale_win7 = GetDpiX() / 96f;
var scale_win10 = GetScaleX();
var scale = scale_win7 > scale_win10 ? scale_win7 : scale_win10;
TProduct.WinFrmUI.GlobeParas.ScreenScale = scale;
#region 修改注册表默认勾选DPI
try
{
if (scale > 1.1)
{
var path = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers";
string content = "~ DPIUNAWARE";//~ PERPROCESSSYSTEMDPIFORCEOFF 勾选兼容性DPI //~ DPIUNAWARE 系统
var regeditName = System.Windows.Forms.Application.ExecutablePath;
var regeditKey = TProduct.Common.RegeditHelper.ReadRegeditKey(regeditName, path, TProduct.Model.eRegDomain.CurrentUser);
if (regeditKey == null || regeditKey.ToString() != "~ DPIUNAWARE")
{
TProduct.Common.RegeditHelper.WriteRegeditKey(content, regeditName, path, TProduct.Model.eRegDomain.CurrentUser);
}
TProduct.WinFrmUI.GlobeParas.IsNeedTip4ScreenScale = false;
}
else
{
TProduct.WinFrmUI.GlobeParas.IsNeedTip4ScreenScale = false;
}
}
catch
{//修改失败表示需要在主界面上提示一下
TProduct.WinFrmUI.GlobeParas.IsNeedTip4ScreenScale = true;
}
#endregion
}
#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 属性
///
/// 获取屏幕分辨率当前物理大小
///
public static System.Drawing.Size WorkingArea
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
System.Drawing.Size size = new System.Drawing.Size();
size.Width = GetDeviceCaps(hdc, HORZRES);
size.Height = GetDeviceCaps(hdc, VERTRES);
ReleaseDC(IntPtr.Zero, hdc);
return size;
}
}
///
/// 当前系统DPI_X 大小 一般为96
///
public static int GetDpiX()
{
IntPtr hdc = GetDC(IntPtr.Zero);
int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
ReleaseDC(IntPtr.Zero, hdc);
return DpiX;
}
///
/// 当前系统DPI_Y 大小 一般为96
///
public static int DpiY
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
ReleaseDC(IntPtr.Zero, hdc);
return DpiX;
}
}
///
/// 获取真实设置的桌面分辨率大小
///
public static System.Drawing.Size DESKTOP
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
System.Drawing.Size size = new System.Drawing.Size();
size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
ReleaseDC(IntPtr.Zero, hdc);
return size;
}
}
///
/// 获取宽度缩放百分比
///
public static float GetScaleX()
{
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;
}
///
/// 获取高度缩放百分比
///
public static float GetScaleY()
{
IntPtr hdc = GetDC(IntPtr.Zero);
float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
ReleaseDC(IntPtr.Zero, hdc);
return ScaleY;
}
#endregion
}
}