×

# C# 获取windows显示缩放比例

hqy hqy 发表于2026-04-08 16:55:44 浏览6 评论0

抢沙发发表评论

# C# 获取windows显示缩放比例

C#
public const int DESKTOPVERTRES = 117;public const int DESKTOPHORZRES = 118;public const int SM_CXSCREEN = 0;public const int SM_CYSCREEN = 1;public const int SM_REMOTESESSION = 0x1000;[DllImport("user32.dll")]public static extern IntPtr GetDC(IntPtr ptr);[DllImport("user32.dll", EntryPoint = "ReleaseDC")]public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);[DllImport("gdi32.dll", EntryPoint = "GetDeviceCaps", SetLastError = true)]public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]public static extern int GetSystemMetrics(int mVal);private bool GetSystemScale(out float x, out float y, out int sourceWidth, out int sourceHeight){    x = 1;    y = 1;    sourceWidth = 0;    sourceHeight = 0;    IntPtr hdc = GetDC(IntPtr.Zero);    if (hdc != IntPtr.Zero)    {        sourceWidth = GetDeviceCaps(hdc, DESKTOPHORZRES);        sourceHeight = GetDeviceCaps(hdc, DESKTOPVERTRES);        int screenWidth = GetSystemMetrics(SM_CXSCREEN);        int screenHeight = GetSystemMetrics(SM_CYSCREEN);        x = (sourceWidth * 1.0f / screenWidth);        y = (sourceHeight * 1.0f / screenHeight);        ReleaseDC(IntPtr.Zero, hdc);        return true;    }    return false;}


打赏

本文链接:https://www.kinber.cn/post/6404.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客