C# WPF获取屏幕缩放比例(4种方法)

1,调用windows系统函数

public class GetSreenScaleAPI
{
    public const int HORZRES = 8;
    public const int VERTRES = 10;
    public const int LOGPIXELSX = 88;
    public const int LOGPIXELSY = 90;
    public const int DESKTOPVERTRES = 117;
    public const int DESKTOPHORZRES = 118;

    [DllImport("user32.dll")]
    public static extern IntPtr GetDC(IntPtr ptr);

    [DllImport("gdi32.dll")]
    public static extern int GetDeviceCaps(
        IntPtr hdc, // handle to DC
        int nIndex  // index of capability
        );

    [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
    public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc); [DllImport("user32.dll")]
    public static extern IntPtr GetDesktopWindow();

    public static float GetSreenScale()
    {
        var hdc = GetDC(GetDesktopWindow());
        int nWidth = GetDeviceCaps(hdc, DESKTOPHORZRES);
        ReleaseDC(IntPtr.Zero, hdc);
        float f_Scale = (float)nWidth / (float)SystemParameters.PrimaryScreenWidth;
        return f_Scale;
    }
}

定义完后时直接调用GetSreenScale;注:这种方式在界面启动时调用可以获取到准确的值,二次调用无效。

2,采样winform用法

const double DpiPercent = 96;
private double GetScreenScale()
{
    var intPtr = new WindowInteropHelper(this).Handle;//获取当前窗口的句柄
    var screen = Screen.FromHandle(intPtr);//获取当前屏幕

    double scale = 0;
    using (Graphics currentGraphics = Graphics.FromHwnd(intPtr))
    {
        double dpiXRatio = currentGraphics.DpiX / DpiPercent;
        double dpiYRatio = currentGraphics.DpiY / DpiPercent;

        scale = dpiXRatio; // dpiYRatio均可
    }
    return scale;
}

需要引用 using System.Windows.Forms;  注:同样,这种方式在界面启动时调用可以获取到准确的值,二次调用无效。

3,FormVisual

private double GetScreenScale()
{
	double scale = 0;
	PresentationSource presentationSource = PresentationSource.FromVisual(this);
	if (presentationSource != null)
	{
		Matrix m = presentationSource.CompositionTarget.TransformToDevice;
		double scaleX = m.M11;
		double scaleY = m.M22;
		
		scale = scaleX; // scaleY均可
	}
	
	return scale;
}

直接调用GetSreenScale;  注:这种方式在窗体未显化之前无法使用,程序运行期间反复可用。

4,VisualTreeHelper

double dpiScaleX = VisualTreeHelper.GetDpi(this).DpiScaleX;
double dpiScaleY = VisualTreeHelper.GetDpi(this).DpiScaleY;

以上2句使用其中任意一句即可; 注:在程序运行任何时间均可调用,反复可用。

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# WPF 实现页面的自适应分辨率,可以采取以下几个步骤: 1. 使用相对布局:使用 XAML 的布局容器(如 Grid、StackPanel、WrapPanel 等),并设置控件的行、列以及其他布局属性,以实现自适应布局。 2. 使用 Grid 的行和列定义:通过设置 Grid 的行和列的定义,可以让控件根据窗口大小自动调整位置和大小。可以使用 "*" 表示自动调整大小的列或行,使用具体数值(如 "200")表示固定大小的列或行。 3. 使用 ViewBox 控件:ViewBox 是一个用于缩放其内容的容器控件。将需要自适应分辨率的内容放置在 ViewBox ,并设置 Stretch 属性为 Uniform 或 UniformToFill,即可实现内容的自动缩放。 4. 响应窗口大小改变事件:在窗口的 SizeChanged 事件编写代码,根据窗口的大小变化,调整控件的位置和大小来实现自适应分辨率。 5. 使用 VisualStateManager:通过使用 VisualStateManager,可以在不同的状态下应用不同的布局。根据窗口大小或其他条件,设置不同的视觉状态,在不同的状态下使用不同的布局。 6. 使用分辨率相关信息:可以通过 System.Windows.Forms.Screen 类获取当前屏幕的分辨率信息,并根据该信息调整控件的布局和大小。 综上所述,以上是一些常用的方法来实现 C# WPF 页面的自适应分辨率。根据具体的需求和场景,可以选择适合的方法来实现页面的自适应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值