C# 获取当前屏幕的宽高和位置

本文介绍了在WPF应用程序中如何获取当前窗口所在屏幕的信息,包括屏幕分辨率、位置及DPI等关键参数,并提供了直接获取主屏幕高宽的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇博客《C# 获取当前屏幕DPI》,介绍了如何获取当前屏幕的DPI设置

本章主要介绍如何获取当前窗口所在屏幕的信息

当前屏幕信息

如果当前是单屏幕,可以直接获取主屏幕

    var primaryScreen = Screen.PrimaryScreen;

如果当前是多屏,建议通过窗口句柄获取Screen信息

    var window = Window.GetWindow(ExportButton);//获取当前主窗口
    var intPtr = new WindowInteropHelper(window).Handle;//获取当前窗口的句柄
    var screen = Screen.FromHandle(intPtr);//获取当前屏幕

获取屏幕高宽/位置

DpiPercent

DPI转换比例常量,DpiPercent = 96;

为何DpiPercent为96 ?有一个概念“设备无关单位尺寸”,其大小为1/96英寸。比如:

【物理单位尺寸】=1/96英寸 * 96dpi = 1像素;

【物理单位尺寸】=1/96英寸 * 120dpi = 1.25像素;

关于WPF单位和系统DPI,可以参考《WPF编程宝典》中相关章节

Screen.Bounds

Bounds对应的是屏幕的分辨率,而要通过Bounds.Width获取屏幕的宽度,则需要将其转化为WPF单位的高宽。

步骤:

  1. 获取当前屏幕的物理尺寸(X/Y方向的像素)--如X方向 currentGraphics.DpiX / DpiPercent
  2. 将Screen.Bounds的信息转化为WPF单位信息 --如高度 screen.Bounds.Width / dpiXRatio
    using (Graphics currentGraphics = Graphics.FromHwnd(intPtr))
    {
        double dpiXRatio = currentGraphics.DpiX / DpiPercent;
        double dpiYRatio = currentGraphics.DpiY / DpiPercent;
        var width = screen.Bounds.Width / dpiXRatio;
        var height = screen.Bounds.Height / dpiYRatio;
        var left = screen.Bounds.Left / dpiXRatio;
        var top = screen.Bounds.Top / dpiYRatio;
    }

  

直接获取屏幕的高宽

也可以通过System.Windows.SystemParameters,直接获取主屏幕信息,不过这个类只能获取主屏幕的高宽

这里的高宽指的是实际高宽。

主屏幕:

    var screenHeight = SystemParameters.PrimaryScreenHeight;
    var screenWidth = SystemParameters.PrimaryScreenWidth;

多屏时全屏幕:

    var primaryScreenHeight = SystemParameters.FullPrimaryScreenHeight;
    var primaryScreenWidth = SystemParameters.FullPrimaryScreenWidth;

当前工作区域:(除去任务栏的区域)

    var workAreaWidth = SystemParameters.WorkArea.Size.Width;
    var workAreaHeight = SystemParameters.WorkArea.Size.Height;

 

关键字:WPF单位,屏幕高宽/位置

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
在VB (Visual Basic) 中,你可以使用 `Win32 API` 函数来获取其他窗口的度、度以及屏幕位置。以下是一些相关的函数示例: 1. 获取窗口大小 (`GetWindowRect` 或 `GetClientRect`): ```vbnet Public Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As Integer, ByRef lpRect As RECT) As Boolean ' RECT 结构体定义了窗口的左上角右下角坐标 Private Structure RECT Public Left As Long Public Top As Long Public Right As Long Public Bottom As Long End Structure Dim rect As RECT If GetWindowRect(hWndToCheck, rect) Then Dim width As Long = rect.Right - rect.Left Dim height As Long = rect.Bottom - rect.Top ' Do something with width and height End If ``` 2. 获取窗口相对于屏幕位置 (`GetWindowPlacement`): ```vbnet Public Declare Function GetWindowPlacement Lib "user32.dll" (ByVal hWnd As Integer, lpwndpl As WINDOWPLACEMENT) As Integer Private Structure WINDOWPLACEMENT Public length As Integer Public flags As Integer Public showCmd As Integer ' hwndInsertAfter ptMinTrackPos 不用于此场景 Public ptMaxTrackSize As POINT Public ptMaximizedScreenSize As POINT Public ptMinTrackSize As POINT Public ptPosition As POINT End Structure Dim wp As WINDOWPLACEMENT If GetWindowPlacement(hWndToCheck, wp) And wp.showCmd <> SW_HIDE Then Dim left As Integer = wp.ptPosition.X Dim top As Integer = wp.ptPosition.Y ' Do something with left and top End If ``` 其中 `hWndToCheck` 是你要查询的窗口句柄。记得引用 "user32.dll" 库,并确保程序有相应的权限访问其他窗口的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值