Mac开发-- 获取窗口应用ID

24 篇文章 0 订阅
11 篇文章 0 订阅

macOS 系统为每个屏幕分配一个 displayId,数据类型为 CGDirectDisplayID,32 位无符号整型。该 ID 对应唯一的 macOS 屏幕,通过这个ID 我们可以进行一些关于有关屏幕的操作,比如屏幕共享。
那么,我们怎么获取这个ID呢
首先我们要获取桌面应用

 CFArrayRef windowListAll = CGWindowListCopyWindowInfo(kCGWindowListOptionAll| kCGWindowListExcludeDesktopElements, kCGNullWindowID);

这里参数类型有:

typedef CF_OPTIONS(uint32_t, CGWindowListOption) {
    /* List all windows in this user session, including both on- and
     off-screen windows. The parameter `relativeToWindow' should be
     `kCGNullWindowID'. */
    kCGWindowListOptionAll = 0,

    /* List all on-screen windows in this user session, ordered from front to
     back. The parameter `relativeToWindow' should be `kCGNullWindowID'. */
    kCGWindowListOptionOnScreenOnly = (1 << 0),

    /* List all on-screen windows above the window specified by
     `relativeToWindow', ordered from front to back. */
    kCGWindowListOptionOnScreenAboveWindow = (1 << 1),

    /* List all on-screen windows below the window specified by
     `relativeToWindow', ordered from front to back. */
    kCGWindowListOptionOnScreenBelowWindow = (1 << 2),

    /* Include the window specified by `relativeToWindow' in any list,
     effectively creating `at-or-above' or `at-or-below' lists. */
    kCGWindowListOptionIncludingWindow = (1 << 3),

    /* Exclude any windows from the list that are elements of the desktop. */
    kCGWindowListExcludeDesktopElements = (1 << 4)
};

通过注解我们可以配置参数来获取我们想要的窗口,当然如果我们获取参数为kCGWindowListOptionAll就需要我们自我过滤,将参数转换成数组:

  NSArray* arr = CFBridgingRelease(windowListAll);

接下来就对数组进行遍历,然后进行筛选!
文章的最后,欢迎各位参与Mac开发的讨论,一起成长!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值