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开发的讨论,一起成长!